Flutter One time Intro Screen?

后端 未结 6 1269
无人共我
无人共我 2021-01-30 11:22

I have an intro screen for my app, but it shows every time I open the app, I need to show that for the 1st time only.

How to do that?

//         


        
6条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-30 11:59

    I was able to do without using after_layout package and Mixins and instead I have used FutureBuilder.

    class SplashState extends State {
      Future checkFirstSeen() async {
        SharedPreferences prefs = await SharedPreferences.getInstance();
        bool _seen = (prefs.getBool('seen') ?? false);
    
        if (_seen) {
          return HomeScreen.id;
        } else {
            // Set the flag to true at the end of onboarding screen if everything is successfull and so I am commenting it out
          // await prefs.setBool('seen', true);
          return IntroScreen.id;
        }
      }
    
      @override
      Widget build(BuildContext context) {
        return FutureBuilder(
            future: checkFirstSeen(),
            builder: (context, snapshot) {
              if (snapshot.connectionState == ConnectionState.waiting) {
                return Center(
                  child: CircularProgressIndicator(),
                );
              } else {
                return MaterialApp(
                  initialRoute: snapshot.data,
                  routes: {
                    IntroScreen.id: (context) => IntroScreen(),
                    HomeScreen.id: (context) => HomeScreen(),
                  },
                );
              }
            });
      }
    }
    
    class HomeScreen extends StatelessWidget {
    static String id = 'HomeScreen';
      @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 {
    static String id = 'IntroScreen';
      @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'),
          ),
        );
      }
    }
    

提交回复
热议问题