Debendra
Debendra

Reputation: 65

How to check condition in splashscreen?

I've implemented a splash screen. I want to check the condition if agree is empty go to the TermsAndCondition page else move HomePage. Below I've attached the code of splashscreen please find and check.

SplashScreen :


import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:newbharatbiz/Screens/HomePage.dart';
import 'package:newbharatbiz/Screens/TermsAndCondition.dart';
import 'package:newbharatbiz/Utils/MySharedPreferences.dart';


var agree;

void main() async {


  WidgetsFlutterBinding.ensureInitialized();

  var initialRoute = '';

  MySharedPreferences.instance
      .getStringValue("agree")
      .then((value) => setState(() {
    agree = value;
  }));

  if (agree.isEmpty) {
    initialRoute = 'TermsAndCondition';
  }else{
    initialRoute = 'home';
  }

  runApp(MyApp(initialRoute: initialRoute));
}

setState(Null Function() param0) {
}


class MyApp extends StatelessWidget {
  final String initialRoute;

  MyApp({required this.initialRoute});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'Flutter Demo',
        theme: ThemeData(
          primarySwatch: Colors.blue,
        ),
        initialRoute: initialRoute,
        routes: {
          'TermsAndCondition': (context) => TermsAndCondition(),
          'home': (context) => HomePage(),
        });
  }
}

Upvotes: 2

Views: 574

Answers (2)

Sudip Mahanta
Sudip Mahanta

Reputation: 117

Consider the given code as an example and implement the logic in your application.

Overview Initialse your SharedPrefrence.
PrefUtils prefUtils = PrefUtils();

Write a custom tailored method with desired duration to be execute in initState() method.

Future.delayed(const Duration(seconds: 3), () async {
      
      String? check = prefUtils.getString(PrefString.isFirstTime);
      await _controller.getPermission();
      if (check == "true") {
        Get.offAllNamed(AppRoute.loginPage);
        return;
      } else {
        Get.offAllNamed(AppRoute.privacyPolicyInitial);
        return;
      }
    });

In brief you can go through below code:

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

@override
State<SplashPage> createState() => _SplashPageState();
}

class _SplashPageState extends State<SplashPage> with TickerProviderStateMixin {
// Initialse your preference class.
PrefUtils prefUtils = PrefUtils();

@override
void initState() {
 super.initState();

 Future.delayed(const Duration(seconds: 3), () async {
   
   String? check = prefUtils.getString(PrefString.isFirstTime);
   await _controller.getPermission();
   if (check == "true") {
     Get.offAllNamed(AppRoute.loginPage);
     return;
   } else {
     Get.offAllNamed(AppRoute.privacyPolicyInitial);
     return;
   }
 });
}

@override
Widget build(BuildContext context) {
 return Scaffold(
   body: Container(
     width: ScreenUtils.screenWidth,
     height: ScreenUtils.screenHeight,
     padding: EdgeInsets.symmetric(horizontal: 25.customWidth),
     alignment: Alignment.center,
     decoration: BoxDecoration(
       color: Colors.white
     ),
     child: SvgPicture.asset(ImagePath.splashScreenLogo),
   ),
 );
}
}```

Upvotes: 1

Saied Islam Shuvo
Saied Islam Shuvo

Reputation: 237

if your shared preferences give the right data then change the initial route to your logical page.

initialRoute: agree == null || agree == ''? TermsAndCondition() : HomePage()

Upvotes: 1

Related Questions