- 当为TextButton等button添加颜色时,使用ButtonStyle为其添加颜色
TextButton(
onPressed: () {},
child: Text('text'),
style:
ButtonStyle(backgroundColor:Colors.white),
);
The argument type 'Color' can't be assigned to the parameter type 'MaterialStateProperty<Color?>?'.
MaterialStateProperty.all() 方法是设置点击事件所有状态下的样式。
MaterialStateProperty.resolveWith() 可拦截分别设置不同状态下的样式。
TextButton( onPressed: () {}, child: Text('text'), style: ButtonStyle( //backgroundColor:MaterialStateProperty.all(Colors.white) backgroundColor: MaterialStateProperty.resolveWith( (states) { if (states.contains(MaterialState.focused) && !states.contains(MaterialState.pressed)) { //获取焦点时的颜色 return Colors.blue; } else if (states.contains(MaterialState.pressed)) { //按下时的颜色 return Colors.deepPurple; } //默认状态使用灰色 return Colors.grey; }, ), ), );