0

I make bottomNavigationBar like below

  List<Widget> pages = [
    Dashboard(),
    AllServices(),
    InformationPage(),
  ];


  int _selectedIndex = 0;

  Widget _bottomNavigationBar(int selectedIndex)  {
    return BottomNavigationBar(
      onTap: (int index) => setState(() => _selectedIndex = index),
      currentIndex: selectedIndex,
      type: BottomNavigationBarType.fixed,
      items: <BottomNavigationBarItem>[
        BottomNavigationBarItem(
          icon: new Icon(Icons.home),
          title:  new Text("Dashboard"),
        ),
        BottomNavigationBarItem(
          icon: new Icon(Icons.location_on),
          title: new Text("AllServices"),
        ),
        BottomNavigationBarItem(
          icon: new Icon(Icons.person),
          title:new Text("InformationPage"),
      ],
    );

  }

  @override
  Widget build(BuildContext context) {

    return Scaffold(
      bottomNavigationBar: _bottomNavigationBar(_selectedIndex),
      body: pages[_selectedIndex],
    );
  }

Every thing is correct if user navigates between pages, but AllServices class has two conditions to render two different widgets, I want if user in AllServices page again ontap AllServices bottom navigate AllServices page again rebuild but ontap does not work for same page to rebuild it again how can I solve this problem?

Shojaeddin
  • 111
  • 2
  • 16

1 Answers1

1

Just check the Code below that i have made :


import 'package:flutter/material.dart';
import 'package:wrap_text/dashboard.dart';


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

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Dashboard(),
      ),
    );
  }
}

The below is the helper_widget file where i have made the bootomAppbar :

import 'package:flutter/material.dart';
import 'package:wrap_text/dashboard.dart';


Widget bottomAppBar(context) {
  return BottomAppBar(
    color: Colors.black,
    child: Container(
      child: Row(
        mainAxisSize: MainAxisSize.max,
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        children: <Widget>[
          IconButton(
            iconSize: 30.0,
            icon: Icon(Icons.home,color: Colors.white,),
            onPressed: () {
              Navigator.push(
                context,
                MaterialPageRoute(builder: (context) => Dashboard()),
              );
            },
          ),
          IconButton(
            iconSize: 30.0,
            icon: Icon(Icons.location_on,color: Colors.white,),
            onPressed: () {
              Navigator.push(
                context,
                MaterialPageRoute(builder: (context) => AllServices()),
              );
            },
          ),
          IconButton(
            iconSize: 30.0,
            icon: Icon(Icons.settings,color: Colors.white,),
            onPressed: () {
              Navigator.push(
                context,
                MaterialPageRoute(builder: (context) => InformationPage()),
              );
            },
          ),
        ],
      ),
    ),
  );
}



// this are the different pages for just a sample.
 class AllServices extends StatelessWidget {
   @override
   Widget build(BuildContext context) {
     return Container(

     );
   }
 }



class InformationPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Center(child: Text('Information PAge')),
    );
  }
}

This is the DashBoard Page :

import 'package:flutter/material.dart';
import 'package:wrap_text/helper_widget.dart';

class Dashboard extends StatefulWidget {
  @override
  _DashBoardState createState() => _DashBoardState();
}

class _DashBoardState extends State<Dashboard> {

  @override
  Widget build(BuildContext context) {
     print('fguyhg');
    return Scaffold(
      appBar: AppBar(title: Text('sample'),),
      bottomNavigationBar: bottomAppBar(context),
          body: Container(
        child: Center(child: Text('Dashboard Page')),
      ),
    );
  }
}
Sagar Acharya
  • 3,397
  • 4
  • 12
  • 34