Skip to content

Instantly share code, notes, and snippets.

@ricardogottheil
Created April 24, 2024 01:00
Show Gist options
  • Save ricardogottheil/a44f1a0067f7aba44205401a537b1671 to your computer and use it in GitHub Desktop.
Save ricardogottheil/a44f1a0067f7aba44205401a537b1671 to your computer and use it in GitHub Desktop.
Ejemplo 4 - Integración con Páginas Dinámicas - 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: DynamicPagesNavigation(),
);
}
}
class DynamicPagesNavigation extends StatefulWidget {
@override
_DynamicPagesNavigationState createState() => _DynamicPagesNavigationState();
}
class _DynamicPagesNavigationState extends State<DynamicPagesNavigation> {
int _selectedIndex = 0;
PageController _pageController = PageController();
@override
void initState() {
super.initState();
_pageController = PageController(initialPage: 0);
}
@override
void dispose() {
_pageController.dispose();
super.dispose();
}
void _onPageChanged(int page) {
setState(() {
_selectedIndex = page;
});
}
void _onItemTapped(int index) {
_pageController.animateToPage(index,
duration: Duration(milliseconds: 300), curve: Curves.easeIn);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: PageView(
controller: _pageController,
onPageChanged: _onPageChanged,
children: <Widget>[
Center(child: Text('Inicio')),
Center(child: Text('Negocios')),
Center(child: Text('Escuela')),
],
),
bottomNavigationBar: BottomNavigationBar(
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.home),
label: 'Inicio',
),
BottomNavigationBarItem(
icon: Icon(Icons.business),
label: 'Negocios',
),
BottomNavigationBarItem(
icon: Icon(Icons.school),
label: 'Escuela',
),
],
currentIndex: _selectedIndex,
selectedItemColor: Colors.blue[800],
onTap: _onItemTapped,
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment