简体   繁体   English

如何根据MaterialState更改flutter按钮边框颜色?

[英]How to change flutter button border color based on MaterialState?

Is there a way to change the flutter MateriaButton border color (or any other properties) based on the MaterialState?有没有办法根据 MaterialState 更改 flutter MateriaButton 边框颜色(或任何其他属性)?

ButtonStyle(
  backgroundColor: MaterialStateProperty.resolveWith<Color?>((states) => ...),
  overlayColor: MaterialStateProperty.resolveWith<Color?>((states) => ...),
  side: MaterialStateProperty.resolveWith<BorderSide?>((states) => {
    if (states.contains(MaterialState.pressed)) {
      return BorderSide(color: Colors.Blue);
    }
    return BorderSide(color: Colors.Red);
  }),
)

Trying to set the border in this way doesn't seem to have any effect on the side, but it stays always red.尝试以这种方式设置边框似乎对侧面没有任何影响,但它始终保持红色。 Both backgroundColor and overlayColor seem to change based on the state, but the side property doesn't. backgroundColor 和 overlayColor 似乎都会根据 state 发生变化,但 side 属性不会。 Is there a way to achieve this?有没有办法做到这一点?

Edit: Setting the side property this way seems to work after all.编辑:毕竟以这种方式设置副属性似乎可行。 Also the way proposed by @nagendra nag works well too. @nagendra nag 提出的方法也很有效。 The actual problem seems to be that the animation seems to be slow, so the change isn't visible unless the button is pressed for a short moment.实际问题似乎是 animation 似乎很慢,所以除非按下按钮片刻,否则变化是不可见的。

Try this尝试这个

shape: MaterialStateProperty.resolveWith(
  (states) {
    if (states.contains(MaterialState.focused)) {
      return const RoundedRectangleBorder(
        side: BorderSide(color: Colors.red),
      );
    }
    if (states.contains(MaterialState.pressed)) {
      return const RoundedRectangleBorder(
        side: BorderSide(color: Colors.green),
      );
    }
    if (states.contains(MaterialState.hovered)) {
      return const RoundedRectangleBorder(
        side: BorderSide(color: Colors.blue),
      );
    }
  },
),
),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM