Created
November 16, 2023 18:19
-
-
Save leighajarett/a3898aa7265f6ed59a9e5fb211b85ce7 to your computer and use it in GitHub Desktop.
Icon scales in BottomNavigationBar
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
void main() => runApp(MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
home: MyHomePage(), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
@override | |
_MyHomePageState createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> with TickerProviderStateMixin { | |
int _selectedIndex = 0; | |
late List<AnimationController> _controllers; | |
late List<Animation<double>> _animations; | |
@override | |
void initState() { | |
super.initState(); | |
_controllers = List.generate(4, (index) => AnimationController( | |
vsync: this, | |
duration: Duration(milliseconds: 200), | |
)); | |
_animations = _controllers.map((controller) => Tween(begin: 1.0, end: 1.1).animate(controller)).toList(); | |
} | |
@override | |
void dispose() { | |
for (var controller in _controllers) { | |
controller.dispose(); | |
} | |
super.dispose(); | |
} | |
void _onItemTapped(int index) { | |
setState(() { | |
_controllers[_selectedIndex].reverse(); | |
_selectedIndex = index; | |
_controllers[_selectedIndex].forward(); | |
}); | |
} | |
Widget _buildPageContent() { | |
switch (_selectedIndex) { | |
case 0: | |
return Text('Home Page'); | |
case 1: | |
return Text('Search Page'); | |
case 2: | |
return Text('Notifications Page'); | |
case 3: | |
return Text('Profile Page'); | |
default: | |
return Text('Page not found'); | |
} | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar(title: Text('Animated Navigation Bar')), | |
body: Center(child: _buildPageContent()), | |
bottomNavigationBar: BottomNavigationBar( | |
items: <BottomNavigationBarItem>[ | |
BottomNavigationBarItem(icon: AnimatedIcon(index: 0, iconData: Icons.home), label: ''), | |
BottomNavigationBarItem(icon: AnimatedIcon(index: 1, iconData: Icons.search), label: ''), | |
BottomNavigationBarItem(icon: AnimatedIcon(index: 2, iconData: Icons.notifications), label: ''), | |
BottomNavigationBarItem(icon: AnimatedIcon(index: 3, iconData: Icons.person), label: ''), | |
], | |
currentIndex: _selectedIndex, | |
onTap: _onItemTapped, | |
), | |
); | |
} | |
} | |
class AnimatedIcon extends StatelessWidget { | |
final int index; | |
final IconData iconData; | |
AnimatedIcon({required this.index, required this.iconData}); | |
@override | |
Widget build(BuildContext context) { | |
final _MyHomePageState state = context.findAncestorStateOfType<_MyHomePageState>()!; | |
return ScaleTransition( | |
scale: state._animations[index], | |
alignment: Alignment.center, | |
child: Icon( | |
iconData, | |
size: 30, | |
color: Colors.black, | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment