简体   繁体   English

如何在 flutter 中为下拉按钮制作圆角边框?

[英]How to make rounded border for dropdownbutton in flutter?

How to Add Rounded Rectangle Border?如何添加圆角矩形边框? Below Code didn't result in any border on screen.下面的代码不会在屏幕上产生任何边框。

Container(margin: EdgeInsets.only(top: 10.0, right: 10.0, left: 10.0),
 width: double.infinity,
 // decoration: ShapeDecoration(
 //  shape: RoundedRectangleBorder(
 //   borderRadius:BorderRadius.all(Radius.circular(5.0)),
 //                             ),

 child: DropdownButtonHideUnderline(
  child: Container(
   margin: EdgeInsets.only(
    left: 10.0, right: 10.0),
     child: new DropdownButton<UserTest>(...),
                           ),
                          ),
                   ),

With the form field variant, you can use the OutlineInputBorder InputBorder , used normally for input text fields:对于表单字段变体,您可以使用OutlineInputBorder InputBorder ,通常用于输入文本字段:

DropdownButtonFormField(
  ...
  decoration: const InputDecoration(
    border: OutlineInputBorder(),
  ),
),

The way the form field does this can be replicated and used with the regular DropdownButton :表单字段执行此操作的方式可以复制并与常规DropdownButton

InputDecorator(
  decoration: const InputDecoration(border: OutlineInputBorder()),
  child: DropdownButtonHideUnderline(
    child: DropdownButton(
      ...
    ),
  ),
),

边框预览

You need to specify the side: property.您需要指定side:属性。 By default it is BorderSide.none .默认情况下它是BorderSide.none

      decoration: ShapeDecoration(
        shape: RoundedRectangleBorder(
          side: BorderSide(width: 1.0, style: BorderStyle.solid),
          borderRadius: BorderRadius.all(Radius.circular(5.0)),
        ),
      ),

if what you want is this如果你想要的是这个

在此处输入图片说明

then here you go那么你去

    import 'package:flutter/material.dart';

class RoundedBorderDropdown extends StatelessWidget {
  final List<String> _dropdownValues = [
    "One",
    "Two",
    "Three",
    "Four",
    "Five"
  ]; //The list of values we want on the dropdown

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Rounded Border Button in AppBar'),
      ),
      body: Center(
        child: Container(
          padding: EdgeInsets.symmetric(horizontal: 10.0),
          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(15.0),
            border: Border.all(
                color: Colors.red, style: BorderStyle.solid, width: 0.80),
          ),
          child: DropdownButton(
            items: _dropdownValues
                .map((value) => DropdownMenuItem(
                      child: Text(value),
                      value: value,
                    ))
                .toList(),
            onChanged: (String value) {},
            isExpanded: false,
            value: _dropdownValues.first,
          ),
        ),
      ),
    );
  }
}

that is courtesy inducesmile那是礼貌引诱

Happy Coding....快乐编码....

样本输出

Column(
    crossAxisAlignment : CrossAxisAlignment.start,
    children: <Widget> [
        Text('Gender:'),
        InputDecorator(
            decoration: InputDecoration(
                border: OutlineInputBorder(borderRadius: BorderRadius.circular(5.0)),
                contentPadding: EdgeInsets.all(10),
            ),
            child: DropdownButtonHideUnderline(
                child: DropdownButton<String>(
                    value: gender,
                    isDense: true,
                    isExpanded: true,
                    items: [
                        DropdownMenuItem(child: Text("Select Gender"), value: ""),
                        DropdownMenuItem(child: Text("Male"), value: "Male"),
                        DropdownMenuItem(child: Text("Female"), value: "Female"),
                    ],
                    onChanged: (newValue) {
                        setState(() {
                        });
                    },
                ),
            ),
        ),
    ]
),

You can try with this你可以试试这个

 Container(
                                  padding: EdgeInsets.symmetric(horizontal: 5),
                                  decoration: BoxDecoration(
                                    border: Border.all(color: Colors.blueGrey),
                                    borderRadius: BorderRadius.circular(5),
                                  ),
                                  child: DropdownButtonHideUnderline(
                                    child: DropdownButton<String>(
                                      borderRadius: BorderRadius.circular(12),
                                      isExpanded: true,
                                      items: <String>[
                                        '1 km',
                                        '2 km',
                                        '3 km',
                                        '4 km',
                                        '5 km'
                                      ].map((String value) {
                                        return DropdownMenuItem<String>(
                                          value: value,
                                          child: Text(value),
                                        );
                                      }).toList(),
                                      hint:
                                          ourtext("Select area", fontSize: 14),
                                      onChanged: (_) {},
                                    ),
                                  ),
                                ),
Container(width: 200.0,
          height: 50.0,
          decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(7.0),
          border: Border.all(color: Colors.blueGrey)),
                         child: DropdownButton<String>(
                         hint: Text("Messaging"),
                         items: <String>['Messaging', 'Chating', 'No Longer Interested', 'Document Request'].map((String value) {
                            return new DropdownMenuItem<String>(
                              value: value,
                              child: new Text(value),
                            );
                          }).toList(),
                          onChanged: (_) {},
                      ),
                      )

Wrap it in a Material and remove DropDown borders将其包裹在 Material 中并移除 DropDown 边框

Material(
    borderRadius: BorderRadius.circular(40),
    child: SizedBox(
      width: MediaQuery.of(context).size.width / 1.08,
      child: DropdownButton(
        style: style.copyWith(color: Colors.black),
        isExpanded: true,
        underline: DropdownButtonHideUnderline(child: Container()),
        value: value,
        items: ...
   
    ),
  )
Container(
    decoration: BoxDecoration(
      borderRadius: BorderRadius.circular(20.0),
      border: Border.all(
        color: HexColor('#C88A3D'),
        width: 3.0
      )
    ),
    child: Container(
      decoration: new BoxDecoration(borderRadius:
      BorderRadius.circular(20.0),
      color: Colors.white,),
    )
  ),

In case you want to make rounded corners for the menu itself, just add borderRadius properties in DropdownButton :如果您想为菜单本身制作圆角,只需在DropdownButton中添加borderRadius属性:

DropdownButton(
  borderRadius:BorderRadius.circular(12),
  items: ...
)

String selectedValue字符串选择值

String selected value;

Widget createRoundedDropDown (){
  return Container(
  decoration: BoxDecoration(
    color: Colors.white,
    border: Border.all(),
    borderRadius: BorderRadius.circular(20),
  ),
  child: Padding(
    padding: const EdgeInsets.all(5.0),
    child: DropdownButtonHideUnderline(
      child: DropdownButton<String>(
        borderRadius: BorderRadius.circular(10),
        style: TextStyle(color: Colors.black,fontSize: 14,),
        hint: Text("hint text"),
        value: selectedValue,
        isDense: true,
        onChanged: (newValue) {
          setState(() {
            selectedValue = newValue;
          });
        },
        items: listItems.map((document)  {
          return DropdownMenuItem<String>(
            value: document.name,
            child: FittedBox(fit:BoxFit.fitWidth,
                child: Text(document.name,
                       style:TextStyle(fontSize: 16))),
            );
        }).toList(),
      ),
    ),
  ),
);}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM