-
-
Notifications
You must be signed in to change notification settings - Fork 542
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]: FormField gains focus
back after a modal/dialog is closed
#1429
Comments
Hi!
Example: import 'package:flutter/material.dart';
import 'package:flutter_form_builder/flutter_form_builder.dart';
import 'package:flutter_localizations/flutter_localizations.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(
title: 'Flutter FormBuilder Example',
debugShowCheckedModeBanner: false,
localizationsDelegates: const [
FormBuilderLocalizations.delegate,
...GlobalMaterialLocalizations.delegates,
GlobalWidgetsLocalizations.delegate,
],
supportedLocales: FormBuilderLocalizations.supportedLocales,
home: const _ExamplePage(),
);
}
}
class _ExamplePage extends StatefulWidget {
const _ExamplePage();
@override
State<_ExamplePage> createState() => _ExamplePageState();
}
class _ExamplePageState extends State<_ExamplePage> {
final _formKey = GlobalKey<FormBuilderState>();
@override
Widget build(BuildContext context) {
return Scaffold(
body: FormBuilder(
key: _formKey,
child: Column(
children: [
FormBuilderTextField(
name: 'full_name',
decoration: const InputDecoration(labelText: 'Full Name'),
validator: FormBuilderValidators.compose([
FormBuilderValidators.required(),
]),
),
const SizedBox(height: 10),
ElevatedButton(
onPressed: () {
_formKey.currentState?.saveAndValidate();
debugPrint(_formKey.currentState?.value.toString());
},
child: const Text('Print'),
)
],
),
),
);
}
} Thanks a lot |
Hi @deandreamatias import 'package:flutter/material.dart';
import 'package:flutter_form_builder/flutter_form_builder.dart';
import 'package:flutter_localizations/flutter_localizations.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(
title: 'Flutter FormBuilder Example',
debugShowCheckedModeBanner: false,
localizationsDelegates: const [
FormBuilderLocalizations.delegate,
...GlobalMaterialLocalizations.delegates,
GlobalWidgetsLocalizations.delegate,
],
supportedLocales: FormBuilderLocalizations.supportedLocales,
home: const _ExamplePage(),
);
}
}
class _ExamplePage extends StatefulWidget {
const _ExamplePage();
@override
State<_ExamplePage> createState() => _ExamplePageState();
}
class _ExamplePageState extends State<_ExamplePage> {
final _formKey = GlobalKey<FormBuilderState>();
@override
Widget build(BuildContext context) {
return Scaffold(
body: FormBuilder(
key: _formKey,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
FormBuilderTextField(
name: 'full_name',
decoration: const InputDecoration(labelText: 'Full Name'),
validator: FormBuilderValidators.compose([
FormBuilderValidators.required(),
]),
),
const SizedBox(height: 10),
ElevatedButton(
onPressed: () {
final isValid = _formKey.currentState!.saveAndValidate();
if (!isValid) return;
FocusScope.of(context).unfocus();
showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: const Text('Form Data'),
content: Text(_formKey.currentState!.value.toString()),
actions: [
TextButton(
onPressed: () => Navigator.of(context).pop(),
child: const Text('Close'),
),
],
);
},
);
},
child: const Text('Open Dialog'),
),
],
),
),
);
}
}
Results:Screen.Recording.2024-12-30.at.8.35.04.PM.mov |
Related to #1297 |
I can replicate this error with Flutter pure components, so this bug maybe is a behavior of Flutter SDK. Form(
child: TextFormField(
decoration: InputDecoration(labelText: "Full name"),
validator: FormBuilderValidators.compose([
FormBuilderValidators.required(),
FormBuilderValidators.minLength(1)
]),
),
) Related to flutter/flutter#153079, flutter/flutter#92349 and flutter/flutter#145155 I think that would exist a way to skip this behavior, but can't think a some way right now |
Probably we can open an issue on Flutter SDK itself? @deandreamatias |
I think that the best way is follow the flutter/flutter#153079 issue and when Flutter team solve this, we try again if our use cases are solved. |
Is there an existing issue for this?
Package/Plugin version
9.3.0 <Tried on latest 9.4.1 as well>
Platforms
Flutter doctor
Flutter doctor
Minimal code example
Code sample
Current Behavior
When any text field is tap to enter some value, and any dialog or modal is opened with the field still in focus. When the dialog exit the focus goes back to the text field. Which is weird, it shouldn't happen.
I've already tried the
FocusScope.of(context).unfocus()
call before opening the keyboard but doesn't work.Expected Behavior
When a text field is in focus and any dialog or modal is opened, the field should lose its focus i.e.
unfocus
and shouldn't gain focus back after the modal or dialog is closed.Steps To Reproduce
FYI:
This won't happen if you unfocus the field yourself i.e. tap on the screen or the "done" button on keyboard.
Aditional information
It only happens if the text field has gained its focus one time, if you don't tap the field, it won't gain its focus on closing of modal or dialog.
The text was updated successfully, but these errors were encountered: