web-dev-qa-db-fra.com

Écran d'introduction Flutter One time?

J'ai un écran d'introduction pour mon application, mais il s'affiche chaque fois que j'ouvre l'application, je dois le montrer pour la 1ère fois uniquement Comment faire?

//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'),)
);
}
}

À tout moment, cet écran ouvre l'introscreen avec un délai de 2 secondes. mais je veux pour la première fois seulement Comment faire cela avec sharedpreference ?? Pls ajouter les codes requis pls ....

10
Rajesh Jr.

Si vous ne souhaitez afficher l'écran d'introduction que pour la première fois, vous devrez enregistrer localement que cet utilisateur a déjà vu l'intro.

Pour une telle chose, vous pouvez utiliser Préférence partagée . Il y a un paquet flutter pour la préférence partagée que vous pouvez utiliser

[~ # ~] modifié [~ # ~] :

Veuillez vous référer au code testé complet ci-dessous pour comprendre comment l'utiliser :

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()));
            },
            )
        ],
        ),
    ),
    );
}
}
24
Arnold Parge