diff --git a/lib/components/app_animated_switcher.dart b/lib/components/app_animated_switcher.dart new file mode 100644 index 0000000..0d2cc38 --- /dev/null +++ b/lib/components/app_animated_switcher.dart @@ -0,0 +1,42 @@ +import 'package:flutter/material.dart'; +import 'package:shop/constants.dart'; + +/// Generic AnimatedSwitcher wrapper used to keep page transitions consistent. +class AppAnimatedSwitcher extends StatelessWidget { + const AppAnimatedSwitcher({ + super.key, + required this.child, + this.duration = defaultDuration, + this.switchInCurve = Curves.easeOut, + this.switchOutCurve = Curves.easeIn, + this.offset = const Offset(0, 0.04), + }); + + final Widget child; + final Duration duration; + final Curve switchInCurve; + final Curve switchOutCurve; + final Offset offset; + + @override + Widget build(BuildContext context) { + return AnimatedSwitcher( + duration: duration, + switchInCurve: switchInCurve, + switchOutCurve: switchOutCurve, + transitionBuilder: (child, animation) { + final offsetAnimation = + Tween(begin: offset, end: Offset.zero).animate(animation); + + return FadeTransition( + opacity: animation, + child: SlideTransition( + position: offsetAnimation, + child: child, + ), + ); + }, + child: child, + ); + } +} diff --git a/lib/entry_point.dart b/lib/entry_point.dart index f38cf8b..df8e23b 100644 --- a/lib/entry_point.dart +++ b/lib/entry_point.dart @@ -1,5 +1,6 @@ import 'package:flutter/material.dart'; import 'package:flutter_svg/flutter_svg.dart'; +import 'package:shop/components/app_animated_switcher.dart'; import 'package:shop/constants.dart'; import 'package:shop/route/screen_export.dart'; @@ -75,24 +76,7 @@ class _EntryPointState extends State { ), ], ), - // body: _pages[_currentIndex], - body: AnimatedSwitcher( - duration: defaultDuration, - switchInCurve: Curves.easeOut, - switchOutCurve: Curves.easeIn, - transitionBuilder: (child, animation) { - final offsetAnimation = Tween( - begin: const Offset(0, 0.04), - end: Offset.zero, - ).animate(animation); - return FadeTransition( - opacity: animation, - child: SlideTransition( - position: offsetAnimation, - child: child, - ), - ); - }, + body: AppAnimatedSwitcher( child: KeyedSubtree( key: ValueKey(_currentIndex), child: _pages[_currentIndex], diff --git a/lib/screens/main_screen.dart b/lib/screens/main_screen.dart index 98644f7..83a9a5b 100644 --- a/lib/screens/main_screen.dart +++ b/lib/screens/main_screen.dart @@ -1,5 +1,6 @@ import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; +import 'package:shop/components/app_animated_switcher.dart'; import 'package:shop/constants.dart'; import 'package:shop/providers/cart_provider.dart'; import 'package:shop/screens/category/views/categories_list_screen.dart'; @@ -27,24 +28,7 @@ class _MainScreenState extends State { @override Widget build(BuildContext context) { return Scaffold( - body: AnimatedSwitcher( - duration: defaultDuration, - switchInCurve: Curves.easeOut, - switchOutCurve: Curves.easeIn, - transitionBuilder: (child, animation) { - final offsetAnimation = Tween( - begin: const Offset(0, 0.04), - end: Offset.zero, - ).animate(animation); - - return FadeTransition( - opacity: animation, - child: SlideTransition( - position: offsetAnimation, - child: child, - ), - ); - }, + body: AppAnimatedSwitcher( child: KeyedSubtree( key: ValueKey(_selectedIndex), child: _screens[_selectedIndex],