-
Notifications
You must be signed in to change notification settings - Fork 28.5k
[Proposal] Add a text
parameter to TextField
and TextFormField
to update their values
#167983
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
Comments
This approach introduces limitations for more advanced TextField use cases. If implemented, it might be provided as an optional feature for a basic scenario. |
What limitations? I am not restricting the API, just expanding it. You can still use a controller, as I said and explained how an |
I am labelling this as a proposal to add text/value parameter to textfield/textformfield |
TextField
and TextFormField
should have a more reactive APITextField
and TextFormField
a text parameter to update its value
TextField
and TextFormField
a text parameter to update its valuetext
parameter to TextField
and TextFormField
to update their values
@maheshj01 can you please assign this to me? I can look into this |
I don't get this change, there is |
@iamdipanshusingh Please wait until the secondary triage takes a look at this issue and this is prioritized. So that we know this is something flutter team would be interested to work on in the near future. |
Most of the Flutter widgets work in a reactive manner where they inform they changed and they accept a value. By changing the value in the widget's constructor, the widget reacts. TextField only does half of this, it only informs change. It can't be updated in a declarative manner, it can't react to change, unless we use the controller in an imperative manner, not declarative. Thus, if my state manager (e.g. Bloc) has a new state, and I have to update a form with 10 TextFields, then I need to create 10 TextEditingControllers, their variables, initialise them, and not forget to dispose them, and set them when the state changes. That's a lot of extra boilerplate if you ever had to deal with forms and values coming from other sources. The TextEditingController has its benefits, it allows us to control lots of things and also get notified of changes, but for the vast majority of cases we only want to set the text and get change notifications, and having this at the widget level will simplify these flows a lot. |
Got it. Actually it would be useful in that specific case π |
I've wanted to do this kind of thing for so long. This was the very first quirk that bugged me about Flutter when I started using it in 2018. Coming from React at the time, I much preferred the declarative approach. Since then I have been secretly hoping to find a good reason to make it declarative or at least find some clear counterexample to show that we shouldn't. First hurdle: The parameter should probably be Then we'd have to make sure that this approach generally works with all of our existing text input features. For example, TextInputFormatter would be one to check. There's probably a lot of other stuff to worry about. The first step should probably be to just try it out. Add textEditingValue and onChangedValue to EditableText and open a draft PR and see how it works. If we can get it to work with everything and the API is not too confusing being alongside of TextEditingController, then I think I would approve that π |
@justinmc good to hear. This was honestly my first line if thought, but I also wasn't sure if this change wouldn't be too big, and that's why I said that just adding the option for text won't be the perfect solution but would already solve the vast majority of cases we need. So IMO I agree with your plan, but if at the end you discover that it can't work, than I wouldn't scrap this, I would instead add the option to only set the text and get a solution that's not perfect, but still a lot more helpful than having nothing. |
I created this initial draft. I might scrape it though π
as I think @justinmc's proposal seems to cover all the editable texts. Will have to see how In my case, I modified how the local controller is being created, and on Screen.Recording.2025-05-02.at.11.56.35.AM.movExample app: import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
int counter = 0;
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Center(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
TextField(text: 'initial text $counter'),
FilledButton(
onPressed: () {
setState(() {
counter++;
});
},
child: Text("increment!"),
),
],
),
),
),
),
);
}
} |
Use case
The current
TextField
andTextFormField
APIs are almost reactive, but unfortunately we still need to deal with aTextEditingController
in most cases, which makes it still an imperative API.Most Flutter widgets come with a pure reactive API, such as the
Switch
API:This way the widget informs that a value changed, and we can independently control the widget state by setting it's
value
. This is excellent for integrating Flutter widgets with State Managers and other advanced logic that can react to changes on the fly.The
TextField
on the other hand can only do thisread/write
cycle with a very cumbersome and clunky API:What really bothers me is that the
TextField
API is almost there. It already has aonChanged
callback, it just lacks atext
parameter to set it's value.Proposal
I propose we add a
text
parameter in both theTextField
andTextFormField
, we need to also add anassert
to validate if bothtext
andcontroller
were defined at the same time, as this isn't a valid option, the developer needs to choose between one or the other (many widgets have this pattern already).If
controller
isnull
, then internally aTextEditingController
is created and disposed (I think this is already the case). WhendidUpdateWidget
shows that the new value oftext
is different than the old value oftext
, it just updates the controller.I understand that this won't cover all the possible cases that a
TextEditingController
covers, but still this small change will improve developer experience by a big margin, removing lots and lots of boilerplate from our apps.I think this also aligns with the current strategy of refining Flutter and improving developer experience.
The text was updated successfully, but these errors were encountered: