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

9w11ddsr  于 2022-12-24  发布在  Flutter
关注(0)|答案(9)|浏览(250)

如何添加圆角矩形边框?下面的代码没有在屏幕上产生任何边框.

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>(...),
                           ),
                          ),
                   ),
2o7dmzc5

2o7dmzc51#

对于表单域变体,您可以使用OutlineInputBorderInputBorder,通常用于输入文本域:

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

表单字段执行此操作的方式可以复制并用于常规DropdownButton

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

7cjasjjr

7cjasjjr2#

您需要指定side:属性。默认情况下,该属性为BorderSide.none

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

uelo1irk3#

如果你想要的是这个...

那就给你

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,
          ),
        ),
      ),
    );
  }
}

这是礼貌
快乐编码...

vlurs2pr

vlurs2pr4#

Column(
    crossAxisAlignment : CrossAxisAlignment.start,
    children: <Widget> [
        Text('Gender:'),
        InputDecorator(
            decoration: InputDecoration(
                border: OutlineInputBorder(borderRadius: const BorderRadius.all(Radius.circular(4.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(() {
                        });
                    },
                ),
            ),
        ),
    ]
),
roejwanj

roejwanj5#

你可以试试这个

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: (_) {},
                                    ),
                                  ),
                                ),
enxuqcxy

enxuqcxy6#

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: (_) {},
                      ),
                      )
nwsw7zdq

nwsw7zdq7#

将其包裹在材质中并删除下拉框

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: ...
   
    ),
  )
pb3s4cty

pb3s4cty8#

如果你想让菜单本身变成圆角,只需在DropdownButton中添加borderRadius属性:

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

wfveoks09#

字符串选定值

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(),
      ),
    ),
  ),
);}

相关问题