【问题标题】:How to show a container for 3 seconds如何显示一个容器 3 秒
【发布时间】:2020-08-07 14:07:28
【问题描述】:

我有一个容器,其中有 2 个字段。 1 是百分比,另一个是简单文本。我需要的是我不想显示百分比容器,当我单击容器时,它只会显示百分比 3 秒然后消失,谁能告诉它怎么可能?

这是我的代码

int size = _questions.length;

void nextQuestion(){
  if(index < size - 1)
    setState(() {
      index++;
    });
  print(index);
}


double percentage1Calculate(){
  int wouldClick = int.parse(_questions[index]['wouldclick']);
  int ratherClick = int.parse(_questions[index]['ratherclick']);
  double percentage1 = wouldClick / (wouldClick + ratherClick) * 100;
  return percentage1;
}


        GestureDetector(
          child: Container(
            height: stackHeight * 0.5,
            width: stackWidth,
            color: Colors.blue,
            child: Column(
              children: <Widget>[
                Container(
                    padding: const EdgeInsets.only(top: 10, right: 10),
                    height: stackHeight * 0.1,
                    color: Colors.blue,
                    width: double.infinity,
                    child: Column(
                      mainAxisSize: MainAxisSize.max,
                      crossAxisAlignment: CrossAxisAlignment.end,
                      children: <Widget>[
                        Text('${percentage1Calculate().toStringAsFixed(0)}%',
                          style: TextStyle(
                            color: Colors.white,
                            fontSize: 23,
                            fontFamily: 'NewsCycle',
                          ),
                        ),
                      ],

                    )
                ),
                Container(
                  color: Colors.blue,
                  height: stackHeight * 0.4,
                  width: double.infinity,
                  child: Column(
                    children: <Widget>[
                      Padding(
                        padding: const EdgeInsets.only(top: 20),
                        child: Text(
                          _questions[index]['would'],
                          style: TextStyle(
                            color: Colors.white,
                            fontSize: 23,
                            fontFamily: 'NewsCycle',
                          ),
                        ),
                      ),
                    ],
                  )
                ),
              ],
            ),
          ),
        ),

在代码中,我在 GestureDetector 中包装了一个容器。在容器中我有 2 个容器。两者都显示文本。我需要的是当用户单击手势检测器时,第一个容器显示该值,并在 3 秒后隐藏。

【问题讨论】:

    标签: flutter


    【解决方案1】:

    这是一个示例代码

    class _MyWidgetState extends State<MyWidget> {
      var showPercentage = true;
    
      @override
      Widget build(BuildContext context) {
        return Container(
          child: Column(
            children: <Widget>[
                GestureDetector(
                  // ==== THIS IF STATEMENT WILL TAKE CARE OF THE REST
                  child:if (showPercentage) Text('Percentage widget'),
                  onTap: () {
                    setState(() {
                      showPercentage = !showPercentage;
                    });
                    Timer timer = Timer(Duration(seconds: 3), () {
                      setState(() {
                        showPercentage = false;
                      });
                    });
                  },
                ),
              Text('text widget'),
            ],
          ),
        );
      }
    }
    

    【讨论】:

    • 此代码不起作用,因为GestureDetector 将在一次使用后消失。
    • 不是问题的全部,如果想再次显示百分比应该从其他地方致电setState(() { showPercentage = true; });
    • 您的结构方式使得在不重新运行应用程序的情况下无法再次显示百分比。
    • 它可能有效,但不是 OP 所要求的,它有冗余代码。将GestureDetector 放在if 语句之外会是一个更好的解决方案。
    【解决方案2】:

    您应该首先创建一个类似shouldShow 的条件来确定何时显示容器,然后在列中的容器之前执行类似if(shouldShow) 的操作。

    GestureDetectoronTap 回调中,调用setState 并将shouldShow 的值更改为true。在 onTap 中,您还应该使用 3 秒的 Duration 开始一个新的 Timer,并通过回调再次调用 setState 并将 shouldShow 更改为 false

    onTap 示例:

    onTap: () {
      setState(() {
        shouldShow = true;
      });
      Timer timer = Timer(Duration(seconds: 3), () {
        setState(() {
          shouldShow = false;
        });
      });
    }
    

    构建方法sn-p:

    child: Column(
      children: <Widget>[
        if(shouldShow)
        Container(
          padding: const EdgeInsets.only(top: 10, right: 10),
          height: stackHeight * 0.1,
          color: Colors.blue,
          width: double.infinity,
          child: Column(
            mainAxisSize: MainAxisSize.max,
            crossAxisAlignment: CrossAxisAlignment.end,
              children: <Widget>[
                Text('${percentage1Calculate().toStringAsFixed(0)}%',
                  style: TextStyle(
                  color: Colors.white,
                  fontSize: 23,
                  fontFamily: 'NewsCycle',
                ),
              ),
            ],
          )
        ),
        //Other container here
      ],
    ),
    

    【讨论】:

    • 定时器显示错误。需要为计时器导入一些东西吗?
    • 方法 Timer 没有为类定义
    • 你必须导入import 'dart:async';
    • @ShahnazRaheem 告诉我进展如何。
    • 感谢它的工作你能告诉我如何应用其他条件吗?如在此,如果 shouldShow 为 true,则它将显示此容器,如果为 false,则显示其他容器,再次感谢
    【解决方案3】:

    flutter 文档有一个很好的例子,通过用AnimatedOpacity 小部件包装一个容器,在 500 毫秒后淡出一个小部件。

    AnimatedOpacity(
      // If the widget is visible, animate to 0.0 (invisible).
      // If the widget is hidden, animate to 1.0 (fully visible).
      opacity: _visible ? 1.0 : 0.0,
      duration: Duration(milliseconds: 500),
      // The green box must be a child of the AnimatedOpacity widget.
      child: Container(
        width: 200.0,
        height: 200.0,
        color: Colors.green,
      ),
    );
    

    【讨论】:

    • 这并不能真正回答 OP 的问题。
    猜你喜欢
    • 1970-01-01
    • 2016-02-12
    • 2023-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多