颤抖一次介绍屏幕? [英] Flutter One time Intro Screen?

查看:64
本文介绍了颤抖一次介绍屏幕?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序有一个简介屏幕,但是每次打开该应用程序时都会显示
我只需要第一次显示该信息

I have an intro screen for my app, but it shows every time I open the app, I need to show that for the 1st time only.

该怎么做?

//THIS IS THE SCREEN COMES 1ST WHEN OPENING THE APP (SPLASHSCREEN)

class SplashScreen extends StatefulWidget {
  @override
  _SplashScreenState createState() => _SplashScreenState();
}

class _SplashScreenState extends State<SplashScreen> {
  @override
  void initState() {
    super.initState();

    //After 2seconds of time the Introscreen will e opened by bellow code
    Timer(Duration(seconds: 2), () => MyNavigator.goToIntroscreen(context));
  }

  //The below code has the text to show for the spalshing screen
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: new Center(
        child: Text('SPLASH SCREEN'),
      ),
    );
  }
}

每次此屏幕打开2秒钟的简介屏幕时延迟。
,但我只想第一次使用 sharedpreference ??

Every time this screen opens the intro screen with 2 seconds delay. but I want for the first time only How to do that with sharedpreference??

推荐答案

如果只希望首次显示介绍屏幕,则需要将其保存在本地该用户已经看到了介绍。

If you wish to show the intro screen only for the first time, you will need to save locally that this user has already seen intro.

对于这种情况,您可以使用共享的偏好。对于共享首选项,有一个 flutter软件包,您可以使用

For such thing you may use Shared Preference. There is a flutter package for Shared Preference which you can use

已编辑

请参考下面完整的经过测试的代码,以了解如何使用 >:

import 'dart:async';

import 'package:after_layout/after_layout.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      color: Colors.blue,
      home: new Splash(),
    );
  }
}

class Splash extends StatefulWidget {
  @override
  SplashState createState() => new SplashState();
}

class SplashState extends State<Splash> with AfterLayoutMixin<Splash> {
  Future checkFirstSeen() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    bool _seen = (prefs.getBool('seen') ?? false);

    if (_seen) {
      Navigator.of(context).pushReplacement(
          new MaterialPageRoute(builder: (context) => new Home()));
    } else {
      await prefs.setBool('seen', true);
      Navigator.of(context).pushReplacement(
          new MaterialPageRoute(builder: (context) => new IntroScreen()));
    }
  }

  @override
  void afterFirstLayout(BuildContext context) => checkFirstSeen();

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      body: new Center(
        child: new Text('Loading...'),
      ),
    );
  }
}

class Home extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text('Hello'),
      ),
      body: new Center(
        child: new Text('This is the second page'),
      ),
    );
  }
}

class IntroScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text('IntroScreen'),
      ),
      body: new Center(
        child: new Text('This is the IntroScreen'),
      ),
    );
  }
}

感谢Ben B 注意到 initState 中延迟的错误使用。我使用了 delay ,因为有时上下文未立即在 initState 内部准备好。

Thanks to Ben B for noticing the incorrect use of delay in initState. I had used a delay because sometimes the context is not ready immediately inside initState.

所以现在我将其替换为 afterFirstLayout ,它已经准备好了上下文。您将需要安装软件包 after_layout

So now I have replaced that with afterFirstLayout which is ready with the context. You will need to install the package after_layout.

这篇关于颤抖一次介绍屏幕?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆