nc-photos/app/lib/widget/simple_input_dialog.dart

68 lines
1.6 KiB
Dart
Raw Normal View History

2021-07-09 09:35:46 +02:00
import 'package:flutter/material.dart';
class SimpleInputDialog extends StatefulWidget {
2021-09-15 08:58:06 +02:00
const SimpleInputDialog({
2021-07-23 22:05:57 +02:00
Key? key,
2021-09-28 06:44:03 +02:00
required this.buttonText,
this.titleText,
2021-07-09 09:35:46 +02:00
this.initialText,
this.hintText,
this.validator,
this.obscureText = false,
2021-07-09 09:35:46 +02:00
}) : super(key: key);
@override
createState() => _SimpleInputDialogState();
2021-09-28 06:44:03 +02:00
final String buttonText;
final String? titleText;
2021-07-23 22:05:57 +02:00
final String? initialText;
final String? hintText;
final FormFieldValidator<String>? validator;
final bool obscureText;
2021-07-09 09:35:46 +02:00
}
class _SimpleInputDialogState extends State<SimpleInputDialog> {
@override
build(BuildContext context) {
return AlertDialog(
title: widget.titleText == null ? null : Text(widget.titleText!),
2021-07-09 09:35:46 +02:00
content: Form(
key: _formKey,
child: TextFormField(
decoration: widget.hintText == null
? null
: InputDecoration(hintText: widget.hintText),
validator: widget.validator,
onSaved: (value) {
2021-07-23 22:05:57 +02:00
_formValue.text = value!;
2021-07-09 09:35:46 +02:00
},
initialValue: widget.initialText,
obscureText: widget.obscureText,
2021-07-09 09:35:46 +02:00
),
),
actions: [
TextButton(
2021-09-28 06:44:03 +02:00
onPressed: _onButtonPressed,
child: Text(widget.buttonText),
2021-07-09 09:35:46 +02:00
),
],
);
}
2021-09-28 06:44:03 +02:00
void _onButtonPressed() {
2021-07-09 09:35:46 +02:00
if (_formKey.currentState?.validate() == true) {
_formValue = _FormValue();
2021-07-23 22:05:57 +02:00
_formKey.currentState!.save();
2021-07-09 09:35:46 +02:00
Navigator.of(context).pop(_formValue.text);
}
}
final _formKey = GlobalKey<FormState>();
2021-07-23 22:05:57 +02:00
var _formValue = _FormValue();
2021-07-09 09:35:46 +02:00
}
class _FormValue {
2021-07-23 22:05:57 +02:00
late String text;
2021-07-09 09:35:46 +02:00
}