如何在一定时间后在 Flutter 中切换 Widgets?

How to Switch Widgets after certain time in Flutter?

我遇到了这个主题的问题,我想在一定时间后更改一个小部件。例如 我有这个 animatedFlutter Screen(屏幕 1),然后在动画结束后我想将屏幕更改为我拥有的任何屏幕,例如。登录();

有什么建议吗?谢谢

import 'package:flutter/material.dart';
import 'dart:async';

void main() => runApp(AnimatedFlutterLogo());

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: 800), () {
      setState(() {
        _logoStyle = FlutterLogoStyle.horizontal;
      });
    });
  }

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

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

    );
  }
}

您需要编写导航到另一个屏幕的代码(如果是 Flutter,则为 Widget)

Navigator.push(
    context,
    MaterialPageRoute(builder: (context) => Login()),
  );

您需要在当前屏幕导入登录。

试试这个,

_Act_NotificationScreenState() {
_timer = new Timer(const Duration(milliseconds: 800), () {
  setState(() {
    _logoStyle = FlutterLogoStyle.horizontal;
  });
  _timer = new Timer(const Duration(seconds: 1), () {
      Navigator.push(context, MaterialPageRoute(builder: (context) => Act_Login()));
  });
});
}

MaterialApp 包装你的 AnimatedFlutterLogo 并在定时器的回调函数中使用 Navigator 导航到相应的页面

示例:

import 'package:flutter/material.dart';
import 'dart:async';

void main() => runApp(MaterialApp(home:AnimatedFlutterLogo()));

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: 800), () {
       setState(() {
        _logoStyle = FlutterLogoStyle.horizontal;
      });
    Navigator.push(                      //<-- Navigate to loginPage on Timeout
    context,
    MaterialPageRoute(builder: (context) => LoginPage()),
  );
    });
  }

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

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

    );
  }
}

class LoginPage extends StatelessWidget{
  @override 
  Widget build(BuildContext context){
    return Container(alignment: Alignment.center,child: Text("LOG IN PAGE"));
  }
}