Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 更改颜色ListView.Builder onPressed颤振_Flutter - Fatal编程技术网

Flutter 更改颜色ListView.Builder onPressed颤振

Flutter 更改颜色ListView.Builder onPressed颤振,flutter,Flutter,我想换颜色。如果我选择值为“einamal”(true)的索引,它将为绿色,否则为红色(false) List _choices=['einmal'、'zweimal'、'dreimal'、'viemal']; 列表_hasBeenPressed=[true,false,false,false]; 小部件_buildChoice(){ 返回容器( 高度:MediaQuery.of(context).size.height/4, 子项:ListView.builder( itemCount:_ch

我想换颜色。如果我选择值为“einamal”(true)的索引,它将为绿色,否则为红色(false)

List _choices=['einmal'、'zweimal'、'dreimal'、'viemal'];
列表_hasBeenPressed=[true,false,false,false];
小部件_buildChoice(){
返回容器(
高度:MediaQuery.of(context).size.height/4,
子项:ListView.builder(
itemCount:_choices.length,
itemBuilder:(构建上下文,int索引)
{
返回上升按钮(
子项:新文本(_选项[索引]),
text颜色:_已按[索引]?颜色。白色:颜色。绿色,
颜色:_已按[索引]?颜色。绿色:颜色。白色,
按下:()=>
{
设置状态(){
如果(_选项[索引]=='einmal'){
_hasBeenPressed[索引]=!\u hasBeenPressed[索引];
}
})
},
形状:圆形矩形边框(
边界半径:边界半径。圆形(30.0)
),
);
}
),
);
}

您需要另一个变量来存储实际答案,在您的情况下

String answer = "einmal";
现在,您只需要一个
字符串
来存储用户选择的选项,而不是一个列表,如下所示

String selected = "";
onPressed: () => {
  setState(() {
    selected = _choices[index];
  })
},
现在,每当用户选择一个选项时,更新您选择的
,如下所示

String selected = "";
onPressed: () => {
  setState(() {
    selected = _choices[index];
  })
},
现在,用这个来获得正确的颜色

textColor: selected == _choices[index] ? Colors.white : Colors.green,
color: selected == _choices[index]
         ? (selected == answer ? Colors.green : Colors.red)
         : Colors.white,
完整代码

List<String> _choices = <String>['einmal', 'zweimal', 'dreimal', 'viemal'];
String answer = "einmal";

String selected = "";

Widget _buildChoice() {
  return Container(
    height: MediaQuery.of(context).size.height / 4,
    child: ListView.builder(
      itemCount: _choices.length,
      itemBuilder: (BuildContext context, int index) {
        return RaisedButton(
          child: new Text(_choices[index]),
          textColor:
              selected == _choices[index] ? Colors.white : Colors.green,
          color: selected == _choices[index]
              ? (selected == answer ? Colors.green : Colors.red)
              : Colors.white,
          onPressed: () => {
            setState(() {
              selected = _choices[index];
            })
          },
          shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(30.0)),
        );
      }),
  );
}
List _choices=['einmal','zweimal','dreimal','viemal'];
字符串answer=“einmal”;
所选字符串=”;
小部件_buildChoice(){
返回容器(
高度:MediaQuery.of(context).size.height/4,
子项:ListView.builder(
itemCount:_choices.length,
itemBuilder:(构建上下文,int索引){
返回上升按钮(
子项:新文本(_选项[索引]),
文本颜色:
选定==\u选项[索引]?颜色。白色:颜色。绿色,
颜色:选定==\u选项[索引]
?(所选==答案?颜色。绿色:颜色。红色)
:颜色。白色,
按下:()=>{
设置状态(){
所选=_选项[索引];
})
},
形状:圆形矩形边框(
边界半径:边界半径。圆形(30.0)),
);
}),
);
}


您面临什么问题。无法从您的问题中真正理解您的问题如果我选择索引按钮0,它将变为绿色,如果我选择除0以外的索引按钮,它将变为红色。请查看“实施截图”,红色将出现在哪里?非常感谢!嗨@TriWijiHastuti。很高兴能帮忙。如果它对你有帮助,请考虑接受答案: