Flutter 3.10启用Material3后无法修改按钮形状问题求助
你的代码问题出在FilledButtonTheme.of(context).style?.copyWith这部分——默认的Material3主题中,FilledButtonTheme.style是null,调用?.copyWith后返回的还是null,等于没有给按钮设置自定义样式,所以按钮会使用Material3内置的默认圆角样式。
要实现方形(或自定义形状)按钮,直接给FilledButton设置style即可,推荐用更简洁的styleFrom方法:
FilledButton( style: FilledButton.styleFrom( shape: const RoundedRectangleBorder( borderRadius: BorderRadius.zero, // 方形边框 ), ), onPressed: () {}, child: const Text("Click"), )
如果你需要更灵活的样式配置,也可以用ButtonStyle:
FilledButton( style: ButtonStyle( shape: MaterialStateProperty.all( const RoundedRectangleBorder( borderRadius: BorderRadius.zero, ), ), ), onPressed: () {}, child: const Text("Click"), )
另外你提到“即使不为按钮添加shape属性,结果也和截图一致”——这是Material3的默认设计,FilledButton默认就带有圆角,和Material2的默认样式不同。
内容的提问来源于stack exchange,提问作者Kamran Akbary
相关产品推荐
相关产品推荐

