简体   繁体   English

如何在 Flutter 中经过一定时间后切换小部件?

[英]How to Switch Widgets after certain time in Flutter?

I am facing problems with this tematic, I would like to change a widget after certain time.我遇到了这个 tematic 的问题,我想在一段时间后更改一个小部件。 eg I have this animatedFlutter Screen (screen 1) and then after the animation end I would like to change the screen to any screen that I have, for example.例如,我有这个 animationFlutter 屏幕(屏幕 1),然后在动画结束后,我想将屏幕更改为我拥有的任何屏幕。 Login();登录();

Any tips?有小费吗? Thanks.谢谢。

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,

    );
  }
}

try this,尝试这个,

_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()));
  });
});
}

You need to write code of navigating to another screen (Widget in case of Flutter)您需要编写导航到另一个屏幕的代码(Flutter 中的 Widget)

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

You need to import Login in your current screen.您需要在当前屏幕中导入登录。

Wrap your AnimatedFlutterLogo with MaterialApp and in the callback function of Timer use Navigator to navigate to respective PageMaterialApp包裹你的AnimatedFlutterLogo并在 Timer 的回调函数中使用Navigator导航到相应的页面

Example:例子:

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"));
  }
}

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

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