颤动一次介绍屏幕?

我的应用程序有一个简介屏幕,但是每次打开应用程序时都会显示该屏幕, 屏幕 。

怎么做?

//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??

请添加所需的代码。

回答:

如果您只想第一次显示介绍屏幕,则需要在本地保存该用户已经看过介绍的屏幕。

对于这种情况,您可以使用Shared

Preference。有一个用于共享首选项的Flutter软件包,您可以使用

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'),

),

);

}

}

感谢BenB注意到错误地使用延迟initState。我用过a,delay因为有时上下文尚未立即准备就绪initState

因此,现在我替换afterFirstLayout了上下文准备就绪的内容。您将需要安装after_layout软件包。

以上是 颤动一次介绍屏幕? 的全部内容, 来源链接: utcz.com/qa/397726.html

回到顶部