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;
|
2022-03-10 18:39:57 +00:00
|
|
|
final IconData? icon;
|
|
|
|
final String? initialValue;
|
2020-04-25 15:16:33 +00:00
|
|
|
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;
|
2022-03-10 18:39:57 +00:00
|
|
|
final int? maxLength;
|
2020-04-25 12:38:15 +00:00
|
|
|
|
2020-04-25 15:16:33 +00:00
|
|
|
const SingleInputDialog({
|
2022-03-10 18:39:57 +00:00
|
|
|
Key? key,
|
|
|
|
required this.title,
|
|
|
|
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,
|
2022-03-11 16:09:37 +00:00
|
|
|
}) : super(key: key);
|
2020-04-25 12:38:15 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
__InputURLDialogState createState() => __InputURLDialogState();
|
|
|
|
}
|
|
|
|
|
2020-04-25 15:16:33 +00:00
|
|
|
class __InputURLDialogState extends State<SingleInputDialog> {
|
2022-03-10 18:39:57 +00:00
|
|
|
TextEditingController? _controller;
|
2020-04-25 12:38:15 +00:00
|
|
|
|
|
|
|
bool get _isValid =>
|
2022-03-10 18:39:57 +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,
|
2022-03-10 18:39:57 +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(
|
2022-03-10 18:39:57 +00:00
|
|
|
child: Text(tr("Cancel")!.toUpperCase()),
|
2020-04-25 12:38:15 +00:00
|
|
|
onPressed: () => Navigator.of(context).pop(),
|
|
|
|
textColor: Colors.red,
|
|
|
|
),
|
|
|
|
|
|
|
|
// Confirm
|
|
|
|
MaterialButton(
|
2022-03-10 18:39:57 +00:00
|
|
|
child: Text(tr("Confirm")!.toUpperCase()),
|
2020-04-25 12:38:15 +00:00
|
|
|
onPressed: _isValid
|
2022-03-10 18:39:57 +00:00
|
|
|
? () => Navigator.of(context).pop(_controller!.text)
|
2020-04-25 12:38:15 +00:00
|
|
|
: null,
|
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|