showNInputDialog function
Shows an input dialog returning the entered text.
Implementation
Future<String?> showNInputDialog({
required BuildContext context,
required String title,
String? message,
String? initialText,
String confirmLabel = 'Save',
String cancelLabel = 'Cancel',
String? hintText,
TextInputType? keyboardType,
int maxLines = 1,
String? Function(String?)? validator,
}) {
final TextEditingController controller =
TextEditingController(text: initialText);
return showDialog<String>(
context: context,
child: Dialog(
title: title,
subtitle: message,
content: TextField(
controller: controller,
keyboardType: keyboardType,
maxLines: maxLines,
decoration: InputDecoration(
hintText: hintText,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(AppRadius.small)),
),
),
actions: <Widget>[
TextButton(
onPressed: () => Navigator.of(context).pop(),
child: Text(cancelLabel),
),
FilledButton(
onPressed: () {
if (validator != null) {
final String? error = validator(controller.text);
if (error != null) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text(error)),
);
return;
}
}
Navigator.of(context).pop(controller.text);
},
child: Text(confirmLabel),
),
],
),
);
}