Want to customize DropdownButtons and DropdownMenuItems in Flutter? You can accomplish this by wrapping the DropdownButton in a Theme widget and overriding the canvasColor.
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 |
import 'package:flutter/material.dart'; void main() { runApp(new MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return new MaterialApp( home: new MyHomePage(), ); } } class MyHomePage extends StatefulWidget { State createState() => new MyHomePageState(); } class MyHomePageState extends State<MyHomePage> { int _value = 42; @override Widget build(BuildContext context) { return new Scaffold( body: new Center( child: new Theme( data: Theme.of(context).copyWith( canvasColor: Colors.blue.shade200, ), child: new DropdownButton( value: _value, items: <DropdownMenuItem<int>>[ new DropdownMenuItem( child: new Text('Foo'), value: 0, ), new DropdownMenuItem( child: new Text('Bar'), value: 42, ), ], onChanged: (int value) { setState(() { _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.