Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Animated Page Transitions for Bottom Navigation Tabs #191

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 30 additions & 9 deletions lib/pages/main_home.dart
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -23,6 +22,15 @@ class MainHome extends StatefulWidget {
}

class _MainHomeState extends State<MainHome> {
late PageController _pageController;
DateTime? currentBackPressTime;

@override
void initState() {
super.initState();
_pageController = PageController();
}

bool _onBackPressed(bool canPop) {
DateTime now = DateTime.now();
if (currentBackPressTime == null ||
Expand All @@ -40,6 +48,12 @@ class _MainHomeState extends State<MainHome> {
return canPop;
}

@override
void dispose() {
_pageController.dispose();
super.dispose();
}

@override
Widget build(BuildContext context) {
return PopScope(
Expand All @@ -53,13 +67,20 @@ class _MainHomeState extends State<MainHome> {
child: BlocBuilder<IndexCubit, int>(
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<IndexCubit>().changeIndex(index);
},
),
bottomNavigationBar:
BottomNavBar(pageController: _pageController),
);
},
),
Expand Down
13 changes: 12 additions & 1 deletion lib/widgets/navbar/navbar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<BottomNavBar> createState() => _BottomNavBarState();
Expand All @@ -18,6 +23,12 @@ class _BottomNavBarState extends State<BottomNavBar> {
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<IndexCubit>().changeIndex(index);
},
destinations: const [
Expand Down