Flutter中TextButton设置字体加粗报错,如何通过styleFrom实现?
你遇到的错误是因为TextButton.styleFrom()方法并没有提供直接的fontWeight参数,所以添加这个参数会触发参数不匹配的报错。
要实现按钮文本加粗,有两种正确的方式:
方式一:直接在Text组件中设置
把字体加粗的样式直接写在按钮的Text子组件里,这是最直观的做法:
TextButton( onPressed: () {}, child: Text( "Don't have account?", style: TextStyle(fontWeight: FontWeight.bold), ), style: TextButton.styleFrom( foregroundColor: Colors.black, padding: EdgeInsets.symmetric(horizontal: 16, vertical: 16), ), ),
方式二:通过styleFrom的textStyle参数设置
如果一定要通过styleFrom()来统一控制文本样式,可以使用textStyle参数传入TextStyle对象:
TextButton( onPressed: () {}, child: Text("Don't have account?"), style: TextButton.styleFrom( foregroundColor: Colors.black, textStyle: TextStyle(fontWeight: FontWeight.bold), padding: EdgeInsets.symmetric(horizontal: 16, vertical: 16), ), ),
内容的提问来源于stack exchange,提问作者umarkaa
相关产品推荐
相关产品推荐

