Skip to content

Instantly share code, notes, and snippets.

@leighajarett
Created December 1, 2023 02:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leighajarett/40827283791d2748f929fcfa13b66c3a to your computer and use it in GitHub Desktop.
Save leighajarett/40827283791d2748f929fcfa13b66c3a to your computer and use it in GitHub Desktop.
Starting navbar
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Root(),
);
}
}
class Root extends StatefulWidget {
@override
State<Root> createState() => _RootState();
}
class _RootState extends State<Root> {
int currentPageIndex = 0;
// Destinations are the pieces of the bottom bar displayed
// Label does not have to be displayed but is required for accessibility
final destinations = const [
NavigationDestination(
icon: Icon(Icons.home),
selectedIcon: Icon(Icons.home, color: Colors.blue),
label: 'Home',
tooltip: '',
),
NavigationDestination(
icon: Icon(Icons.business),
selectedIcon: Icon(Icons.business, color: Colors.blue),
label: 'Home',
tooltip: '',
)];
final pages = const [Home(destinationIndex: 0), Center(child: Text('Business Page'))];
@override
Widget build(BuildContext context) {
return Scaffold(
bottomNavigationBar: NavigationBar(
onDestinationSelected: (int index) {
setState(() {
currentPageIndex = index;
});
},
backgroundColor: Colors.transparent,
indicatorColor: Colors.transparent,
overlayColor: MaterialStateProperty.all(Colors.transparent),
surfaceTintColor: Colors.transparent,
selectedIndex: currentPageIndex,
labelBehavior: NavigationDestinationLabelBehavior.alwaysHide,
destinations: destinations
),
body: pages[currentPageIndex],
);
}
}
class Home extends StatelessWidget {
final int destinationIndex;
const Home({Key? key, required this.destinationIndex}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Home Page')),
body: Center(
child: ElevatedButton(
child: const Text('Go to List Page'),
onPressed: () {
Navigator.of(context).push(MaterialPageRoute(
builder: (context) => ListPage(),
));
},
),
),
);
}
}
class ListPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('List Page')),
body: const Center(
child: Text('Welcome to List Page'),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment