Flutter中设置TextButton颜色遇'color'参数未定义错误,求解决
问题原因
TextButton组件本身没有color这个命名参数,直接传入该参数会触发"未定义命名参数'color'"的报错。
修改后的代码
child: TextButton( style: TextButton.styleFrom( backgroundColor: Colors.black, // 设置按钮背景色 ), child: const Text( 'Dark', style: TextStyle(color: Colors.white), ), onPressed: () { setState(() { darkMode = true; }); }, ),
补充说明
在Flutter中,TextButton的样式统一通过style参数配置,TextButton.styleFrom()是官方提供的快捷样式配置方法,你可以在这里设置背景色、文字颜色、内边距、圆角等各类样式属性,替代旧版本中零散的样式参数写法。
内容的提问来源于stack exchange,提问作者Constant-Law
相关产品推荐
相关产品推荐

