简体   繁体   English

Flutter] 如何切换多个按钮的 colors

[英]Flutter] How can I toggle colors of multiple buttons

Let's suppose that I have 3 button( OutlinedButton , in my case).假设我有 3 个按钮(在我的情况下为OutlinedButton )。

What I want to make is to toggle the color of outlines of all buttons, not individually.我想做的是切换所有按钮的轮廓颜色,而不是单独切换。

For example If I press button 1, the outline color of button 1 goes yellow, and the color of outlines of button 2 and 3 goes white.例如,如果我按下按钮 1,按钮 1 的轮廓颜色变为黄色,按钮 2 和 3 的轮廓颜色变为白色。

What I tried is this:我尝试的是这样的:

    List<dynamic> colorlist_down = [Colors.amber,Colors.white,Colors.white];

...

return Row(
children:
[
OutlinedButton(
                        onPressed: (){
                          setState(() {
                            mode=0;
                            colorlist_down = [Colors.amber,Colors.white,Colors.white];
                          });
                        },
                    style: OutlinedButton.styleFrom(
                      primary: colorlist_down.elementAt(0),
                      side: BorderSide(color: colorlist_down.elementAt(0), width: 2),
                    ),

                        child: Text('Filter : Age',style: TextStyle(color: Colors.brown[900]))
                    ),
                      OutlinedButton(
                          onPressed: (){

                            setState(() {
                              mode=1;
                              colorlist_down = [Colors.white,Colors.amber,Colors.white];
                            });
                          },
                          style: OutlinedButton.styleFrom(
                              primary: colorlist_down.elementAt(1),
                            side: BorderSide(color: colorlist_down.elementAt(1), width: 2),

                          ),

                          child: Text('Filter : Gender',style: TextStyle(color: Colors.brown[900]))
                      ),
                      OutlinedButton(
                          onPressed: (){
                            setState(() {
                              mode=2;
                              colorlist_down = [Colors.white,Colors.white,Colors.amber];

                            });
                          },
                          style: OutlinedButton.styleFrom(
                              primary: colorlist_down.elementAt(2),
                            side: BorderSide(color: colorlist_down.elementAt(2), width: 2),

                          ),
                          child: Text('Filter : Location',style: TextStyle(color: Colors.brown[900]))
                      ),

]
);

Of course it did not work.当然它没有用。

I think that the style of buttons do not react to state change and somehow found that I should use MaterialStateProperty , but cannot get how to use it correctly for my purpose.我认为按钮的样式不会对 state 更改做出反应,并且以某种方式发现我应该使用MaterialStateProperty ,但无法了解如何正确使用它来达到我的目的。 Somebody please give me a help.有人请给我帮助。

-> You can create one object of Color class -> 您可以创建一个 object颜色class

-> Assign that object to all OutlinedButton with default color -> 将 object 分配给所有具有默认颜色的 OutlinedButton

-> While click on first button, as per your condition set color and refresh it using setState() -> 在单击第一个按钮时,根据您的条件设置颜色并使用 setState() 刷新它

For Each button to have its text and function, you may create a Label And Function class somewhere else.对于每个按钮都有其文本和 function,您可以在其他地方创建Label 和 Function ZA2F2ED4F298EBC2C454F46C745FZ。

class LabelAndFunction {
  String label;
  Function function;
  LabelAndFunction({required this.label, required this.function});
}

Then for you buttons create a list of TextLAbel and Functions然后为您的按钮创建一个 TextLAbel 和 Functions 列表

 List<LabelAndFunction> btnLbelsAndFunctions = [
    LabelAndFunction(
        label: "Filter : Age",
        function: () {
          // your function for filtering by age
        }),
    LabelAndFunction(
        label: "Filter: Gender",
        function: () {
          // your function for filtering by gender
        }),
    LabelAndFunction(
        label: "Filter: Location",
        function: () {
          // your function for filtering by location
        }),
  ];

Now to know which of the button is active, you can create an activeBtnIndex variable现在要知道哪个按钮处于活动状态,您可以创建一个activeBtnIndex变量

int activeBtnIndex = 0

Then Make a listview Builder然后制作一个列表视图生成器

ListView.builder(
                  itemCount: btnLbelsAndFunctions.length,
                  itemBuilder: (context, index) {
                    return OutlinedButton(
                        onPressed: () {
                          setState(() {
                            activeBtnIndex = index;
                          });
                          btnLbelsAndFunctions.elementAt(index).function();
                        },
                        style: OutlinedButton.styleFrom(
                          primary: activeBtnIndex == index
                              ? Colors.amber
                              : Colors.white,
                          side: BorderSide(
                            color: activeBtnIndex == index
                                ? Colors.amber
                                : Colors.white,
                          ),
                        ),
                        child: Text(btnLbelsAndFunctions.elementAt(index).label,
                            style: TextStyle(color: Colors.brown[900])));
                  })

I solved my problem by ? :我解决了我的问题? : ? : operator. ? :运营商。

p0_0 = true;
p0_1 = false;
p0_2 = false;

...

onPressed:()
{
p0_0 = false;
p0_1 = true;
p0_2 = false; 
setState(){
};

},
style: OutlinedButton.styleFrom( primary: colorlist_down.elementAt(2),side: BorderSide(color: p0_1? Colors.amberAccent:Colors.white, width: 2),

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

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