更改文本颜色 onTap,Flutter
Changing Text Color onTap, Flutter
我是一名 android 开发人员,刚接触 Flutter,我正在尝试创建 3 个灰色文本小部件,当用户单击一个时,被单击的小部件变为蓝色,而其他小部件保持灰色。这是我目前拥有的代码
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: const <Widget>[
InkWell(
child: Text('Click 3',
style: TextStyle(
color: Colors.indigo,
fontSize: 15.0,
fontWeight: FontWeight.bold,
)),
onTap: () => Colors.blue,
),
InkWell(
child: Text('Click 2',
style: TextStyle(
color: Colors.indigo,
fontSize: 15.0,
fontWeight: FontWeight.bold,
)),
onTap: () => Colors.blue,
),
InkWell(
child: Text('Click 3',
style: TextStyle(
color: Colors.indigo,
fontSize: 15.0,
fontWeight: FontWeight.bold,
)),
onTap: () => Colors.blue,
),
],
),
此外,无论我在 onTap() 中输入什么,我都会得到这样的错误
invalid constant value
您应该为每个按钮的颜色创建三个单独的变量,然后删除行中的 const
关键字,
Color firstColor = Colors.indigo;
Color secondColor = Colors.indigo;
Color thirdColor = Colors.indigo;
...
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[ /// remove const keyword
InkWell(
child: Text('Click 3',
style: TextStyle(
color: firstColor,
fontSize: 15.0,
fontWeight: FontWeight.bold,
),
),
onTap: (){
setState((){
firstColor=Colors.blue;
});
},
),
InkWell(
child: Text('Click 2',
style: TextStyle(
color: secondColor,
fontSize: 15.0,
fontWeight: FontWeight.bold,
),
),
onTap: (){
setState((){
secondColor=Colors.blue;
});
},
),
InkWell(
child: Text('Click 3',
style: TextStyle(
color: thirdButton,
fontSize: 15.0,
fontWeight: FontWeight.bold,
),
),
onTap: (){
setState((){
thirdColor=Colors.blue;
});
},
),
],
),
我是一名 android 开发人员,刚接触 Flutter,我正在尝试创建 3 个灰色文本小部件,当用户单击一个时,被单击的小部件变为蓝色,而其他小部件保持灰色。这是我目前拥有的代码
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: const <Widget>[
InkWell(
child: Text('Click 3',
style: TextStyle(
color: Colors.indigo,
fontSize: 15.0,
fontWeight: FontWeight.bold,
)),
onTap: () => Colors.blue,
),
InkWell(
child: Text('Click 2',
style: TextStyle(
color: Colors.indigo,
fontSize: 15.0,
fontWeight: FontWeight.bold,
)),
onTap: () => Colors.blue,
),
InkWell(
child: Text('Click 3',
style: TextStyle(
color: Colors.indigo,
fontSize: 15.0,
fontWeight: FontWeight.bold,
)),
onTap: () => Colors.blue,
),
],
),
此外,无论我在 onTap() 中输入什么,我都会得到这样的错误
invalid constant value
您应该为每个按钮的颜色创建三个单独的变量,然后删除行中的 const
关键字,
Color firstColor = Colors.indigo;
Color secondColor = Colors.indigo;
Color thirdColor = Colors.indigo;
...
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[ /// remove const keyword
InkWell(
child: Text('Click 3',
style: TextStyle(
color: firstColor,
fontSize: 15.0,
fontWeight: FontWeight.bold,
),
),
onTap: (){
setState((){
firstColor=Colors.blue;
});
},
),
InkWell(
child: Text('Click 2',
style: TextStyle(
color: secondColor,
fontSize: 15.0,
fontWeight: FontWeight.bold,
),
),
onTap: (){
setState((){
secondColor=Colors.blue;
});
},
),
InkWell(
child: Text('Click 3',
style: TextStyle(
color: thirdButton,
fontSize: 15.0,
fontWeight: FontWeight.bold,
),
),
onTap: (){
setState((){
thirdColor=Colors.blue;
});
},
),
],
),