Maeve
Maeve

Reputation: 25

Flutter: How to add SplashScreen in main page

Hi I'm trying to add my SplashScreen page to my main.dart but I'm having problem adding the SplashScreen() to my home:

My home currently is Scaffold. I'm was wondering how can I include my SplashScreen there. I'm trying to figure out how to change the Scaffold. Could someone please help me. Thank you so much in advance.

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


class MyApp extends StatefulWidget {
  @override
    State<StatefulWidget> createState() {
      return MyAppState();
    }
}

class MyAppState extends State<MyApp> {

  int _selectedPage = 0;
  final _pageOptions = [
    Home(),
    Category(),
    Video(),
    
  ];

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Flutter Demo',
   
         
      initialRoute: '/',
      onGenerateRoute: RouteGenerator.generateRoute,
    

    home: Scaffold(

      
     
      body: _pageOptions[_selectedPage],
      bottomNavigationBar: BottomNavigationBar(
        type: BottomNavigationBarType.fixed,
        currentIndex: _selectedPage,
        onTap: (int index) {
          setState(() {
            _selectedPage = index;
          });
        },
         items: [
              BottomNavigationBarItem(
                icon: Icon(Icons.home), title: Text('Home')),
              BottomNavigationBarItem(
                icon: Icon(Icons.format_list_bulleted), title: Text('Category')),
              BottomNavigationBarItem(
                icon: Icon(Icons.subscriptions), title: Text('Videos')),
              
            ]

      ),
    )

    );
    
  }
}

Upvotes: 2

Views: 233

Answers (1)

Sanket Vekariya
Sanket Vekariya

Reputation: 2956

Try below code:

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: SplashScreen(),
    );
  }
}

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

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Text("Splash Screen"),
      ),
    );
  }

  countDownTime() async {
    return Timer(
      Duration(seconds: 2),
      () async {
        Navigator.pushReplacement(
          context,
          MaterialPageRoute(builder: (context) => HomeScreen()),
        );
      },
    );
  }
}

class HomeScreen extends StatefulWidget {
  @override
  HomeScreenState createState() => HomeScreenState();
}

class HomeScreenState extends State<HomeScreen> {
  int _selectedPage = 0;
  final _pageOptions = [
    Scaffold(body: Center(child: Text("Home")),),
    Scaffold(body: Center(child: Text("Category")),),
    Scaffold(body: Center(child: Text("Video")),),
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: _pageOptions[_selectedPage],
      bottomNavigationBar: BottomNavigationBar(
          type: BottomNavigationBarType.fixed,
          currentIndex: _selectedPage,
          onTap: (int index) {
            setState(() {
              _selectedPage = index;
            });
          },
          items: [
            BottomNavigationBarItem(
                icon: Icon(Icons.home), title: Text('Home')),
            BottomNavigationBarItem(
                icon: Icon(Icons.format_list_bulleted),
                title: Text('Category')),
            BottomNavigationBarItem(
                icon: Icon(Icons.subscriptions), title: Text('Videos')),
          ]),
    );
  }
}

Upvotes: 2

Related Questions