nc-photos/lib/widget/simple_input_dialog.dart

63 lines
1.4 KiB
Dart
Raw Normal View History

2021-07-09 09:35:46 +02:00
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
class SimpleInputDialog extends StatefulWidget {
SimpleInputDialog({
2021-07-23 22:05:57 +02:00
Key? key,
2021-07-09 09:35:46 +02:00
this.initialText,
this.hintText,
this.validator,
}) : super(key: key);
@override
createState() => _SimpleInputDialogState();
2021-07-23 22:05:57 +02:00
final String? initialText;
final String? hintText;
final FormFieldValidator<String>? validator;
2021-07-09 09:35:46 +02:00
}
class _SimpleInputDialogState extends State<SimpleInputDialog> {
@override
build(BuildContext context) {
return AlertDialog(
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,
),
),
actions: [
TextButton(
onPressed: _onSavePressed,
child: Text(
MaterialLocalizations.of(context).saveButtonLabel,
),
),
],
);
}
void _onSavePressed() {
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
}