StatefulBuilder (Widget of the Week)
Why Stack Overflow recommends it
Stack Overflow users link it when answering questions about calling setState inside dialogs, bottom sheets, checkboxes and dropdowns that refuse to update.
Recommended by 4 people across 3 different months, 2019 to 2023.
What it is
A 1 minute 25 second entry in the Flutter team's Widget of the Week series. It explains StatefulBuilder, a widget that lets you rebuild just part of a widget tree with its own setState without writing a full separate StatefulWidget.
What people said
Recommends Alex.F · 2019-06 · Stack Overflow · on “StatefulBuilder vs StatefulWidget”
StatefulBuilder [LINK] is actually a StatefulWidget [LINK] under the hood. So it's not that you would use one over the other. It's that StatefulBuilder is a specific implementation of StatefulWidget . So we extend a StatefulWidget any time we want to create a custom Widget that has an inner state. And we use a StatefulBuilder in a very specific case when we want to expose the setState of the StatefulBuilder in a build method of another Widget. The updated Flutter documentation has a good video [this video] on StatefulBuilder . In the example bellow we use StatefulBuilder to rebuild a Column containing 4 Radio widgets whenever the selected radio button changes: await showDialog<void>( context: con
Recommends Thusitha Deepal · 2022-05 · Stack Overflow · on “Checkbox refresh Flutter”
wrap in StatefulBuilder to reload only the checkbox.For more info watch this How to use statefulbuilder [this video]
Recommends Wiktor · 2022-05 · Stack Overflow · on “Opening a text field causes the parrent widget to rebuild”
It seems like a great usecase for `StatefulBuilder`. You can see quickly how does it work here [this video] . Let me know if it helped
Recommends MendelG · 2022-12 · Stack Overflow · on “Issue with statefulWidget unable to make desired changes”
Since your in a Dialog , for setState to work, you need to wrap it with a StatefulBuilder [LINK] . You haven't included your full code, so I'm using this example taken from the docs [LINK] : await showDialog<void>( context: context, builder: (BuildContext context) { int? selectedRadio = 0; return AlertDialog( content: StatefulBuilder( builder: (BuildContext context, StateSetter setState) { return Column( mainAxisSize: MainAxisSize.min, children: List<Widget>.generate(4, (int index) { return Radio<int>( value: index, groupValue: selectedRadio, onChanged: (int? value) { setState(() => selectedRadio = value); }, ); }), ); }, ), ); }, ); See also A [this video] by the Flutter team explaining Stateful
Cites Bihim · 2022-04 · Stack Overflow · on “Flutter DropDownButton using FutureBuilder value is not updating after selecting values from DropdownItems”
I have wrapped my DropdownButton with StatefulBuilder. This is the only way I could change the data of DropdownButton without recalling FutureBuilder. Demo code: StatefulBuilder(builder: (context, setState) { return DropdownButton<String>( borderRadius: BorderRadius.circular( 10, ), hint: Text( "Your Hint", ), value: selectedValue, //your selected value isExpanded: true, items: your_list.map((String value) { //your list here return DropdownMenuItem<String>( value: value, child: Text(value), ); }).toList(), onChanged: (value) { setState(() => selectedValue = value); //your selected value }, ); }), Learn more from: StatefulBuilder [this video]
Cites Harsh Sureja · 2023-04 · Stack Overflow · on “Flutter DropDownButton not displaying Selected Value”
Need to rebuild Dialog it required to wrap with StatefulBuilder . Here [this video] you can find more info about it. For example: await showDialog<void>( context: context, builder: (BuildContext context) { int? selectedRadio = 0; return AlertDialog( content: StatefulBuilder( builder: (BuildContext context, StateSetter setState) { return Column( mainAxisSize: MainAxisSize.min, children: List<Widget>.generate(4, (int index) { return Radio<int>( value: index, groupValue: selectedRadio, onChanged: (int? value) { setState(() => selectedRadio = value); }, ); }), ); }, ), ); }, );
Cites MendelG · 2023-08 · Stack Overflow · on “How to change the value of "CheckboxListTile" widget in "showModalBottomSheet" widget in flutter?”
Within a showModalSheet , if you call setState , it won't work. You need to use a StatefulBuilder [LINK] within the sheet instead. So wrap the contents of the modalSheet with a StatefulBuilder : ElevatedButton( onPressed: () { showModalBottomSheet( context: context, builder: (BuildContext context) { return StatefulBuilder( builder: (BuildContext context, StateSetter setState) { return SizedBox( height: 500, Here's a complete runnable snippet: import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'Material App', home: Scaffold( appBar: AppBar( title: Text('Material