延迟使用颤振

时间:2019-04-10 13:21:04

标签: flutter

我有带有动画的启动画面 在一段时间的延迟下,我检查用户是否以前登录过。如果是,则将其导航到HomeActivity或LoginActivity。

但是它并没有成功,并且停留在启动屏幕上

这是我在做什么

class Splash extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      debugShowCheckedModeBanner: false,
      home: SplashRippleView(),
    );
  }
}

class SplashRippleView extends StatefulWidget {
  @override
  _SplashRippleViewState createState() => _SplashRippleViewState();
}

class _SplashRippleViewState extends State<SplashRippleView>
    with SingleTickerProviderStateMixin {
  PageController pageController = new PageController();
  var _currentIndex = 0;
  AnimationController _controller;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(
      vsync: this,
      lowerBound: 0.5,
      duration: Duration(seconds: 3),
    )..repeat();

    Future.delayed(const Duration(seconds: 5), () {
      checkloggedIn();
    });
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  checkloggedIn() async {
    bool loggedIn = await Preference().readBoolValue("login");
    if (loggedIn) {
      Navigator.push(context,
          MaterialPageRoute(builder: (context) => HomeActivity()));
    } else {
      Navigator.push(
          context, MaterialPageRoute(builder: (context) =>Login()));
    }
  }

   @override
  Widget build(BuildContext context) {}
}

1 个答案:

答案 0 :(得分:0)

可以尝试一下吗?它为我工作。

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

class Splash extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      debugShowCheckedModeBanner: false,
      home: SplashRippleView(),
    );
  }
}

class SplashRippleView extends StatefulWidget {
  @override
  _SplashRippleViewState createState() => _SplashRippleViewState();
}

class _SplashRippleViewState extends State<SplashRippleView> with SingleTickerProviderStateMixin {
  PageController pageController = new PageController();
  var _currentIndex = 0;
  AnimationController _controller;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(
      vsync: this,
      lowerBound: 0.5,
      duration: Duration(seconds: 3),
    )..repeat();

    Future.delayed(const Duration(seconds: 5), () {
      checkLoggedIn();
    });
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  void checkLoggedIn() async {
    bool loggedIn = await Future.delayed(Duration(seconds: 1), () => false);
    if (loggedIn) {
      Navigator.push(context, MaterialPageRoute(builder: (context) => HomeActivity()));
    } else {
      Navigator.push(context, MaterialPageRoute(builder: (context) => Login()));
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("Splash"),),
      body: Center(child: Text("Splash screen")),
    );
  }
}

class HomeActivity extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("Home"),),
    );
  }
}

class Login extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("Login"),),
    );
  }
}