import 'package:flutter/material.dart';
class TestForm extends StatefulWidget {
const TestForm({super.key});
@override
State<TestForm> createState() => _TestFormState();
}
class _TestFormState extends State<TestForm> {
final formKey = GlobalKey<FormState>();
void validateForm() {
final invalidFields = formKey.currentState?.validateGranularly();
if (invalidFields != null && invalidFields.isNotEmpty) {
Scrollable.ensureVisible(invalidFields.first.context);
}
}
@override
Widget build(BuildContext context) {
return SingleChildScrollView(
child: Form(
key: formKey,
child: Column(
children: [
const TextField(),
const TextField(),
const TextField(),
OutlinedButton(
onPressed: validateForm,
child: const Text('Validate'),
),
],
),
),
);
}
}
import 'package:flutter/material.dart';
class TestForm extends StatefulWidget {
const TestForm({super.key});
@override
State<TestForm> createState() => _TestFormState();
}
class _TestFormState extends State<TestForm> {
final formKey = GlobalKey<FormState>();
void validateForm() {
final invalidFields = formKey.currentState?.validateGranularly();
if (invalidFields != null && invalidFields.isNotEmpty) {
Scrollable.ensureVisible(invalidFields.first.context);
}
}
@override
Widget build(BuildContext context) {
return SingleChildScrollView(
child: Form(
key: formKey,
child: Column(
children: [
const TextField(),
const TextField(),
const TextField(),
OutlinedButton(
onPressed: validateForm,
child: const Text('Validate'),
),
],
),
),
);
}
}