简体   繁体   English

Flutter - 按时更改文本不透明度

[英]Flutter - change text opacity on press

Basically I want to change the opacity of the text when it is clicked.基本上我想在单击时更改文本的不透明度。 Like when it is pressed, its opacity becomes 0.4 and once it is released, its opacity becomes 1.0.就像按下它一样,它的不透明度变为 0.4,一旦释放,它的不透明度变为 1.0。 Currently I'm trying to use AnimatedOpacity and I can only change the opacity on tap.目前我正在尝试使用AnimatedOpacity并且我只能在点击时更改不透明度。

double textOpacity = 1.0;
//some widgets
 AnimatedOpacity(
   duration: const Duration(milliseconds: 100),
   opacity: textOpacity,
   child: GestureDetector(
     child: Text(
       'Tap',
       style: TextStyle(
          color: CupertinoColors.systemBlue,
       ),
     ),
     onTap: () {
       setState(() => textOpacity = 0.4);
     }
  ),
),

I also tried我也试过

onForcePressStart: (details) =>
    setState(() => textOpacity = 0.4),
onForcePressEnd: (details) =>
    setState(() => textOpacity = 1.0),

but it didn't work.但它没有用。

Can any one help me with it?任何人都可以帮助我吗? Thanks!谢谢!

As pointed out in the comments onTapDown() and OnTapCancel will do the job.正如评论中所指出的onTapDown()OnTapCancel将完成这项工作。

This is the code:这是代码:


double opacity= 1.0; //Initialise for the first time Widget build.
GestureDetector(
              onTapDown: (TapDownDetails details) {
                opacity = 0.4;
                print('tappedDown');
                setState(() {});
              },
              onTapCancel: () {
                opacity = 1.0;
                print('tapcancel');
                setState(() {});
              },
              child: AnimatedOpacity(
                duration: Duration(milliseconds: 300),
                opacity: opacity,
                child: Text('Your Text'),
              ),
            )

There is ready-to-use solution:有现成的解决方案:

CupertinoButton(
  onPressed: () {
    // any action
  },
  child: Text('Tap'),
)

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

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