我的应用程序有一个介绍屏幕,但每次我打开应用程序时它都会显示, 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块钱怎么做??

请添加所需代码.

推荐答案

如果您希望仅第一次显示Introo屏幕,则需要在本地保存该用户已经看到的Introo.

对于这样的事情,你可以用Shared Preference.有一个flutter package的共享偏好,你可以使用

EDITED:

Please refer to the below complete tested code to understand how to use it:

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内部的上下文并不是立即准备好的.

现在我用afterFirstLayout代替了它,它已经准备好了.您将需要安装软件包after_layout.

Flutter相关问答推荐

使用Flutter 提供程序包加载状态应用程序时,在自定义按钮中显示CircularProgressIndicator

使用ThemeProvider不在亮模式和暗模式之间切换

如何将Xpriter代码页更改为代码页27?

Flutter creating Constant.dart InputDecoration section put pass in hintText

Flutter 隔离.使用多个参数运行

如何在抖动中预加载一条路由,或者即使在屏幕外也保持加载小工具?

FOR回路中的Flutter 模型

从Firestore流式传输单个文档还是整个集合更好?

dotenv:未处理的异常:FileNotFoundError的实例

处理 flutter/dart 模型中的空值

如何在Flutter 中使用选定的键和值从 map 列表创建 map

为 flutter app/firebase 保存 chatgpt api-key 的正确方法是什么

我无法找到修复此错误的位置,指出无法使用静态访问来访问实例成员

网页浏览器不支持鼠标滚动的行项目

Flutter: pie_chart 插件动画滚动问题

无法将Null类型的值分配给 const 构造函数中List类型的参数

Flutter HLS .m3u8 视频无法实时播放

Firestore:缓存和读取数据

我想在数组中添加项目

Flutter Web App 未启动:脚本的 MIME 类型不受支持