提问者:小点点

如何在特定的时间内显示小部件?


我在主屏幕上使用一个图标来向用户显示正在进行的某个过程。我希望这个图标在特定的时间内(比如100秒)可见。用户可能会导航到各种屏幕,但是当他回到主屏幕时,他应该能够看到这个图标,并且图标应该在100秒后消失。我该怎么做?


共2个答案

匿名用户

class AnimatedFlutterLogo extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => new _AnimatedFlutterLogoState();
}

class _AnimatedFlutterLogoState extends State<AnimatedFlutterLogo> {
  Timer _timer;
  FlutterLogoStyle _logoStyle = FlutterLogoStyle.markOnly;

  _AnimatedFlutterLogoState() {
    _timer = new Timer(const Duration(milliseconds: 100), () {
      setState(() {
        _logoStyle = FlutterLogoStyle.horizontal;
      });
    });
  }

  @override
  void dispose() {
    super.dispose();
    _timer.cancel();
  }

  @override
  Widget build(BuildContext context) {
    return new FlutterLogo(
      size: 200.0,
      textColor: Palette.white,
      style: _logoStyle,
    );
  }
}

参考此链接如何在Flutter中运行一些延迟后的代码?

或者,您可以使用以下代码来实现延迟状态更新:

Future.delayed(const Duration(milliseconds: 100), () {
  setState(() {
    // Here you can write your code to update the state to show/hide the icon
  });
});

匿名用户

bool _visibility = false;
---------------------------
Timer _timer;
int _start = 1;

 --------------------------
  void startTimer() {
const oneSec = const Duration(seconds: 1);
_timer = new Timer.periodic(
    oneSec,
    (Timer timer) => setState(() {
          if (_start == 10) {
            timer.cancel();
            _changed(true);
          } else {
            _start = _start + 1;
          }
        }));
  }
 ---------------------------------
  void _changed(bool visibility) {
setState(()
  if (_start == 10) {
    _visibility = visibility;
  }
  });
  }
 ---------------------------
  @override
  void initState() {
  super.initState();
  startTimer();
  setState(() {});
  }
-----------------------------
 _visibility ? new Row(
  // create Widget

  )