Flutter-自定义切换按钮选择

索瓦尔德·奥拉夫森

我正在构建一排切换按钮(打开/关闭选择器),该按钮基本上是一排容器,每个容器都是一个类别,可以单击。

问:如何选择一个类别,而其他所有类别都被取消选择呢?

这是我构建的类别小部件:

Widget header(){
  return Column(
    mainAxisSize: MainAxisSize.min,
    children: <Widget>[
      Container(
        padding: EdgeInsets.only(top: 4.0, left: 0.0, right: 0.0, bottom: 6.0),
        child: Container(
          child: Center(
            child: Column(
              children: <Widget>[
                SizedBox(height: 4.0,),
                Container(
                  margin: EdgeInsets.only(left: 10.0, right: 10.0),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                    children: <Widget>[
                      InkWell(
                        splashColor: Colors.blue[100],
                        onTap: (){},
                        child: Container(
                          padding: EdgeInsets.symmetric(vertical: 10.0, horizontal: 12.0),
                          decoration: BoxDecoration(
                            color: Colors.blueAccent[100],
                            borderRadius: BorderRadius.all(Radius.circular(48.0)),
                          ),
                          child: Text('All',
                          style: TextStyle(color: Colors.white, fontSize: 10.0, fontWeight: FontWeight.w500),),
                        ),
                      ),
                      SizedBox(width: 2.0,),
                      InkWell(
                        splashColor: Colors.blue[100],
                        onTap: (){},                        
                        child: Container(
                          padding: EdgeInsets.symmetric(vertical: 10.0, horizontal: 12.0),
                          decoration: BoxDecoration(
                            color: Colors.grey[300],
                            borderRadius: BorderRadius.all(Radius.circular(48.0)),
                          ),
                          child: Text('category 1',
                          style: TextStyle(color: Colors.grey[900], fontSize: 10.0, fontWeight: FontWeight.w500),),
                        ),
                      ),
                      SizedBox(width: 2.0,),
                      InkWell(
                        splashColor: Colors.blue[100],
                        onTap: (){},                        
                        child: Container(
                          padding: EdgeInsets.symmetric(vertical: 10.0, horizontal: 12.0),
                          decoration: BoxDecoration(
                            color: Colors.grey[300],
                            borderRadius: BorderRadius.all(Radius.circular(48.0)),
                          ),
                          child: Text('category 2',
                          style: TextStyle(color: Colors.grey[900], fontSize: 10.0, fontWeight: FontWeight.w500),),
                        ),
                      ),
                      SizedBox(width: 2.0,),
                      InkWell(
                        splashColor: Colors.blue[100],
                        onTap: (){},                        
                        child: Container(
                          padding: EdgeInsets.symmetric(vertical: 10.0, horizontal: 12.0),
                          decoration: BoxDecoration(
                            color: Colors.grey[300],
                            borderRadius: BorderRadius.all(Radius.circular(48.0)),
                          ),
                          child: Text('category 3',
                          style: TextStyle(color: Colors.grey[900], fontSize: 10.0, fontWeight: FontWeight.w500),),
                        ),
                      ),
                      SizedBox(width: 2.0,),
                      InkWell(
                        splashColor: Colors.blue[100],
                        onTap: (){},                        
                        child: Container(
                          padding: EdgeInsets.symmetric(vertical: 10.0, horizontal: 12.0),
                          decoration: BoxDecoration(
                            color: Colors.grey[300],
                            borderRadius: BorderRadius.all(Radius.circular(48.0)),
                          ),
                          child: Text('category 4',
                          style: TextStyle(color: Colors.grey[900], fontSize: 10.0, fontWeight: FontWeight.w500),),
                        ),
                      ),
                    ],
                  ),
                ),
                SizedBox(height: 6.0,)
              ],
            ),
          ),
        ),
      ),
    ],
  );
}`
专业版

最好的方法是用来ListView.builder构建项目并保存选定的索引,但您也可以修改现有代码以将选定的项目保存在列表中,并检查该项目是否在列表中,然后再添加选择格式并刷新页面。您可以如下所示实现它,

class MyApp extends StatefulWidget {
  @override
  MyAppState createState() => MyAppState();
}

class MyAppState extends State<MyApp> {
  List<String> selectedCategory = new List<String>();
  String all = 'All';
  String category1 = 'category 1';
  String category2 = 'category 2';
  String category3 = 'category 3';
  String category4 = 'category 4';

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
            appBar: AppBar(
              title: Text('Test'),
            ),
            body: Container(
                padding: const EdgeInsets.all(20.0),
                child: header()
            )));
  }

  Widget header(){
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: <Widget>[
        Container(
          padding: EdgeInsets.only(top: 4.0, left: 0.0, right: 0.0, bottom: 6.0),
          child: Container(
            child: Center(
              child: Column(
                children: <Widget>[
                  SizedBox(height: 4.0,),
                  Container(
                    margin: EdgeInsets.only(left: 10.0, right: 10.0),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                      children: <Widget>[
                        InkWell(
                          splashColor: Colors.blue[100],
                          onTap: (){
                            selectedCategory.add(all);
                            selectedCategory.add(category1);
                            selectedCategory.add(category2);
                            selectedCategory.add(category3);
                            selectedCategory.add(category4);
                            setState(() {});
                          },
                          child: Container(
                            padding: EdgeInsets.symmetric(vertical: 10.0, horizontal: 12.0),
                            decoration: BoxDecoration(
                              color: selectedCategory.contains(all) ? Colors.blueAccent[100] : Colors.grey[500],
                              borderRadius: BorderRadius.all(Radius.circular(48.0)),
                            ),
                            child: Text('All',
                              style: TextStyle(color: Colors.white, fontSize: 10.0, fontWeight: FontWeight.w500),),
                          ),
                        ),
                        SizedBox(width: 2.0,),
                        InkWell(
                          splashColor: Colors.blue[100],
                          onTap: (){
                            selectedCategory = new List<String>();
                            selectedCategory.add(category1);
                            setState(() {});
                          },
                          child: Container(
                            padding: EdgeInsets.symmetric(vertical: 10.0, horizontal: 12.0),
                            decoration: BoxDecoration(
                              color: selectedCategory.contains(category1) ? Colors.blue[100] : Colors.grey[300],
                              borderRadius: BorderRadius.all(Radius.circular(48.0)),
                            ),
                            child: Text('category 1',
                              style: TextStyle(color: Colors.grey[900], fontSize: 10.0, fontWeight: FontWeight.w500),),
                          ),
                        ),
                        SizedBox(width: 2.0,),
                        InkWell(
                          splashColor: Colors.blue[100],
                          onTap: (){
                            selectedCategory = new List<String>();
                            selectedCategory.add(category2);
                            setState(() {});
                          },
                          child: Container(
                            padding: EdgeInsets.symmetric(vertical: 10.0, horizontal: 12.0),
                            decoration: BoxDecoration(
                              color: selectedCategory.contains(category2) ? Colors.blue[100] : Colors.grey[300],
                              borderRadius: BorderRadius.all(Radius.circular(48.0)),
                            ),
                            child: Text('category 2',
                              style: TextStyle(color: Colors.grey[900], fontSize: 10.0, fontWeight: FontWeight.w500),),
                          ),
                        ),
                        SizedBox(width: 2.0,),
                        InkWell(
                          splashColor: Colors.blue[100],
                          onTap: (){
                            selectedCategory = new List<String>();
                            selectedCategory.add(category3);
                            setState(() {});
                          },
                          child: Container(
                            padding: EdgeInsets.symmetric(vertical: 10.0, horizontal: 12.0),
                            decoration: BoxDecoration(
                              color: selectedCategory.contains(category3) ? Colors.blue[100] : Colors.grey[300],
                              borderRadius: BorderRadius.all(Radius.circular(48.0)),
                            ),
                            child: Text('category 3',
                              style: TextStyle(color: Colors.grey[900], fontSize: 10.0, fontWeight: FontWeight.w500),),
                          ),
                        ),
                        SizedBox(width: 2.0,),
                        InkWell(
                          splashColor: Colors.blue[100],
                          onTap: (){
                            selectedCategory = new List<String>();
                            selectedCategory.add(category4);
                            setState(() {});
                          },
                          child: Container(
                            padding: EdgeInsets.symmetric(vertical: 10.0, horizontal: 12.0),
                            decoration: BoxDecoration(
                              color: selectedCategory.contains(category4) ? Colors.blue[100] : Colors.grey[300],
                              borderRadius: BorderRadius.all(Radius.circular(48.0)),
                            ),
                            child: Text('category 4',
                              style: TextStyle(color: Colors.grey[900], fontSize: 10.0, fontWeight: FontWeight.w500),),
                          ),
                        ),
                      ],
                    ),
                  ),
                  SizedBox(height: 6.0,)
                ],
              ),
            ),
          ),
        ),
      ],
    );
  }
}

演示

希望这可以帮助。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在Access中制作自定义切换按钮

来自分类Dev

ReactJS中的自定义图像切换按钮

来自分类Dev

FooTable自定义切换按钮

来自分类Dev

React Native 自定义切换按钮组件

来自分类Dev

带动画的自定义圆形切换按钮

来自分类Dev

Flutter-如何使用“切换”按钮启用TextFormField

来自分类Dev

如何在Flutter中创建切换按钮?

来自分类Dev

Flutter中的自定义按钮

来自分类Dev

Flutter-自定义按钮上的InkResponse

来自分类Dev

如何在Flutter中制作自定义按钮形状

来自分类Dev

如何使用三个选项自定义切换按钮

来自分类Dev

在Lotus Notes表单上的“操作”按钮上切换自定义图标

来自分类Dev

在Outlook加载项中获取自定义的切换按钮

来自分类Dev

SWRevealViewController切换开关不适用于自定义导航栏按钮

来自分类Dev

在Lotus Notes表单上的“操作”按钮上切换自定义图标

来自分类Dev

如何自定义带有三个选项的android的切换按钮

来自分类Dev

引导程序自定义单选按钮切换隐藏的输入

来自分类Dev

在自定义列表视图中切换 ImageView 按钮

来自分类Dev

带有 ContentControl 的 WPF 自定义切换按钮

来自分类Dev

动作栏抽屉切换自定义图标

来自分类Dev

在AngularJS中切换自定义排序图标

来自分类Dev

Polylang自定义语言切换器

来自分类Dev

动作栏抽屉切换自定义图标

来自分类Dev

自定义分段控件不切换视图

来自分类Dev

jQuery 切换自定义动画

来自分类Dev

在自定义 UITableViewCell 中设置切换目标

来自分类Dev

自定义CheckBox不会在选中/取消选中时切换按钮图像(Android)

来自分类Dev

无法删除自定义插件详细信息页面上不需要的切换按钮

来自分类Dev

在Flutter中按切换开关上的小部件

Related 相关文章

热门标签

归档