scrollToFirstInvalidField function
Scrolls to the first invalid FormField in the subtree rooted at formContext.
Must be called after Form.validate so that FormFieldState.hasError is up to date.
Implementation
void scrollToFirstInvalidField(BuildContext formContext) {
WidgetsBinding.instance.addPostFrameCallback((_) {
bool found = false;
void visitor(Element element) {
if (found) return;
if (element is StatefulElement && element.state is FormFieldState) {
if ((element.state as FormFieldState).hasError) {
found = true;
Scrollable.ensureVisible(
element,
duration: const Duration(milliseconds: 300),
curve: Curves.easeInOut,
alignment: 0.1,
);
return;
}
}
element.visitChildren(visitor);
}
(formContext as Element).visitChildren(visitor);
});
}