Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add decoration DropdownButton in Flutter

I have a dropdown button as you can see below.

  child: DropdownButton<String>(
                              value: dropDownValue,
                              icon: Icon(Icons.keyboard_arrow_down),
                              iconSize: 15,
                              elevation: 16,
                              style: TextStyle(color: Colors.grey),
                              underline: Container(
                                decoration: ShapeDecoration(
                                  shape: RoundedRectangleBorder(
                                    side: BorderSide(width: 1.0, style: BorderStyle.solid),
                                    borderRadius: BorderRadius.all(Radius.circular(5.0)),
                                  ),
                                ),
                            ),
                              onChanged: (String newValue) {
                                setState(() {
                                  dropDownValue = newValue;
                                });
                              },
                              items: [dropDownValue,...snapshot.data.data]
                                  .map<DropdownMenuItem<String>>((String value) {
                                return DropdownMenuItem<String>(
                                  value: value,
                                  child: Text(value.name),
                                );
                              }).toList(),
                            ),

I want to shape it like in the image by using decoration in Container, but i can't shape it the way i want enter image description here

But right now this is the image I have. How do I add an edge to my dropdown button? Is there a known way for this?

enter image description here

like image 355
kimSoo Avatar asked Sep 22 '20 07:09

kimSoo


People also ask

How do you add a drop down border in flutter?

To add a border to the dropdown button, if you use the DropdownButton widget, replace it with the DropdownButtonFormField. Inside the DropdownButtonFormField widget, you can specify the decoration property and then use the InputDecoration and OutlineInputBorder widget.

How do I set dropdown size in flutter?

Dropdown menu always open below the button and you can edit its position by using the offset parameter. You can make the menu open above the button by setting showAboveButton to true. You can edit (button, menu and menu items) height, width and decoration as you want. You can align hint or value and customize them.

How do I change the dropdown height in flutter?

You just need to leave the itemHeight with the null value. It will make the height of the DropdownButton with the menu item's intrinsic height. Save this answer.


2 Answers

You can copy paste run full code below
You can use DropdownButtonFormField with InputDecoration set fillColor and hintText
code snippet

DropdownButtonFormField(
              decoration: InputDecoration(
                  border: OutlineInputBorder(
                    borderRadius: const BorderRadius.all(
                      const Radius.circular(30.0),
                    ),
                  ),
                  filled: true,
                  hintStyle: TextStyle(color: Colors.grey[800]),
                  hintText: "Name",
                  fillColor: Colors.blue[200]),
              value: dropDownValue,

working demo

enter image description here

full code

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;
  String dropDownValue;
  List<String> cityList = [
    'Ajman',
    'Al Ain',
    'Dubai',
    'Fujairah',
    'Ras Al Khaimah',
    'Sharjah',
    'Umm Al Quwain'
  ];
  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  void initState() {
    //setFilters();
    super.initState();
  }

  setFilters() {
    setState(() {
      dropDownValue = cityList[2];
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            DropdownButtonFormField(
              decoration: InputDecoration(
                  border: OutlineInputBorder(
                    borderRadius: const BorderRadius.all(
                      const Radius.circular(30.0),
                    ),
                  ),
                  filled: true,
                  hintStyle: TextStyle(color: Colors.grey[800]),
                  hintText: "Name",
                  fillColor: Colors.blue[200]),
              value: dropDownValue,
              onChanged: (String Value) {
                setState(() {
                  dropDownValue = Value;
                });
              },
              items: cityList
                  .map((cityTitle) => DropdownMenuItem(
                      value: cityTitle, child: Text("$cityTitle")))
                  .toList(),
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}
like image 126
chunhunghan Avatar answered Sep 20 '22 21:09

chunhunghan


You can just wrap your DropdownButton widget into DecoratedBox :

return DecoratedBox(
      decoration: ShapeDecoration(
        color: Colors.cyan,
        shape: RoundedRectangleBorder(
          side: BorderSide(width: 1.0, style: BorderStyle.solid, color: Colors.cyan),
          borderRadius: BorderRadius.all(Radius.circular(25.0)),
        ),
      ),
      child: Padding(
        padding: const EdgeInsets.symmetric(horizontal: 40.0, vertical: 0.0),
        child: DropdownButton<String>(
          value: dropdownValue,
          icon: Icon(null),
          elevation: 16,
          onChanged: (String newValue) {
            setState(() {
              dropdownValue = newValue;
            });
          },
          underline: SizedBox(),
          items: <String>['City', 'Country', 'State']
              .map<DropdownMenuItem<String>>((String value) {
            return DropdownMenuItem<String>(
              value: value,
              child: Text(value),
            );
          }).toList(),
        ),
      ),
    );

Output :

enter image description here

like image 32
Lapa Ny Aina Tanjona Avatar answered Sep 16 '22 21:09

Lapa Ny Aina Tanjona