Splash Screen, On boarding and Login Screen in Flutter Visual Studio Code

1

I have a few question because I'm a bit confused and don't know where to start. I create a splash screen(and it's working properly), but after the splash screen I am expecting onboarding is next, but when i run the app, it shows splash and login is next. Do i need to create a class in main.dart to all screen?

main
import 'dart:async';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/physics.dart';
import 'package:flutter_app/splash_screen.dart';
import 'package:flutter/services.dart';
import 'package:flutter_app/onboarding_screen.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    SystemChrome.setPreferredOrientations([
      DeviceOrientation.portraitUp,
      DeviceOrientation.portraitDown,
    ]);

    return MaterialApp(
        theme: ThemeData(
          primarySwatch: Colors.blue,
        ),
        debugShowCheckedModeBanner: false,
        home: SplashScreen());
  }
}

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: "Travel On Boarding",
    );
  }
}
splash Screen
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:shimmer/shimmer.dart';
import 'package:flutter_app/onboarding_screen.dart';
import 'package:flutter_app/login_screen.dart';

class SplashScreen extends StatefulWidget {
  @override
  _SplashScreenState createState() => _SplashScreenState();
}

class _SplashScreenState extends State<SplashScreen> {
  @override
  void initState() {
    super.initState();

    _mockCheckForSession().then((status) {
      if (status) {
        _navigateToHome();
      } else {
        _navigateToLogin();
      }
    });
  }

  Future<bool> _mockCheckForSession() async {
    await Future.delayed(Duration(milliseconds: 6000), () {});

    return true;
  }

  void _navigateToHome() {
    Navigator.of(context).pushReplacement(
        MaterialPageRoute(builder: (BuildContext context) => LoginScreen()));
  }

  void _navigateToLogin() {
    Navigator.of(context).pushReplacement(
        MaterialPageRoute(builder: (BuildContext context) => LoginScreen()));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.black,
      body: Container(
        child: Stack(
          alignment: Alignment.center,
          children: <Widget>[
            Opacity(
              opacity: 0.5,
              child: Image.asset('assets/img/bg.png'),
            ),
            Shimmer.fromColors(
              period: Duration(milliseconds: 2000),
              baseColor: Color(0xffffffff),
              highlightColor: Color(0xff000000),
              child: Container(
                alignment: Alignment.center,
                padding: EdgeInsets.all(30.0),
                child: Text(
                  "ASE",
                  textAlign: TextAlign.center,
                  style: TextStyle(
                    fontSize: 90.0,
                    fontFamily: 'Pacifico',
                  ),
                ),
              ),
            )
          ],
        ),
      ),
    );
  }
}

onboarding
import 'package:flutter/material.dart';

class OnboardingScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(title: Text('Onboarding')),
        body: Center(
          child: Card(
            color: Colors.grey[300],
            elevation: 10.0,
            child: Container(
                height: 100.0,
                width: 100.0,
                alignment: Alignment.center,
                child: Text(
                  'Welcome Home!',
                  textAlign: TextAlign.center,
                  style: TextStyle(fontSize: 20.0),
                )),
          ),
        ));
  }
}

Login
import 'package:flutter/material.dart';

class LoginScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(title: Text('Login')),
        body: Center(
          child: Container(
              alignment: Alignment.center,
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Container(
                    margin: EdgeInsets.all(8.0),
                    child: TextField(
                      decoration: InputDecoration(hintText: "Enter Username"),
                    ),
                  ),
                  Container(
                    margin: EdgeInsets.all(8.0),
                    child: TextField(
                      decoration: InputDecoration(hintText: "Enter Password"),
                    ),
                  ),
                  MaterialButton(
                    onPressed: () {},
                    child: Text("Login", style: TextStyle(color: Colors.white)),
                    color: Colors.blueAccent,
                  )
                ],
              )),
        ));
  }
}
visual-studio-code
asked on Stack Overflow Jul 13, 2020 by SEA • edited Jul 13, 2020 by Christopher Moore

1 Answer

0

just change this function this way

void _navigateToHome() {
    Navigator.of(context).pushReplacement(
        MaterialPageRoute(builder: (BuildContext context) => OnboardingScreen()));
  }
answered on Stack Overflow Jul 13, 2020 by Niteesh

User contributions licensed under CC BY-SA 3.0