How to make page transition when use bottomNavigationBar in flutter

262 views Asked by At

I use a basic bottomNavigationBar and now it shows just page without any transition,i want to use page transition when tap different bottomNavigationBars item.

4

There are 4 answers

2
Jungwon On

Use IndexedStack Widget

int _currentIndex = 0;

@override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: IndexedStack(
          index: _currentIndex,
          children: const [
            HomePage(),
            SettingsPage(),
          ],
        ),
      ),
      bottomNavigationBar: BottomNavigationBar(
        currentIndex: _currentIndex,
        onTap: (int index) => setState(() => _currentIndex = index),
        items: [
          BottomNavigationBarItem(icon: Icon(Icons.home), label: 'Home'),
          BottomNavigationBarItem(icon: Icon(Icons.settings), label: 'Settings'),
        ],
      ),
    );
  }
1
anKii On

use PageView or TabView Check this article

0
AlishanMuhammadAmin On
 List pages = [
    HomeScreen(),
    DiscoverScreen(),
    Container(),
    MessageScreen(),
    ProfileScreen(),
  ];
Scaffold(
      body: pages.elementAt(current!),
 bottomNavigationBar: BottomNavigationBar(
          currentIndex: widget.current!,
          unselectedItemColor: Colors.black,
          selectedItemColor: Colors.green,
          showSelectedLabels: true,
          showUnselectedLabels: true,
          onTap: (val) {
            setState(() {
              current = val;
            });
2
Khalid Hassan On

Bottom Nav Transition With Persisting Ui


import 'package:flutter/material.dart';

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

  @override
  State<BottomNavTransitionWithPersistingUi> createState() => _BottomNavTransitionWithPersistingUiState();
}

class _BottomNavTransitionWithPersistingUiState extends State<BottomNavTransitionWithPersistingUi> {
  late PageController _pageController;
  late List<Widget> _tabBody;

  int _currentIndex = 0;

  @override
  void initState() {
    super.initState();
    _pageController = PageController(
      initialPage: 0,
      keepPage: true, // for ui state
    );
    _tabBody = [
      const NavHomeView(key: PageStorageKey("NavHomeView")),
      const NavMoreView(key: PageStorageKey("NavMoreView")),
    ];
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      bottomNavigationBar: _bottomNavigationBarItems,
      body: PageView(
        controller: _pageController,
        onPageChanged: _onPageChanged,
        children: _tabBody,
      ),
    );
  }

  Widget get _bottomNavigationBarItems => BottomNavigationBar(
        type: BottomNavigationBarType.fixed,
        items: const <BottomNavigationBarItem>[
          BottomNavigationBarItem(
            icon: Icon(Icons.home),
            label: 'Home',
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.menu),
            label: 'More',
          ),
        ],
        currentIndex: _currentIndex,
        selectedItemColor: Colors.red,
        elevation: 0.1,
        unselectedItemColor: Colors.grey,
        enableFeedback: true,
        onTap: _onNavBarTapped,
      );

  void _onNavBarTapped(int index) {
    setState(() {
      _currentIndex = index;
    });

    _pageController.animateToPage(
      index,
      duration: const Duration(milliseconds: 500),
      curve: Curves.easeOut,
    );
  }

  void _onPageChanged(int index) {
    setState(() {
      _currentIndex = index;
    });
  }
}

class NavHomeView extends StatelessWidget {
  const NavHomeView({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ListView(
      children: [
        Container(
          height: 500,
          color: Colors.purple,
        ),
        Container(
          height: 500,
          color: Colors.blue,
        ),
        Container(
          height: 500,
          color: Colors.grey,
        ),
      ],
    );
  }
}

class NavMoreView extends StatelessWidget {
  const NavMoreView({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ListView(
      children: [
        Container(
          height: 500,
          color: Colors.red,
        ),
        Container(
          height: 500,
          color: Colors.green,
        ),
        Container(
          height: 500,
          color: Colors.amber,
        ),
      ],
    );
  }
}