Flutter 3.3.8升级后TextButton报'child'参数未定义错误,求正确语法
升级Flutter到3.3.8后出现child参数未定义的报错,大概率是混淆了普通TextButton和TextButton.icon()的参数规则,以下是正确用法:
基础普通按钮
必填参数为onPressed(点击回调)和child(按钮内容,通常是Text组件):
TextButton( onPressed: () { // 执行点击逻辑 }, child: const Text('普通文本按钮'), )
自定义样式按钮
通过style参数配合TextButton.styleFrom快速配置样式:
TextButton( onPressed: () {}, style: TextButton.styleFrom( foregroundColor: Colors.deepPurple, // 文字/图标颜色 backgroundColor: Colors.grey.shade100, // 按钮背景色 padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 10), textStyle: const TextStyle(fontSize: 18, fontWeight: FontWeight.w500), ), child: const Text('带样式的按钮'), )
图标+文字组合按钮
使用TextButton.icon()构造函数,此时需要传入icon和label参数(注意这里没有child参数):
TextButton.icon( onPressed: () {}, icon: const Icon(Icons.share, size: 20), label: const Text('分享'), )
常见错误写法(触发参数未定义报错)
- 给普通
TextButton传入icon/label参数 - 给
TextButton.icon()传入child参数
这两种写法都会导致类似"命名参数未定义"的报错,需要根据按钮类型匹配对应参数。
内容的提问来源于stack exchange,提问作者Alifa Al Farizi
相关产品推荐
相关产品推荐

