Want to use radio buttons inside popup menus? The problem is that the PopupMenuButton is maintaining the popup dialog as private state (it even pushes a new route onto the Navigator stack). Calling setState won’t rebuild the items. You can use an AnimatedBuilder and a ValueNotifier to get around this.
Here’s an example of a working radio button list inside a popup:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; void main() { runApp(new MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return new MaterialApp( title: 'Flutter Demo', home: new MyHomePage(), ); } } class MyHomePage extends StatefulWidget { State createState() => new MyHomePageState(); } enum Fruit { apple, banana, } class MyHomePageState extends State<MyHomePage> { ValueNotifier<Fruit> _selectedItem = new ValueNotifier<Fruit>(Fruit.apple); @override Widget build(BuildContext context) { return new Scaffold( body: new Center( child: new PopupMenuButton<Fruit>( itemBuilder: (BuildContext context) { return new List<PopupMenuEntry<Fruit>>.generate( Fruit.values.length, (int index) { return new PopupMenuItem( value: Fruit.values[index], child: new AnimatedBuilder( child: new Text(Fruit.values[index].toString()), animation: _selectedItem, builder: (BuildContext context, Widget child) { return new RadioListTile<Fruit>( value: Fruit.values[index], groupValue: _selectedItem.value, title: child, onChanged: (Fruit value) { _selectedItem.value = value; }, ); }, ), ); }, ); }, ), ), ); } } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.