Home > OS >  Flutter one time intro screen with 3 diffierent screen
Flutter one time intro screen with 3 diffierent screen

Time:08-30

I have 3 screens which include how to use the app with a slide changing the pages. I tried some works but couldn't get the solution. the final page has a button which can lead to the app home page. if the user once visited I need to get rid of intro screens.

import 'package:google_fonts/google_fonts.dart';
import 'package:todo_app/modal/homepage.dart';

class Banner3 extends StatelessWidget {
  const Banner3({ Key? key }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            SizedBox(
              child: Image.asset("assets/vector3.jpg", height: 300.0,),
            ),
            Padding(
              padding: const EdgeInsets.all(8.0),
              child: SizedBox(child: Text("Unless our conception of patriotism is progressive,It cannot hope to embody the real affection and the real interest of the nation.", style: GoogleFonts.poppins(fontSize: 20.0, fontWeight: FontWeight.bold),)),
            ),
            SizedBox(
            child: ElevatedButton(onPressed: (){
              Navigator.push(context, MaterialPageRoute(builder: (context)=> const HomePage()));
            }, 
            style: ElevatedButton.styleFrom(
                primary: Colors.yellow,
                padding: const EdgeInsets.symmetric(horizontal: 40.0, vertical: 15.0),
                shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(50.0)
              )
              ),
            child: Text("Get Started", style: GoogleFonts.poppins(fontSize: 18.0, fontWeight: FontWeight.bold, color: Colors.white))),)
          ],
        ),
      )
    );
  }
} ```


this is my last banner code which contains a button will lead to homepage. how to show only one time after the user seen. 

CodePudding user response:

Use this

 // ignore_for_file: inference_failure_on_function_invocation

import 'package:bookmystudio/config/size_config.dart';
import 'package:bookmystudio/constant/colors.dart';
import 'package:bookmystudio/constant/text_style.dart';
import 'package:bookmystudio/gen/assets.gen.dart';
import 'package:bookmystudio/screens/login/screen/login_screen.dart';
import 'package:bookmystudio/screens/splash/widget/atom/gredient_container.dart';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:google_fonts/google_fonts.dart';

class OnboardingModel {
  OnboardingModel({
    required this.path,
    required this.title,
    required this.subTitle,
  });
  final String path;
  final String title;
  final String subTitle;
}

class OnboardingScreen extends StatefulWidget {
  const OnboardingScreen({super.key});

  @override
  State<OnboardingScreen> createState() => _OnboardingScreenState();
}

class _OnboardingScreenState extends State<OnboardingScreen> {
  ValueNotifier<int> indexValue = ValueNotifier(0);
  PageController _pageController = PageController();
  List<OnboardingModel> onboardingData = [
    OnboardingModel(
      path: Assets.images.onboarding1.path,
      title: '''instant studio\nbooking''',
      subTitle:
          'You can book your favorite music studio instantly on bookmystudio.',
    ),
    OnboardingModel(
      path: Assets.images.onboarding2.path,
      title: 'select nearby studios\nat anytime.! ',
      subTitle:
          'You can select nearby music studio at anytime on bookmystudio.',
    ),
    OnboardingModel(
      path: Assets.images.onboarding3.path,
      title: '''get discounts\non every booking ''',
      subTitle: 'You get discount on every booking on bookmystudio.',
    ),
  ];
  @override
  Widget build(BuildContext context) {
    return GredientContainer(
      child: ValueListenableBuilder(
        valueListenable: indexValue,
        builder: (BuildContext context, int value, Widget? child) {
          return Scaffold(
            backgroundColor: Colors.transparent,
            floatingActionButton: FloatingActionButton(
              backgroundColor: Colors.transparent,
              onPressed: () {
                if (value == onboardingData.length - 1) {
                  Get.to(LoginScreen());
                } else {
                  _pageController.nextPage(
                    duration: Duration(milliseconds: 500),
                    curve: Curves.easeIn,
                  );
                }
              },
              child: Assets.icons.nextButton.image(),
            ),
            body: PageView.builder(
              controller: _pageController,
              itemCount: 3,
              onPageChanged: (val) {
                indexValue.value = val;
              },
              itemBuilder: (context, index) => OnboadingWidget(
                onboardingData: onboardingData,
                value: value,
              ),
            ),
          );
        },
      ),
    );
  }
}

class OnboadingWidget extends StatelessWidget {
  const OnboadingWidget({
    Key? key,
    required this.onboardingData,
    required this.value,
  }) : super(key: key);

  final List<OnboardingModel> onboardingData;
  final int value;

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Center(
        child: Column(
          children: <Widget>[
            SizedBox(
              height: 20.toHeight,
            ),
            AnimatedContainer(
              duration: const Duration(milliseconds: 500),
              height: 352.toHeight,
              width: 312.toWidth,
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(15),
                image: DecorationImage(
                  image: AssetImage(onboardingData[value].path),
                ),
                color: Kcolor.fadeWhite,
              ),
              child: value == 2
                  ? SizedBox.shrink()
                  : GestureDetector(
                      onTap: () {
                        Get.to(LoginScreen());
                      },
                      child: Align(
                        alignment: Alignment.topRight,
                        child: Padding(
                          padding: EdgeInsets.all(16.toWidth),
                          child: Text(
                            'skip',
                            style: KtextStyle.noColor14500,
                          ),
                        ),
                      ),
                    ),
            ),
            Padding(
              padding: const EdgeInsets.all(8),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  ...List.generate(
                    3,
                    (index) => Padding(
                      padding: EdgeInsets.all(7.toWidth),
                      child: AnimatedContainer(
                        duration: const Duration(seconds: 1),
                        height: 6.toHeight,
                        width: 6.toHeight,
                        decoration: BoxDecoration(
                          color:
                              index == value ? Kcolor.yellow : Kcolor.fadeWhite,
                          borderRadius: BorderRadius.circular(100),
                        ),
                      ),
                    ),
                  ).toList(),
                ],
              ),
            ),
            Padding(
              padding: EdgeInsets.only(top: 28.0.toHeight),
              child: Text(
                onboardingData[value].title,
                textAlign: TextAlign.center,
                style: KtextStyle.noColor24700.copyWith(
                  color: Kcolor.yellow,
                  fontFamily:
                      GoogleFonts.inter(fontWeight: FontWeight.bold).fontFamily,
                ),
              ),
            ),
            Padding(
              padding: EdgeInsets.only(
                  top: 32.0.toHeight, left: 24.toWidth, right: 24.toWidth),
              child: Text(
                onboardingData[value].subTitle,
                style: KtextStyle.noColor16400.copyWith(
                  color: Colors.white.withOpacity(0.8),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

CodePudding user response:

Use shared_preferences and save a value such as bool userVisited = false; on the final page button. on button click change it to true. Then check the value on slider page on app load.

  • Related