Skip to content

Instantly share code, notes, and snippets.

@atreeon
Created November 29, 2022 17:00
Show Gist options
  • Save atreeon/9bcb0ebec0c01142fc7e530c46f94e56 to your computer and use it in GitHub Desktop.
Save atreeon/9bcb0ebec0c01142fc7e530c46f94e56 to your computer and use it in GitHub Desktop.
Flutter TextFormField event examples
// ignore_for_file: annotate_overrides
// ignore_for_file: prefer_const_constructors
// ignore_for_file: prefer_const_literals_to_create_immutables
// ignore_for_file: unused_local_variable
// ignore_for_file: library_private_types_in_public_api
// ignore_for_file: unused_field
import 'package:flutter/material.dart';
const Color darkBlue = Color.fromARGB(255, 18, 32, 47);
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Center(
child: Blah(),
),
),
);
}
}
class Blah extends StatefulWidget {
_BlahState createState() => _BlahState();
}
class _BlahState extends State<Blah> {
var onValue = <String>[];
final _formKey = GlobalKey<FormState>();
Widget build(BuildContext context) {
return Form(
key: _formKey,
child: Column(
children: [
Text('TextFormField'),
Text('Interactive view of events'),
Text(''),
Text('onFieldSubmitted: press return on web'),
Text('onSaved: press button (calls _formKey.currentState.save())'),
Text('onChanged: change text'),
Text('onEditingComplete: press return on web'),
Text(
'More: https://stackoverflow.com/questions/63690311/flutter-textfield-difference-between-onedittingcomplete-and-onsubmitted'),
Text(' '),
TextFormField(
onFieldSubmitted: (x) =>
setState(() => onValue.insert(0, "onFieldSubmitted")),
onSaved: (x) => setState(() => onValue.insert(0, "onSaved")),
onChanged: (x) => setState(() => onValue.insert(0, "onChanged")),
onEditingComplete: () =>
setState(() => onValue.insert(0, "onEditingComplete")),
),
ElevatedButton(
child: Text('Click me'),
onPressed: () {
if (_formKey.currentState!.validate()) {
_formKey.currentState!.save();
print('valid');
}
},
),
...onValue.map((x) => Text(x)),
],
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment