-
-
Notifications
You must be signed in to change notification settings - Fork 541
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[General]: Improve focus behavior when validating a FormBuilderTextField when autovalidateMode is not set to AutovalidateMode.disabled #1462
Labels
bug
Something isn't working
Comments
The second issue is related to the focus behavior of import 'package:flutter/material.dart';
import 'package:flutter_form_builder/flutter_form_builder.dart';
import 'package:form_builder_validators/form_builder_validators.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: FormScreen(),
);
}
}
class FormScreen extends StatefulWidget {
const FormScreen({super.key});
@override
State<FormScreen> createState() => _FormScreenState();
}
class _FormScreenState extends State<FormScreen> {
@override
Widget build(BuildContext context) {
// Using it here causes the bug
final size = MediaQuery.of(context).size;
return Scaffold(
appBar: AppBar(
title: const Text('Flutter Form Builder Example'),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: SingleChildScrollView(
child: FormChild(),
),
),
);
}
}
class FormChild extends StatefulWidget {
const FormChild({super.key});
@override
State<FormChild> createState() => _FormChildState();
}
class _FormChildState extends State<FormChild> {
final GlobalKey<FormBuilderState> _formBuilderKey = GlobalKey<FormBuilderState>();
AutovalidateMode _autovalidateMode = AutovalidateMode.disabled;
@override
Widget build(BuildContext context) {
// Using it here will not cause the bug.
// final size = MediaQuery.of(context).size;
return FormBuilder(
key: _formBuilderKey,
autovalidateMode: _autovalidateMode,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Row(
spacing: 10.0,
children: [
// Submit button
ElevatedButton(
style: ButtonStyle(shape: WidgetStatePropertyAll(RoundedRectangleBorder(borderRadius: BorderRadius.circular(5.0)))),
onPressed: () {
FocusManager.instance.primaryFocus?.unfocus();
},
child: const Text('Unfocus'),
),
ElevatedButton(
style: ButtonStyle(shape: WidgetStatePropertyAll(RoundedRectangleBorder(borderRadius: BorderRadius.circular(5.0)))),
onPressed: () {
final result = _formBuilderKey.currentState?.saveAndValidate();
if(result != true && _autovalidateMode != AutovalidateMode.onUserInteraction){
setState(() {
_autovalidateMode = AutovalidateMode.onUserInteraction;
});
}
},
child: const Text('submit'),
),
],
),
),
// TextField 1
FormBuilderTextField(
name: 'textfield1',
decoration: const InputDecoration(
labelText: 'Enter text 1',
),
validator: FormBuilderValidators.required(),
),
const SizedBox(height: 20),
],
),
);
}
}
The behavior that causes the error is described in the video below. focus_textfield2.mp4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is there an existing issue for this?
Package/Plugin version
10.0.0-dev.1
Platforms
Flutter doctor
Flutter doctor
Minimal code example
Code sample
Current Behavior
I've developed a mixin called ScopedGetItMixin for use with StatefulWidget that contains a FormBuilder. However, I've noticed that after the form calls saveAndValidate, tapping a button to open a showModalBottomSheet causes a FormBuilderTextField to unexpectedly regain focus, triggering the keyboard to appear.
The main cause might be ScopedGetItMixin, but at the moment, I’m not sure why using ScopedGetItMixin results in this behavior.
Expected Behavior
I hope the focus behavior of FormBuilderTextField gets fixed.
Steps To Reproduce
I will describe it again through the video below.
focus_textfield1.mp4
Aditional information
No response
The text was updated successfully, but these errors were encountered: