bottom nav bar

add an index to the bottom nav bar that can be passed to a route in order to navigate to that screen
mail@pastecode.io avatar
unknown
plain_text
2 years ago
3.3 kB
0
Indexable
Never
import 'dart:ui';
import 'package:anime_quiz/components/appbar.dart';
import 'package:anime_quiz/components/quizDrawer.dart';
import 'package:anime_quiz/screens/newsScreen/NewsScreen.dart';
import 'package:anime_quiz/screens/homeScreen/homeScreen.dart';
import 'package:anime_quiz/screens/quizScreens/quizScreen.dart';
import 'package:anime_quiz/utilities/constants.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';

final _scaffoldKey = GlobalKey<ScaffoldState>();

class LayOutScreen extends StatefulWidget {
  static const String id = "layOutScreen";
  final int selectedIndex;
  LayOutScreen({this.selectedIndex});

  @override
  _LayOutScreenState createState() => _LayOutScreenState();
}

class _LayOutScreenState extends State<LayOutScreen> {
  // int fi_currentIndex = 0;
  int _selectedIndex = 1;
  String _title;

  static const List<Widget> _widgetOptions = <Widget>[
    QuizScreen(),
    HomeScreen(),
    NewsScreen(),
  ];

  @override
  initState(){
    _selectedIndex = widget.selectedIndex;
    _title = 'Home';
    super.initState();
  }

  void _onItemTapped(int index) {
    setState(() {
      _selectedIndex = index;
      switch(index) {
        case 0: { _title = 'Anime Quiz'; }
        break;
        case 1: { _title = 'Home'; }
        break;
        case 2: { _title = 'Anime News'; }
        break;
      }
    });
  }

  @override
  Widget build(BuildContext context) {

    return Scaffold(
      key: _scaffoldKey,
      resizeToAvoidBottomInset: false,
      resizeToAvoidBottomPadding: false,
      appBar: PreferredSize(
        preferredSize: Size.fromHeight(kH50),
        child: AppBarAQ(
          title: _title,
          onPress: () => _scaffoldKey.currentState.openDrawer(),
          leadIcon: 'assets/icons/icons8-naruto-50.png',
          actionIcon: null,
        ),
      ),
      bottomNavigationBar: SizedBox(

        height: 52,
        child: Container(
        height: 40,
          child: BottomNavigationBar(
            backgroundColor: kWhiteColor,
            unselectedItemColor: kGreyColor,
            selectedIconTheme: IconThemeData(),
            // showUnselectedLabels: false,
            items: <BottomNavigationBarItem>[
              BottomNavigationBarItem(
                icon: SvgPicture.asset(
                    'assets/icons/shuriken2.svg',
                    width: kH20),
                label: 'Quiz',
              ),
              BottomNavigationBarItem(
                icon: SvgPicture.asset(
                    'assets/icons/icons8-naruto-sign (2).svg',
                    width: kH20),
                label: 'Home',
              ),
              BottomNavigationBarItem(
                icon: SvgPicture.asset(
                    'assets/icons/scroll2.svg',
                    width: kH20),
                label: 'News',
              ),
            ],
            currentIndex: _selectedIndex,
            selectedItemColor: kPrimaryColor,
            onTap: _onItemTapped,
          ),
        ),
      ),
      drawer: QuizDrawer(),
      // body: _widgetOptions.elementAt(_selectedIndex),
      body: IndexedStack(
        index: _selectedIndex,
        children: [
          QuizScreen(),
          HomeScreen(),
          NewsScreen(),
        ],
      ),
    );
  }
}