Skip to content

Instantly share code, notes, and snippets.

@ricardogottheil
Created April 24, 2024 01:22
Show Gist options
  • Save ricardogottheil/218b1d9e0548af58ad5ff1530bbbf235 to your computer and use it in GitHub Desktop.
Save ricardogottheil/218b1d9e0548af58ad5ff1530bbbf235 to your computer and use it in GitHub Desktop.
Ejemplo 6 - Incorporación de Animaciones Personalizadas - BottomNavigationBar
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: AnimatedNavigationBar(),
);
}
}
class AnimatedNavigationBar extends StatefulWidget {
@override
_AnimatedNavigationBarState createState() => _AnimatedNavigationBarState();
}
class _AnimatedNavigationBarState extends State<AnimatedNavigationBar> {
int _selectedIndex = 0;
final List<Widget> _pages = [
Center(child: Text('Página 1')),
Center(child: Text('Página 2')),
Center(child: Text('Página 3')),
];
Widget _buildOffstageNavigator(int index) {
return Offstage(
offstage: _selectedIndex != index,
child: TickerMode(
enabled: _selectedIndex == index,
child: FadeTransition(
opacity: _selectedIndex == index
? AlwaysStoppedAnimation(1)
: AlwaysStoppedAnimation(0),
child: _pages[index],
),
),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
children:
List<Widget>.generate(3, (index) => _buildOffstageNavigator(index)),
),
bottomNavigationBar: BottomNavigationBar(
items: [
BottomNavigationBarItem(icon: Icon(Icons.looks_one), label: "Uno"),
BottomNavigationBarItem(icon: Icon(Icons.looks_two), label: "Dos"),
BottomNavigationBarItem(icon: Icon(Icons.looks_3), label: "Tres"),
],
currentIndex: _selectedIndex,
onTap: (index) {
setState(() {
_selectedIndex = index;
});
},
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment