Reputation: 746
The value of drop down list is changing but the title of it isn't changing, the title only changes when I hot reloads the app.
List<String> _states = ['A', 'B', 'C', 'D'];
String _chosenValue;
void _showSettingsPanel() {
showModalBottomSheet(
context: context,
builder: (context) {
return Column(children: [
DropdownButton(
hint: Text("Select State"),
value: _chosenValue,
onChanged: (newValue) {
setState(() {
_chosenValue = newValue;
});
},
items: _states.map((valueItem) {
return DropdownMenuItem(
value: valueItem,
child: Text(valueItem),
);
}).toList(),
),
]);
},
);
}
Upvotes: 0
Views: 390
Reputation: 2192
Use StatefulBuilder for setting state in ModelSheet. Do as follows:
showModalBottomSheet(
context: context,
builder: (BuildContext context) {
return BottomSheet(
onClosing: () {},
builder: (BuildContext context) {
return StatefulBuilder(
builder: (BuildContext context, setState) {
return Column(
children: [
DropdownButton(
hint: Text("Select State"),
value: _chosenValue,
onChanged: (newValue) {
print(newValue);
setState(() {
_chosenValue = newValue.toString();
});
},
items: _states.map((valueItem) {
return DropdownMenuItem(
value: valueItem.toString(),
child: Text(valueItem),
);
}).toList(),
),
],
);
});
},
);
},
)
Upvotes: 3
Reputation: 916
SetState
won't work for bottomsheet so you can use ValueNotifier
to reflect changes in bottomsheet. Have a look on below code.
class MyHomePage extends StatefulWidget {
final String title;
MyHomePage({Key key, this.title}) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
List<String> _states = ['A', 'B', 'C', 'D'];
// String _chosenValue;
ValueNotifier<String> _chosenValue = ValueNotifier<String>("A");
@override
void dispose() {
_chosenValue.dispose();
super.dispose();
}
void _incrementCounter() {
showModalBottomSheet(
context: context,
builder: (context) {
return Column(
children: [
ValueListenableBuilder<String>(
valueListenable: _chosenValue,
builder: (BuildContext context, String value, Widget child) {
return DropdownButton(
hint: Text("Select State"),
value: _chosenValue.value,
onChanged: (newValue) {
setState(() {
_chosenValue.value = newValue;
});
},
items: _states.map((valueItem) {
return DropdownMenuItem(
value: valueItem,
child: Text(valueItem),
);
}).toList(),
);
},
),
],
);
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}
Upvotes: 1