栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

颤动一次介绍屏幕?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

颤动一次介绍屏幕?

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

对于这种情况,您可以使用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软件包。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/401848.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号