Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.4 kB
19
Indexable
Never
class _MyFormState extends State<MyForm> {
  final TextEditingController _textController = TextEditingController();
  final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
  String text = '';

  void changeText() {
    setState(() {
      text = _textController.text;
      text = NumberFormat('#,###').format(int.parse(text));
    });
  }

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Scaffold(
        body: Form(
          key: _formKey,
          child: Column(
            children: [
              TextFormField(
                keyboardType: TextInputType.number,
                controller: _textController,
                decoration: const InputDecoration(
                  labelText: 'Enter some number',
                ),
                validator: (value) {
                  if (value!.isEmpty) {
                    return 'Please enter some text';
                  }
                  return null;
                },
              ),
              ElevatedButton(
                onPressed: () {
                  if (_formKey.currentState!.validate()) {
                    changeText();
                  }
                },
                child: const Text('Submit'),
              ),
              Text(text)
            ],
          ),
        ),
      ),
    );
  }
}