1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-22 12:59:21 +00:00
comunicmobile/lib/ui/dialogs/single_input_dialog.dart

86 lines
2.3 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;
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;
final int? maxLength;
2020-04-25 12:38:15 +00:00
2020-04-25 15:16:33 +00:00
const SingleInputDialog({
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> {
TextEditingController? _controller;
2020-04-25 12:38:15 +00:00
bool get _isValid =>
(_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,
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()),
2020-04-25 12:38:15 +00:00
onPressed: () => Navigator.of(context).pop(),
textColor: Colors.red,
),
// Confirm
MaterialButton(
child: Text(tr("Confirm")!.toUpperCase()),
2020-04-25 12:38:15 +00:00
onPressed: _isValid
? () => Navigator.of(context).pop(_controller!.text)
2020-04-25 12:38:15 +00:00
: null,
),
],
);
}
}