0

There are many answers on StackOverflow that explain how to draw a border around a widget. However, I am looking for something like TextFormField.

The normal DropdownButton has an underline attribute only but I am looking for something like the following design:

enter image description here

As you can see here, the dropdown list has a border and a title. I can remove the underline attribute from the DropdownButton widget but is there any custom widget that I can use in order to wrap the DropdownButton?

Hesam
  • 52,260
  • 74
  • 224
  • 365

2 Answers2

4

You can replicate this with PopupMenuButton or Wrap it under InputDecorator then hide underline with DropdownButtonHideUnderline

/// Flutter code sample for DropdownButton

// This sample shows a `DropdownButton` with a large arrow icon,
// purple text style, and bold purple underline, whose value is one of "One",
// "Two", "Free", or "Four".
//
// ![](https://flutter.github.io/assets-for-api-docs/assets/material/dropdown_button.png)

import 'package:flutter/material.dart';

void main() => runApp(const MyApp());

/// This is the main application widget.
class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  static const String _title = 'Flutter Code Sample';

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: _title,
      home: Scaffold(
        appBar: AppBar(title: const Text(_title)),
        body: const Center(
          child: MyStatefulWidget(),
        ),
      ),
    );
  }
}

/// This is the stateful widget that the main application instantiates.
class MyStatefulWidget extends StatefulWidget {
  const MyStatefulWidget({Key? key}) : super(key: key);

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

/// This is the private State class that goes with MyStatefulWidget.
class _MyStatefulWidgetState extends State<MyStatefulWidget> {
  String dropdownValue = 'One';

  @override
  Widget build(BuildContext context) {
    return InputDecorator(
          decoration: InputDecoration(
            contentPadding: EdgeInsets.symmetric(
                horizontal: 20.0, vertical: 15.0),
            labelText: 'Label',
            border:
                OutlineInputBorder(borderRadius: BorderRadius.circular(5.0)),
          ),
      
      child: DropdownButtonHideUnderline( child:DropdownButton<String>(
      value: dropdownValue,
      icon: const Icon(Icons.arrow_drop_down),
      iconSize: 24,
      elevation: 16,
      style: const TextStyle(color: Colors.deepPurple),
    
      onChanged: (String? newValue) {
        setState(() {
          dropdownValue = newValue!;
        });
      },
      items: <String>['One', 'Two', 'Free', 'Four']
          .map<DropdownMenuItem<String>>((String value) {
        return DropdownMenuItem<String>(
          value: value,
          child: Text(value),
        );
      }).toList(),
      ),  ),
    );
  }
}

enter image description here

flakerimi
  • 2,580
  • 3
  • 29
  • 49
  • You are awesome. What I exactly needed. It would be great if you put the link to the page you provided the screenshot from. But thank you anyways. – Hesam Mar 27 '21 at 00:33
  • 1
    Its just from Flutter Documentation API, I have use as codepen. this to be exact: https://api.flutter.dev/flutter/material/DropdownButton-class.html – flakerimi Mar 27 '21 at 00:40
-1

I didn't know there is another widget specifically created to be used in the form. DropdownButtonFormField is the widget and it doesn't need all the extra lines mentioned by @flakerimi.

This is my sample code for those who want to look at.

DropdownButtonFormField<String>(
      validator: (value) =>
          dropdownValue == null ? S.of(context).general_make_selection : null,
      autovalidateMode: AutovalidateMode.onUserInteraction,
      value: dropdownValue,
      decoration: InputDecoration(
        labelText: S.of(context).bill_obj_type,
        filled: true,
      ),
      icon: const Icon(Icons.arrow_drop_down),
      iconSize: 24,
      elevation: 16,
      isExpanded: true,
      style: Theme.of(context)
          .textTheme
          .subtitle1
          .copyWith(color: AppColors.neutral1),
      onChanged: (String newValue) {
        setState(() {
          dropdownValue = newValue;
          vm.objectionType = newValue;
        });
      },
      items: _getDropdownMenuItems(),
    );

enter image description here

Hesam
  • 52,260
  • 74
  • 224
  • 365