8
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Flutterで簡単に最初に一回だけ出てくる紹介画面を作る

Last updated at Posted at 2020-12-31

flutterで紹介画面(イントロダクションスクリーン)を作る!

このような感じで作ってみました↓↓
スクリーンショット 2020-12-31 17.03.49.png

公式サイトのデモ画像↓

スクリーンショット 2020-12-31 17.09.13.png

introduction_screen を使う

こんな感じで使う


return IntroductionScreen(
                pages: [
                  PageViewModel(
                    title: "宿題管理アプリSKIMERへようこそ",
                    body:
                        "宿題をみんなで管理\n宿題を終わらせてランクを上げよう!",
                    image: Center(
                      child: Image.asset("assets/skimer-07.png"),
                    ),
                  )
                ],
                onDone: () async {
                  SharedPreferences prefs =
                      await SharedPreferences.getInstance();
                  await prefs.setBool('haveSeenIntro', true);
                  Navigator.of(context).push(MaterialPageRoute(builder: (_) {
                    return HomePage();
                  }));
                },
                showSkipButton: true,
                skip: const Text("スキップ"),
                done: const Text("ホームへ",
                    style: TextStyle(fontWeight: FontWeight.w600)),
              );

一回だけ表示させる

SharedPreferencesの'haveSeenIntro'trueにすることで、初めてみたときだけ表示させることができます。
FutureBuilderを使って、非同期のhaveSeenIntro()関数で見たかどうかをチェックして処理を分けました。
SplashScreenはローディングスクリーンです。

class HogeScreen extends StatelessWidget {
  Future<bool> haveSeenIntro() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    return prefs.getBool('haveSeenIntro') ?? false;
  }
  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
          future: haveSeenIntro(),
          builder: (context, snapshot) {
            if (snapshot.connectionState == ConnectionState.waiting) {
              return SplashScreen();
            }

            if (snapshot.error != null) {
              return SplashScreen();
            }
            if (snapshot.data) {
              return HomePage();
            } else {
              return IntroductionScreen(
                pages: [
                  PageViewModel(
                    title: "宿題管理アプリSKIMERへようこそ",
                    body:
                        "宿題をみんなで管理\n宿題を終わらせてランクを上げよう!",
                    image: Center(
                      child: Image.asset("assets/skimer-07.png"),
                    ),
                  )
                ],
                onDone: () async {
                  SharedPreferences prefs =
                      await SharedPreferences.getInstance();
                  await prefs.setBool('haveSeenIntro', true);
                  Navigator.of(context).push(MaterialPageRoute(builder: (_) {
                    return HomePage();
                  }));
                },
                showSkipButton: true,
                skip: const Text("スキップ"),
                done: const Text("ホームへ",
                    style: TextStyle(fontWeight: FontWeight.w600)),
              );
            }
          },
        );
    }
}
8
14
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?