Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should I customize DropdownButtons and DropdownMenuItems in Flutter?

The default DropdownButton with DropdownMenuItems returns a light-grey dropdown. How should I customize the dropdown (e.g. background color, dropdown width)? I can change the style property in both DropdownButton and DropdownMenuItem, like this:

return new DropdownButton(       value: ...,       items: ...,       onChanged: ...,       style: new TextStyle(         color: Colors.white,       ),     ); 

but this doesn't change the dropdown's background color.

Should I copy DropdownMenu and extend it? Does Flutter plan to add customization for this widget in the near future?

like image 935
Mary Avatar asked Oct 02 '17 17:10

Mary


People also ask

How do I style a drop down menu in flutter?

To change the dropdown Button color in Flutter, simply wrap your DropdownButton widget inside the Container and provide the styling instructions inside the decoration property using the BoxDecoration widget.

How do I change the default value in DropdownButton flutter?

If we want to set an initial value to the dropdown, simply set the value to the value property. Its value must be included in the items list, otherwise, an error occurs.

How do you open dropdown dialog below DropdownButton like spinner in flutter?

Option 1: Set DropDown. dart selectedItemOffset to -40 in then DropDownItems will always opens below the DropdownButton .


1 Answers

You can accomplish this by wrapping the DropdownButton in a Theme widget and overriding the canvasColor.

screenshot

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;               });             },           ),         ),       ),     );   } } 
like image 170
Collin Jackson Avatar answered Oct 17 '22 08:10

Collin Jackson