Search code examples
flutterindexingbottomnavigationview

Why there is no index in navigation bar item?


This is the code without error messages:

import 'dart:ui';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';

 class MyStatefulWidget extends StatefulWidget {
  const MyStatefulWidget({Key? key}) : super(key: key);

  @override
  State<MyStatefulWidget> createState() => BottomNavigation();
}

/// This is the private State class that goes with MyStatefulWidget.
class BottomNavigation extends State<MyStatefulWidget> {
int _selectedIndex = 0;
  List<Widget> _widgetOptions= <Widget>[
    Text('Home'),
    Text('Message')
  ];

  static const TextStyle optionStyle =
  TextStyle(fontSize: 30, fontWeight: FontWeight.bold);

  void _onItemTapped(int index) {
    setState(() {
      _selectedIndex=index;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        actions: <Widget>[
          IconButton(icon: Icon(Icons.search), onPressed: () {}),
        ],
      ),
      body: Container(
        decoration: BoxDecoration(
              image: DecorationImage(
                image:
                AssetImage("assets/OptimizeYourFood/OptimizeYourFood2.png"),
                fit: BoxFit.cover,
            ),
              color: Colors.transparent)
      ),
      bottomNavigationBar: BottomNavigationBar(
        items: const <BottomNavigationBarItem>[
          BottomNavigationBarItem(
            icon: Icon(Icons.local_dining),
            label: 'Reduce',
          ),
          BottomNavigationBarItem(
            icon: Icon(FontAwesomeIcons.recycle),
            label: 'Reuse and recycle',
          ),
        ],
        currentIndex: _selectedIndex,
        selectedItemColor: Colors.amber[800],
        onTap: _onItemTapped,
      ),
    );
  }
}
[Nothing comes out of bottom navigation bar items][1]

..................................................................................................................................................................................................This is the end. This is the end. This is the end. This is the end. This is the end.

New Code

New Picture


Solution

  • You are changing the index but not the UI. use _widgetOptions on body. You can directly use body:_widgetOptions[_selectedIndex ] .

    class MyStatefulWidget extends StatefulWidget {
      const MyStatefulWidget({Key? key}) : super(key: key);
    
      @override
      State<MyStatefulWidget> createState() => BottomNavigation();
    }
    
    /// This is the private State class that goes with MyStatefulWidget.
    class BottomNavigation extends State<MyStatefulWidget> {
      int _selectedIndex = 0;
      List<Widget> _widgetOptions = <Widget>[Text('Home'), Text('Message')];
    
      static const TextStyle optionStyle =
          TextStyle(fontSize: 30, fontWeight: FontWeight.bold);
    
      void _onItemTapped(int index) {
        print("tapped  $index");
        setState(() {
          _selectedIndex = index;
        });
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            actions: <Widget>[
              IconButton(icon: Icon(Icons.search), onPressed: () {}),
            ],
          ),
          body: Column(
            children: [
              _widgetOptions[_selectedIndex]
              // Container(
              //     decoration: BoxDecoration(
              //         image: DecorationImage(
              //           image:
              //               AssetImage("assets/OptimizeYourFood/OptimizeYourFood2.png"),
              //           fit: BoxFit.cover,
              //         ),
              //         color: Colors.transparent)),
            ],
          ),
          bottomNavigationBar: BottomNavigationBar(
            items: const <BottomNavigationBarItem>[
              BottomNavigationBarItem(
                icon: Icon(Icons.local_dining),
                label: 'Reduce',
              ),
              BottomNavigationBarItem(
                icon: Icon(Icons.ac_unit),
                //  Icon(FontAwesomeIcons.recycle),
                label: 'Reuse and recycle',
              ),
            ],
            currentIndex: _selectedIndex,
            selectedItemColor: Colors.amber[800],
            onTap: _onItemTapped,
          ),
        );
      }
    }