-
Notifications
You must be signed in to change notification settings - Fork 29.7k
Fix DropdownButtonFormField not updating when value updated in onChanged
#170050
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
Fix DropdownButtonFormField not updating when value updated in onChanged
#170050
Conversation
|
@bleroux Would you like to review this PR? Seems like you recently edited this widget. |
DropdownMenuFormField not updating when value updated in onChangedDropdownButtonFormField not updating when value updated in onChanged
|
Thanks for working on this. 🙏 Unfortunately, this change is problematic. The new logic will reset the Sample codeimport 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: Scaffold(body: Padding(padding: EdgeInsets.all(8.0), child: HomePage())),
);
}
}
class HomePage extends StatefulWidget {
const HomePage({super.key});
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
int count = 0;
static final List<DropdownMenuItem<String>> menuItems =
['A', 'B', 'C', 'D'].map((String item) {
return DropdownMenuItem<String>(value: item, child: Text(item));
}).toList();
@override
Widget build(BuildContext context) {
return Column(
children: [
const SizedBox(height: 20),
DropdownButtonFormField<String>(
value: 'A',
items: menuItems,
onChanged: (String? value) {
setState(() {
count++;
});
},
decoration: InputDecoration(
labelText: 'Edition n°$count',
border: const OutlineInputBorder(),
),
),
],
);
}
}
(Just run the code sample and try to select an item in the menu, without this PR it works, with it the selection can’t be changed). The root of this problem and of #169983, is that
Deprecating @justinmc @QuncCccccc Do you think we should revisit this decision and rename this field? |
|
Thanks for your clarification! My understanding about the value parameter was incorrect. So, if the value parameter is actually the initial value and works as expected, does that mean that doing something like #169983 is not actually supported? |
I commented on #169983 (comment) to explain how it can be done with the current API. |
|
I guess we can close this then? Maybe there should be a test similar to your example code, so that changes like mine don't get past 😄 |
Yes, sorry for the inconvenience.
That's very true, if you want to file a PR to add such a test, it will be welcomed. If not I will try to file one next week. |
|
Do you prefer if I open a new PR or repurpose this one? |
If possible open a new one as it will make things easier to track for people who will read this in some years 😄 |
|
Opened a new PR |
I say it should be renamed! Assuming any effort to migrate is not too crazy. |
This PR adds a new test that verifies that the value param is only used on initial build and when resetting the field. Test for #170050 (comment) ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. --------- Co-authored-by: Bruno Leroux <[email protected]>
…ialValue" (#170805) ## Description This PR renames the DropdownButtonFormField constuctor parameter 'value' to 'initialValue'. See #170050 (comment) and #170050 (comment) for some context. ## Related Issue Fixes [DropdownButtonFormField retains selected value even after setting value to null](#169983 (comment)) ## Tests Adds 2 tests (one to validate the deprecated parameter can still be used, one for the dart fix). Updates many (renaming the confusing parameter).
This PR adds a new test that verifies that the value param is only used on initial build and when resetting the field. Test for flutter#170050 (comment) ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. --------- Co-authored-by: Bruno Leroux <[email protected]>
…ialValue" (flutter#170805) ## Description This PR renames the DropdownButtonFormField constuctor parameter 'value' to 'initialValue'. See flutter#170050 (comment) and flutter#170050 (comment) for some context. ## Related Issue Fixes [DropdownButtonFormField retains selected value even after setting value to null](flutter#169983 (comment)) ## Tests Adds 2 tests (one to validate the deprecated parameter can still be used, one for the dart fix). Updates many (renaming the confusing parameter).
This PR adds a check to
didUpdateWidgetto make sure that the latest value is rendered if it was updated during theonChangedcallback.Fixes #169983
Pre-launch Checklist
///).