Flutter中点击CustomElevatedButton无法弹出AlertDialog问题求助
问题排查:点击按钮无响应,无法触发AlertDialog弹窗
问题核心
点击LoginPage底部绑定了sendCodeToPhone方法的CustomElevatedButton时,完全没有响应,既不执行手机号校验逻辑,也无法触发showAlertDialog弹窗。
原因定位
问题出在CustomElevatedButton组件的实现中:
组件虽然接收了onPressed参数,但内部嵌套的ElevatedButton硬编码了空回调(){}, 并未调用传入的onPressed方法。也就是说,点击按钮时实际执行的是空函数,自然不会触发后续的手机号校验和弹窗逻辑。
修复方案
修改CustomElevatedButton,将内部ElevatedButton的onPressed替换为组件接收的this.onPressed参数。
修复后的CustomElevatedButton代码
class CustomElevatedButton extends StatelessWidget { final double? buttonWidth; final VoidCallback onPressed; final String text; const CustomElevatedButton( {Key? key, required this.text, this.buttonWidth, required this.onPressed}) : super(key : key); @override Widget build(BuildContext context) { return SizedBox( height: 42, width: buttonWidth ?? MediaQuery.of(context).size.width - 100, child: ElevatedButton( // 替换为传入的onPressed参数 onPressed: onPressed, style: ElevatedButton.styleFrom( backgroundColor: Coloors.greenDark, foregroundColor: Coloors.backgroundDark, splashFactory: NoSplash.splashFactory, elevation: 0, shadowColor: Colors.transparent, ), child: Text(text)), ); } }
验证建议
修复后,分别测试以下场景确认功能正常:
- 手机号输入框为空,点击按钮,弹出"Please enter your phone number"提示弹窗
- 输入长度小于9位的手机号,弹出长度过短的提示弹窗
- 输入长度大于10位的手机号,弹出长度过长的提示弹窗
内容的提问来源于stack exchange,提问作者Ahsan Raza
相关产品推荐
相关产品推荐

