I've got a form and I want to re-do a calculation when the user finishes editing any of the fields. I thought onFieldSubmitted
would do it but this never gets called. In the minimal example below I want the recalc()
function to be called when the user moves off the field. I know onChange
will work but I don't want the recalculation to be done every time the user enters a character as it is a bit slow and it's irritating, in my opinion, to see validation errors while still composing the fields contents.
I'm using Flutter 2.10.4 with the "Edge (web-javascript)" device.
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return const MaterialApp(
title: 'Flutter Demo',
home: MyHomePage(title: 'Form demo'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: const Center(child: MyForm()),
);
}
}
class MyForm extends StatefulWidget {
const MyForm({Key? key}) : super(key: key);
@override
State<MyForm> createState() => _MyFormState();
}
class _MyFormState extends State<MyForm> {
int _q = 0;
void recalc() {
print("Recalculate");
setState(() {
_q += 1;
});
}
@override
Widget build(BuildContext context) {
return Form(
child: Column(
children: [
TextFormField(
onFieldSubmitted: (value) {
recalc();
},
),
TextFormField(
onFieldSubmitted: (value) {
recalc();
},
),
Text('changed $_q')
],
));
}
}
EDIT: I realise now that onFieldSubmitted
gets fired if I press ENTER on a field, not when I tab away from it. I'm using the web-javascript device on a PC so I do have an ENTER key - not sure how this would work if I were using the Android emulator