【问题标题】:Custom dropdown flutter with checkbox带有复选框的自定义下拉菜单
【发布时间】:2021-08-12 05:03:40
【问题描述】:

我是 Flutter 的新手。它是自定义下拉菜单,所有边框都带有圆角。并且还列出了带有复选框的下拉菜单。

我尝试了几个渲染对象的例子。但我不知道如何获得这个设计。

谁能帮我做这个设计?

例子

class _DropdowncustomState extends State<Dropdowncustom> {
  late String valueChoose;
  List listitem = [
    "Item 1","Item 1","Item 1","Item 1","Item 1"
  ];
  List _gender = [ 'Male','Female','Other'];
  String  ? _genderval;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Container(
          padding: EdgeInsets.only(left:10,right:10),

          decoration: BoxDecoration(
            border: Border.all(color:Colors.transparent),
            borderRadius: BorderRadius.all(Radius.circular(30)),
            color: Colors.white,
          ),
          child: DropdownButton(
            hint: Text('Gender'),
            dropdownColor: Colors.white,



            // dropdownColor: Colors.grey[200],
            value: _genderval,
             isExpanded: true,
            onChanged: (value)
            {
              setState(() {
                _genderval= value as String?;
              });
            },
            items: _gender.map((value)
            {
              return DropdownMenuItem(
                value: value,
                child: Text(value) ,);
            }



            ).toList(),

          ),

        ),


      ),
    );
  }
}

【问题讨论】:

  • 分享你的代码
  • 我已经更新了代码

标签: flutter user-interface checkbox dropdown checkboxlist


【解决方案1】:

点击链接Flutter Dropdown。

如果您不想搜索,只需设置 -> showSearchBox: false,

第二件事是你必须用颤振复选框替换图标。

在 DropDownSearch() 中使用这两个功能

 dropdownBuilder: _customDropDownExample,
 popupItemBuilder: _customPopupItemBuilderExample,

这些是 -

Widget _customDropDownExample(
  BuildContext context, UserModel? item, String itemDesignation) {
if (item == null) {
  return Container();
}

return Container(
  child: (item.avatar == null)
      ? ListTile(
          contentPadding: EdgeInsets.all(0),
          leading: CircleAvatar(),
          title: Text("No item selected"),
        )
      : ListTile(
          contentPadding: EdgeInsets.all(0),
          leading: CircleAvatar(
              // this does not work - throws 404 error
              // backgroundImage: NetworkImage(item.avatar ?? ''),
              ),
          title: Text(item.name),
          subtitle: Text(
            item.createdAt.toString(),
          ),
        ),
);

}

第二个是-

Widget _customPopupItemBuilderExample(
  BuildContext context, UserModel item, bool isSelected) {
return Container(
  margin: EdgeInsets.symmetric(horizontal: 8),
  decoration: !isSelected
      ? null
      : BoxDecoration(
          border: Border.all(color: Theme.of(context).primaryColor),
          borderRadius: BorderRadius.circular(5),
          color: Colors.white,
        ),
  child: ListTile(
    selected: isSelected,
    title: Text(item.name),
    subtitle: Text(item.createdAt.toString()),
    leading: CircleAvatar(
        // this does not work - throws 404 error
        // backgroundImage: NetworkImage(item.avatar ?? ''),
        ),
  ),
);

}

【讨论】:

  • 下拉菜单中的圆角呢?如何实现这一目标
【解决方案2】:

为了实现这一点,您可以将下拉菜单包装在卡片小部件中,并为框制作圆角,您可以像这样设置卡片的形状值:

Card (child: ... , 
shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.only(
              topLeft: Radius.circular(//whatever value you want), 
              topRight: Radius.circular(),
...
            ));

【讨论】:

  • 注意下拉列表...它仍然是相同的形状
  • 根据这个页面stackoverflow.com/questions/53150219/…你可以做这样的事情并像这样设置下拉菜单的这个属性: decoration: ShapeDecoration( shape: RoundedRectangleBorder( side: BorderSide(width: 1.0, style: BorderStyle) .solid),borderRadius: BorderRadius.all(Radius.circular(5.0)), ), ),
猜你喜欢
  • 1970-01-01
  • 2023-04-04
  • 2010-11-23
  • 1970-01-01
  • 2023-03-29
  • 2017-05-14
  • 1970-01-01
  • 2021-09-29
相关资源
最近更新 更多