1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-30 00:36:28 +00:00
comunicmobile/lib/ui/dialogs/single_input_dialog.dart

91 lines
2.4 KiB
Dart
Raw Normal View History

2020-04-25 12:38:15 +00:00
import 'package:comunic/utils/intl_utils.dart';
import 'package:flutter/material.dart';
2020-04-27 17:21:30 +00:00
/// Dialog to use to ask the user to enter a value
///
/// @author Pierre Hubert
2020-04-25 15:16:33 +00:00
class SingleInputDialog extends StatefulWidget {
2020-04-25 12:38:15 +00:00
final String title;
2020-04-25 15:16:33 +00:00
final IconData icon;
final String initialValue;
final String label;
final bool Function(String) checkInput;
final String errorMessage;
2020-04-27 11:27:37 +00:00
final bool canBeEmpty;
2020-04-27 17:21:30 +00:00
final int maxLines;
final int maxLength;
2020-04-25 12:38:15 +00:00
2020-04-25 15:16:33 +00:00
const SingleInputDialog({
2020-04-25 12:38:15 +00:00
Key key,
@required this.title,
2020-04-25 15:16:33 +00:00
@required this.icon,
@required this.initialValue,
@required this.label,
@required this.checkInput,
@required this.errorMessage,
2020-04-27 11:27:37 +00:00
this.canBeEmpty = false,
2020-04-27 17:21:30 +00:00
this.maxLines = 1,
this.maxLength,
2020-04-25 12:38:15 +00:00
}) : assert(title != null),
2020-04-25 15:16:33 +00:00
assert(label != null),
assert(checkInput != null),
assert(errorMessage != null),
2020-04-27 17:21:30 +00:00
assert(maxLines != null),
2020-04-25 12:38:15 +00:00
super(key: key);
@override
__InputURLDialogState createState() => __InputURLDialogState();
}
2020-04-25 15:16:33 +00:00
class __InputURLDialogState extends State<SingleInputDialog> {
2020-04-25 12:38:15 +00:00
TextEditingController _controller;
bool get _isValid =>
2020-04-27 11:29:11 +00:00
(_controller.text.isEmpty && widget.canBeEmpty) ||
(_controller.text.isNotEmpty && widget.checkInput(_controller.text));
2020-04-25 12:38:15 +00:00
@override
void initState() {
super.initState();
2020-04-25 15:16:33 +00:00
_controller = new TextEditingController(text: widget.initialValue);
2020-04-25 12:38:15 +00:00
}
@override
Widget build(BuildContext context) {
return AlertDialog(
title: Text(widget.title),
content: TextField(
controller: _controller,
onChanged: (s) => setState(() {}),
2020-04-27 17:21:30 +00:00
maxLines: widget.maxLines,
maxLength: widget.maxLength,
2020-04-25 12:38:15 +00:00
decoration: InputDecoration(
2020-04-27 11:27:37 +00:00
icon: widget.icon == null ? null : Icon(widget.icon),
2020-04-25 12:38:15 +00:00
alignLabelWithHint: true,
2020-04-25 15:16:33 +00:00
labelText: widget.label,
2020-04-25 12:38:15 +00:00
errorText: _controller.text.isNotEmpty && !_isValid
2020-04-25 15:16:33 +00:00
? widget.errorMessage
2020-04-25 12:38:15 +00:00
: null,
),
),
actions: <Widget>[
// Cancel
MaterialButton(
child: Text(tr("Cancel").toUpperCase()),
onPressed: () => Navigator.of(context).pop(),
textColor: Colors.red,
),
// Confirm
MaterialButton(
child: Text(tr("Confirm").toUpperCase()),
onPressed: _isValid
? () => Navigator.of(context).pop(_controller.text)
: null,
),
],
);
}
}