Flutter 在Flatter中从多个按钮更改一个按钮的颜色
我正在使用flatter开发一个问答应用程序。如果该人员单击跳过按钮,则该问题编号按钮应变为红色。我该怎么做 小部件:Flutter 在Flatter中从多个按钮更改一个按钮的颜色,flutter,dart,Flutter,Dart,我正在使用flatter开发一个问答应用程序。如果该人员单击跳过按钮,则该问题编号按钮应变为红色。我该怎么做 小部件: @override Widget build(BuildContext context) { // TODO: implement build return Scaffold( backgroundColor: Colors.blue, body: SingleChildScrollView( scroll
@override
Widget build(BuildContext context) {
// TODO: implement build
return Scaffold(
backgroundColor: Colors.blue,
body: SingleChildScrollView(
scrollDirection: Axis.vertical,
child: SizedBox(
height: 600,
child: Column(
children: <Widget>[
Expanded(
flex: 2,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Container(
width: 40,
padding: EdgeInsets.all(5),
child: RaisedButton(onPressed: () {
i = -1;
ChooseQuestion();
},
color: Colors.deepOrange,
textColor: Colors.white,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(
Radius.circular(10.0))),
child: Text("1",
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 20
),
),
),
),
Container(
width: 40,
padding: EdgeInsets.all(5),
child: RaisedButton(onPressed: () {
i = 0;
ChooseQuestion();
},
color: Colors.deepOrange,
textColor: Colors.white,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(
Radius.circular(10.0))),
child: Text('2',
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 20
),
),
),
),
Container(
width: 40,
padding: EdgeInsets.all(5),
child: RaisedButton(onPressed: () {
i = 1;
ChooseQuestion();
},
color: Colors.deepOrange,
textColor: Colors.white,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(
Radius.circular(10.0))),
child: Text('3',
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 20
),
),
),
),
],
);
),
Expanded(
flex: 3,
child: Center(
child: Container(
width: 400,
height: 400,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Container(
width: 250,
child: Material(
elevation: 5,
color: Colors.deepOrange,
borderRadius: BorderRadius.all(
Radius.circular(10.0)),
child: TextField(
enabled: false,
maxLines: 6,
decoration: InputDecoration(
fillColor: Colors.white,
filled: true,
hintText: questions[i]
),
style: TextStyle(
fontSize: 20,
color: Colors.black
),
),
),
)
],
),
),
)
),
Expanded(
flex: 3,
child: Center(
child: Container(
width: 400,
height: 400,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Container(
width: 250,
child: Material(
elevation: 2,
color: Colors.deepOrange,
borderRadius: BorderRadius.all(
Radius.circular(10.0)),
child: TextField(
controller: answercontroller,
maxLines: 3,
//enabled: false,
decoration: InputDecoration(
fillColor: Colors.white,
filled: true,
hintText: 'Answer'
),
style: TextStyle(
fontSize: 20,
color: Colors.black
),
),
),
),
]
)
)
)
),
Expanded(
flex: 1,
child: Align(
alignment: Alignment.topCenter,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
RaisedButton(onPressed: SkipQuestion,
color: Colors.deepOrange,
textColor: Colors.white,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(
Radius.circular(10.0))),
child: Text('Skip',
style: TextStyle(
fontSize: 20
),
),
),
RaisedButton(onPressed: NextQuestion,
color: Colors.deepOrange,
textColor: Colors.white,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(
Radius.circular(10.0))),
child: Text('Next',
style: TextStyle(
fontSize: 20
),
),
),
],
),
)
)
],
),
),
),
);
}
@覆盖
小部件构建(构建上下文){
//TODO:实现构建
返回脚手架(
背景颜色:Colors.blue,
正文:SingleChildScrollView(
滚动方向:轴垂直,
孩子:大小盒子(
身高:600,
子:列(
儿童:[
扩大(
弹性:2,
孩子:排(
mainAxisAlignment:mainAxisAlignment.space,
儿童:[
容器(
宽度:40,
填充:边缘设置。全部(5),
子项:升起按钮(按下时:(){
i=-1;
选择序列();
},
颜色:颜色。深橙色,
textColor:Colors.white,
形状:圆形矩形边框(
borderRadius:borderRadius.all(
圆半径(10.0)),
子项:文本(“1”,
textAlign:textAlign.center,
样式:TextStyle(
尺寸:20
),
),
),
),
容器(
宽度:40,
填充:边缘设置。全部(5),
子项:升起按钮(按下时:(){
i=0;
选择序列();
},
颜色:颜色。深橙色,
textColor:Colors.white,
形状:圆形矩形边框(
borderRadius:borderRadius.all(
圆半径(10.0)),
子:文本('2',
textAlign:textAlign.center,
样式:TextStyle(
尺寸:20
),
),
),
),
容器(
宽度:40,
填充:边缘设置。全部(5),
子项:升起按钮(按下时:(){
i=1;
选择序列();
},
颜色:颜色。深橙色,
textColor:Colors.white,
形状:圆形矩形边框(
borderRadius:borderRadius.all(
圆半径(10.0)),
子:文本('3',
textAlign:textAlign.center,
样式:TextStyle(
尺寸:20
),
),
),
),
],
);
),
扩大(
弹性:3,
儿童:中心(
子:容器(
宽度:400,
身高:400,
子:列(
mainAxisAlignment:mainAxisAlignment.space,
儿童:[
容器(
宽度:250,
儿童:材料(
标高:5,
颜色:颜色。深橙色,
borderRadius:borderRadius.all(
圆半径(10.0)),
孩子:TextField(
启用:false,
maxLines:6,
装饰:输入装饰(
fillColor:Colors.white,
是的,
hintText:问题[i]
),
样式:TextStyle(
尺寸:20,
颜色:颜色。黑色
),
),
),
)
],
),
),
)
),
扩大(
弹性:3,
儿童:中心(
子:容器(
宽度:400,
身高:400,
子:列(
mainAxisAlignment:mainAxisAlignment.space,
儿童:[
容器(
宽度:250,
儿童:材料(
标高:2,
颜色:颜色。深橙色,
borderRadius:borderRadius.all(
圆半径(10.0)),
孩子:TextField(
控制员:应答控制员,
maxLines:3,
//启用:false,
装饰:输入装饰(
fillColor:Colors.white,
是的,
hintText:“答案”
),
样式:TextStyle(
尺寸:20,
颜色:颜色。黑色
void SkipQuestion() {
setState(() {
if(i < (questions.length - 1)){
// ignore: unnecessary_statements
btncolor[(i+1).toString()] == Colors.red;
i++;
}
else{
Navigator.of(context).pushReplacement(MaterialPageRoute(
builder: (context) => resultpage(),
));
}
});
}
class _quizpageState extends State<quizpage> with SingleTickerProviderStateMixin {
int i = 0;
Map<String, Color> btncolor = {
"1" : Colors.deepOrangeAccent,
"2" : Colors.deepOrangeAccent,
"3" : Colors.deepOrangeAccent,
"4" : Colors.deepOrangeAccent,
"5" : Colors.deepOrangeAccent,
"6" : Colors.deepOrangeAccent,
"7" : Colors.deepOrangeAccent,
"8" : Colors.deepOrangeAccent,
"9" : Colors.deepOrangeAccent,
};
//The widget code is here which is displayed in the first code
}
RaisedButton(onPressed: SkipQuestion,
color: Colors.deepOrange,
textColor: Colors.white,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(
Radius.circular(10.0))),
child: Text('Skip',
style: TextStyle(
fontSize: 20
),
),
),
RaisedButton(onPressed: SkipQuestion,
//colorVariable you want to change the color when you rebuild the widget
color: btncolor[index_here],
textColor: Colors.white,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(
Radius.circular(10.0))),
child: Text('Skip',
style: TextStyle(
fontSize: 20
),
),
),