Button 按下带有梯度的颤振按钮';s高亮颜色

Button 按下带有梯度的颤振按钮';s高亮颜色,button,flutter,dart,gradient,Button,Flutter,Dart,Gradient,我有一个带有渐变颜色的按钮,所以我想在按下时渐变会反转,然后当按下按钮时渐变颜色会恢复到开始时的状态 我有一个数组中的渐变颜色,因此可以很容易地交换,但不知道如何才能做到这一点。我以前用React Native做过这个效果,所以我想知道如何用Flatter做 目前,按下按钮时,它只使用了一种颜色与highlightColor,但这看起来太基本了 const gradientcolours = [ [Color(0xFF00000), Color(0xFFFFFFF)], [Color(

我有一个带有渐变颜色的按钮,所以我想在按下时渐变会反转,然后当按下按钮时渐变颜色会恢复到开始时的状态

我有一个数组中的渐变颜色,因此可以很容易地交换,但不知道如何才能做到这一点。我以前用React Native做过这个效果,所以我想知道如何用Flatter做

目前,按下按钮时,它只使用了一种颜色与
highlightColor
,但这看起来太基本了

 const gradientcolours = [
  [Color(0xFF00000), Color(0xFFFFFFF)],
  [Color(0xFFFFFFF), Color(0xFF00000)]
];

    return Container(
      padding: EdgeInsets.all(20),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(60),
      ),
      child: Container(
        padding: EdgeInsets.all(2),
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(60),
          gradient: LinearGradient(
              begin: Alignment.topCenter, end: Alignment.bottomCenter, colors: gradientcolours[0]),
        ),
        child: RawMaterialButton(
          splashColor: Color(0xFF030303),
          highlightColor: Color(0xFF030303),
          child: Text('Press'),
            child: Container(
              height: 40,
              width: 40,
            ),
          ),
          onPressed: onPressed,
          constraints: BoxConstraints.tightFor(
            width: 80,
            height: 80,
          ),
          shape: CircleBorder(),
        ),
      ),
    );

您可以尝试以下方法:

int index = 0;

/* ... */

Listener(
  child: Container(
    padding: EdgeInsets.all(2),
    decoration: BoxDecoration(
      borderRadius: BorderRadius.circular(60),
      gradient: LinearGradient(
          begin: Alignment.topCenter, end: Alignment.bottomCenter, colors: gradientcolours[index]),
    ),
    /* ... */
  ),
  onPointerDown: (_) {
    setState(() {
      index = 1;
    });
  },
  onPointerUp: (_) {
    setState(() {
      index = 0;
    });
  },
);

天哪,你真是个英雄。我甚至不知道这个
监听器
小部件。它工作得很好。