Флаттер Один раз на экране? - PullRequest
0 голосов
/ 02 июня 2018

У меня есть заставка для моего приложения, но она отображается каждый раз, когда я открываю приложение, Мне нужно показать, что только в первый раз Как это сделать?

//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 ??Пожалуйста, добавьте необходимые коды, пожалуйста ....

1 Ответ

0 голосов
/ 02 июня 2018

Если вы хотите показать экран вступления только в первый раз, вам нужно будет сохранить локально, чтобы этот пользователь уже видел вступление.

Для этого вы можете использовать Shared Preference .Существует пакет флаттера для общих предпочтений, который вы можете использовать

EDITED :

Пожалуйста, ознакомьтесь с приведенным ниже полным протестированным кодом дляпонять, как его использовать :

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()));
            },
            )
        ],
        ),
    ),
    );
}
}
...