Flutter报错:命名参数‘border’未定义,按钮边框设置求助
你遇到的问题核心是OutlinedButton没有border这个命名参数——你把输入框(比如TextField)的边框配置逻辑错误套用到了按钮上,Flutter里按钮的样式统一通过ButtonStyle来设置。
正确的做法是删除无效的border参数,在ButtonStyle里通过side配置边框样式,通过shape设置圆角:
修正后的完整代码
const ChooseOptionScreen({super.key}); @override Widget build(BuildContext context) { return Scaffold( body: Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ Container( alignment: Alignment.center, child: FractionallySizedBox( widthFactor: 0.4, child: OutlinedButton( style: ButtonStyle( backgroundColor: MaterialStateProperty.all(Colors.blue), foregroundColor: MaterialStateProperty.all(Colors.white), // 用这个设置文字颜色适配按钮状态更稳妥 side: MaterialStateProperty.all(const BorderSide(color: Colors.blue)), // 边框颜色、样式 shape: MaterialStateProperty.all(RoundedRectangleBorder( borderRadius: BorderRadius.circular(20.0), )), ), onPressed: () { Navigator.pushNamed(context, '/login'); }, child: const Text('Login'), ), ), ), const SizedBox(height: 10), Container( alignment: Alignment.center, child: FractionallySizedBox( widthFactor: 0.4, child: OutlinedButton( style: ButtonStyle( backgroundColor: MaterialStateProperty.all(Colors.green), foregroundColor: MaterialStateProperty.all(Colors.white), side: MaterialStateProperty.all(const BorderSide(color: Colors.green)), shape: MaterialStateProperty.all(RoundedRectangleBorder( borderRadius: BorderRadius.circular(20.0), )), ), onPressed: () { Navigator.pushNamed(context, '/register'); }, child: const Text('Register'), ), ), ), ], ), ); }
关键说明
side属性:用来定义边框的颜色、宽度、样式,对应你原本想实现的边框效果shape里的RoundedRectangleBorder:专门用于设置按钮的圆角,你之前用的OutlineInputBorder是给输入框设计的组件,不适用于按钮foregroundColor:替代textStyle里的color,能更好适配按钮的不同状态(比如按下、禁用)
内容的提问来源于stack exchange,提问作者Sigitas
相关产品推荐
相关产品推荐

