微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

android – Flutter一次性介绍屏幕?

我的应用程序有一个简介屏幕,但每次打开应用程序时都会显示
我需要第一次证明这一点
怎么做?

//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秒.
但我只是第一次想要如何使用共享偏好?
请添加所需的代码请….

解决方法:

如果您希望仅首次显示介绍屏幕,则需要在本地保存此用户已经看过的介绍.

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

编辑:

请参阅以下完整测试代码以了解如何使用它:

import 'dart:async';

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> {
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 {
    prefs.setBool('seen', true);
    Navigator.of(context).pushReplacement(
        new MaterialPageRoute(builder: (context) => new IntroScreen()));
    }
}

@override
void initState() {
    super.initState();
    new Timer(new Duration(milliseconds: 200), () {
    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(
    body: new Center(
        child: new Column(
        mainAxisSize: MainAxisSize.min,
        children: <Widget>[
            new Text('This is the intro page'),
            new MaterialButton(
            child: new Text('Gogo Home Page'),
            onPressed: () {
                Navigator.of(context).pushReplacement(
                    new MaterialPageRoute(builder: (context) => new Home()));
            },
            )
        ],
        ),
    ),
    );
}
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐