Flutter代码中padding与color未定义问题技术求助
问题根源
- 新版Flutter的
TextButton已移除直接的padding和color参数,这类样式需通过style属性配置 - 代码里的
color变量未定义,没有指定具体颜色值 - 代码存在语法不完整问题(
builder: (co未写完)
修正后的完整代码
Widget build(BuildContext context) { Size size = MediaQuery.of(context).size; return Container( margin: const EdgeInsets.symmetric(vertical: 10), width: size.width * 0.8, child: ClipRRect( borderRadius: BorderRadius.circular(29), child: TextButton( // 通过style配置内边距和背景色 style: TextButton.styleFrom( padding: const EdgeInsets.symmetric(vertical: 20, horizontal: 40), backgroundColor: Colors.blue, // 替换成你需要的颜色 ), onPressed: () { Navigator.pushReplacement( context, MaterialPageRoute( builder: (context) { // 补全语法 return const HomePage(); }, ), ); }, child: const Text("按钮文本"), // TextButton必须包含子组件,通常是Text ), ), ); }
关键修改点
- 把原来的
padding和color参数移到TextButton.styleFrom()方法中,背景色改用backgroundColor指定 - 将
Colors.blue替换为你实际需要的颜色(比如主题色Theme.of(context).primaryColor) - 补全
MaterialPageRoute的builder参数,把(co改成(context) - 给
TextButton添加child属性,这是TextButton的必填项
内容的提问来源于stack exchange,提问作者Zero Two
相关产品推荐
相关产品推荐

