From ae536750e3c9dcce5e1ee003456f7679d35f6f9d Mon Sep 17 00:00:00 2001 From: Lalit Sahu Date: Tue, 25 Jun 2024 21:29:55 +0530 Subject: [PATCH] adding_animation_tab_switching --- lib/pages/main_home.dart | 39 ++++++++++++++++++++++++++-------- lib/widgets/navbar/navbar.dart | 13 +++++++++++- 2 files changed, 42 insertions(+), 10 deletions(-) diff --git a/lib/pages/main_home.dart b/lib/pages/main_home.dart index f9bc566..3e1c819 100644 --- a/lib/pages/main_home.dart +++ b/lib/pages/main_home.dart @@ -1,14 +1,13 @@ -import 'package:adaptive_theme/adaptive_theme.dart'; import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:fluttertoast/fluttertoast.dart'; import 'package:learn/cubit/index_cubit.dart'; -import 'package:learn/main.dart'; import 'package:learn/pages/about.dart'; import 'package:learn/pages/explore/explore.dart'; import 'package:learn/pages/favorite.dart'; import 'package:learn/pages/home.dart'; import 'package:learn/widgets/navbar/navbar.dart'; +import 'package:adaptive_theme/adaptive_theme.dart'; class MainHome extends StatefulWidget { final AdaptiveThemeMode? savedThemeMode; @@ -23,6 +22,15 @@ class MainHome extends StatefulWidget { } class _MainHomeState extends State { + late PageController _pageController; + DateTime? currentBackPressTime; + + @override + void initState() { + super.initState(); + _pageController = PageController(); + } + bool _onBackPressed(bool canPop) { DateTime now = DateTime.now(); if (currentBackPressTime == null || @@ -40,6 +48,12 @@ class _MainHomeState extends State { return canPop; } + @override + void dispose() { + _pageController.dispose(); + super.dispose(); + } + @override Widget build(BuildContext context) { return PopScope( @@ -53,13 +67,20 @@ class _MainHomeState extends State { child: BlocBuilder( builder: (context, index) { return Scaffold( - body: const [ - MyHomePage(), - ExplorePage(), - FavoritePage(), - AboutPage(), - ][index], - bottomNavigationBar: const BottomNavBar(), + body: PageView( + controller: _pageController, + children: const [ + MyHomePage(), + ExplorePage(), + FavoritePage(), + AboutPage(), + ], + onPageChanged: (index) { + context.read().changeIndex(index); + }, + ), + bottomNavigationBar: + BottomNavBar(pageController: _pageController), ); }, ), diff --git a/lib/widgets/navbar/navbar.dart b/lib/widgets/navbar/navbar.dart index fa64c4b..2a1633b 100644 --- a/lib/widgets/navbar/navbar.dart +++ b/lib/widgets/navbar/navbar.dart @@ -3,7 +3,12 @@ import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:learn/cubit/index_cubit.dart'; class BottomNavBar extends StatefulWidget { - const BottomNavBar({super.key}); + final PageController pageController; + + const BottomNavBar({ + super.key, + required this.pageController, + }); @override State createState() => _BottomNavBarState(); @@ -18,6 +23,12 @@ class _BottomNavBarState extends State { return NavigationBar( selectedIndex: currentPageIndex, onDestinationSelected: (index) { + // Animate to the selected page + widget.pageController.animateToPage( + index, + duration: const Duration(milliseconds: 300), + curve: Curves.easeInOut, + ); context.read().changeIndex(index); }, destinations: const [