Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR DART

how to change item when clicked bottom navigation bar flutter

List<Widget> nonAdminWidgets(_BottomNavScaffoldState parent) {
  return <Widget>[
    ProfilePage(parent),
    ClothesPage(),
    ColorsPage(),
  ];
}

List<Widget> adminWidgets(_BottomNavScaffoldState parent) {
  return <Widget>[
    ProfilePage(parent),
    IdeasPage(),
  ];
}

int _currentIndex = 0;

bool isAdmin = true;

List<dynamic> nonAdminNavBars = [
  BottomNavigationBarItem(
    title: Text('Profile'),
    icon: Icon(Icons.face_rounded),
  ),
  BottomNavigationBarItem(
    title: Text('Clothes'),
    icon: Icon(Icons.design_services_rounded),
  ),
  BottomNavigationBarItem(
    title: Text('Colors'),
    icon: Icon(Icons.colorize_rounded),
  ),
];

List<dynamic> adminNavBars = [
  BottomNavigationBarItem(
    title: Text('Profile'),
    icon: Icon(Icons.face_rounded),
  ),
  BottomNavigationBarItem(
    title: Text('Ideas'),
    icon: Icon(Icons.lightbulb_outline_rounded),
  ),
];

class BottomNavScaffold extends StatefulWidget {
  @override
  _BottomNavScaffoldState createState() => _BottomNavScaffoldState();
}

class _BottomNavScaffoldState extends State<BottomNavScaffold> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: isAdmin ? adminWidgets(this)[_currentIndex] : nonAdminWidgets(this)[_currentIndex],
      bottomNavigationBar: BottomNavigationBar(
        type: BottomNavigationBarType.fixed,
        currentIndex: _currentIndex,
        backgroundColor: Colors.orangeAccent,
        selectedItemColor: Colors.white,
        onTap: (value) {
          _currentIndex = value;
          setState(() {});
        },
        items: isAdmin ? adminNavBars : nonAdminNavBars,
      ),
    );
  }
}

class ClothesPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Card(
      color: Colors.white,
      child: Padding(
        padding: EdgeInsets.all(10),
        child: Text(
          "Clothes",
        ),
      ),
    );
  }
}

class ColorsPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Card(
      color: Colors.white,
      child: Padding(
        padding: EdgeInsets.all(10),
        child: Text(
          "Colors",
        ),
      ),
    );
  }
}

class IdeasPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Card(
      color: Colors.white,
      child: Padding(
        padding: EdgeInsets.all(10),
        child: Text(
          "Ideas",
        ),
      ),
    );
  }
}

class ProfilePage extends StatelessWidget {
  _BottomNavScaffoldState parent;

  ProfilePage(this.parent);

  @override
  Widget build(BuildContext context) {
    return RaisedButton(
      onPressed: () {
        isAdmin = !isAdmin;
        _currentIndex = 0;
        parent.setState(() {});
      },
      child: Text("Change"),
    );
  }
}
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #change #item #clicked #bottom #navigation #bar #flutter
ADD COMMENT
Topic
Name
7+8 =