Files
fcs/lib/pages/widgets/dialog_input.dart

104 lines
2.8 KiB
Dart
Raw Normal View History

2020-12-10 20:06:15 +06:30
import 'package:fcs/helpers/theme.dart';
import 'package:fcs/pages/main/util.dart';
import 'package:fcs/pages/widgets/progress.dart';
import 'package:flutter/material.dart';
import 'local_text.dart';
class DialogInput extends StatefulWidget {
2021-09-10 14:25:37 +06:30
final String? value;
final String? label;
2021-01-09 19:11:47 +06:30
2021-09-10 14:25:37 +06:30
const DialogInput({Key? key, this.label, this.value}) : super(key: key);
2020-12-10 20:06:15 +06:30
@override
_DialogInputState createState() => _DialogInputState();
}
class _DialogInputState extends State<DialogInput> {
final _formKey = GlobalKey<FormState>();
TextEditingController _controller = new TextEditingController();
2021-01-08 17:13:51 +06:30
final _focusNode = FocusNode();
2020-12-10 20:06:15 +06:30
bool _isLoading = false;
@override
void initState() {
super.initState();
if (widget.value != null) {
2021-09-10 14:25:37 +06:30
_controller.text = widget.value!;
2021-01-08 17:13:51 +06:30
_focusNode.addListener(() {
if (_focusNode.hasFocus) {
_controller.selection = TextSelection(
baseOffset: 0, extentOffset: _controller.text.length);
}
});
2020-12-10 20:06:15 +06:30
}
}
@override
Widget build(BuildContext context) {
return LocalProgress(
inAsyncCall: _isLoading,
child: AlertDialog(
title: LocalText(
context,
2021-09-10 14:25:37 +06:30
widget.label!,
2020-12-10 20:06:15 +06:30
fontSize: 20,
color: primaryColor,
),
content: Form(
key: _formKey,
child: TextField(
controller: _controller,
2021-01-09 19:11:47 +06:30
focusNode: _focusNode,
2021-01-08 17:13:51 +06:30
autofocus: true,
2021-01-11 19:35:26 +06:30
keyboardType: TextInputType.numberWithOptions(decimal: true),
2020-12-10 20:06:15 +06:30
decoration: new InputDecoration(
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(color: primaryColor, width: 1.0))),
),
),
actions: <Widget>[
2024-01-09 13:11:22 +06:30
TextButton(
2020-12-10 20:06:15 +06:30
child: LocalText(
context,
'btn.cancel',
color: labelColor,
),
onPressed: () {
_controller.clear();
Navigator.of(context).pop();
}),
2024-01-09 13:11:22 +06:30
TextButton(
style: TextButton.styleFrom(backgroundColor: primaryColor),
// color: primaryColor,
2020-12-10 20:06:15 +06:30
child: LocalText(
context,
'btn.ok',
color: Colors.white,
fontWeight: FontWeight.bold,
),
onPressed: () async {
2021-09-10 14:25:37 +06:30
if (!_formKey.currentState!.validate()) return;
2020-12-10 20:06:15 +06:30
_save();
})
],
),
);
}
_save() {
setState(() {
_isLoading = true;
});
try {
Navigator.pop<String>(context, _controller.text);
} catch (e) {
showMsgDialog(context, "Error", e.toString());
} finally {
setState(() {
_isLoading = false;
});
}
}
}