Flutter中如何修改TextButton的边框颜色(仅改边框不改背景)
你可以通过在RoundedRectangleBorder中添加side参数来单独设置边框颜色,无需修改背景色。只需要给side赋值一个BorderSide对象指定目标颜色即可,TextButton默认背景为透明,只要不额外设置backgroundColor就不会改动背景。
修改后的代码示例:
TextButton( style: ButtonStyle( shape: MaterialStateProperty.all(RoundedRectangleBorder( borderRadius: BorderRadius.circular(0), // 添加此行设置边框颜色,替换为你需要的颜色值 side: const BorderSide(color: Colors.red) ))), onPressed: () { replacePoints(); }, child: Text( "${AppLocalizations.of(context)!.replacePoints} +", style: TextStyle(color: Colors.white), ))
如果需要根据按钮状态(如按下、悬停)切换边框颜色,可以用MaterialStateProperty.resolveWith实现:
TextButton( style: ButtonStyle( shape: MaterialStateProperty.resolveWith((states) { Color borderColor = Colors.blue; // 默认边框色 if (states.contains(MaterialState.pressed)) { borderColor = Colors.green; // 按钮按下时的边框色 } return RoundedRectangleBorder( borderRadius: BorderRadius.circular(0), side: BorderSide(color: borderColor), ); })), onPressed: () { replacePoints(); }, child: Text( "${AppLocalizations.of(context)!.replacePoints} +", style: TextStyle(color: Colors.white), ))
这种设置方式只会修改边框颜色,按钮背景会保持默认的透明状态。
内容的提问来源于stack exchange,提问作者editix
相关产品推荐
相关产品推荐

