Skip to content

Instantly share code, notes, and snippets.

@lukepighetti
Created June 11, 2019 20:50
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 lukepighetti/346324900a42ca2332f06ab86edb8b2c to your computer and use it in GitHub Desktop.
Save lukepighetti/346324900a42ca2332f06ab86edb8b2c to your computer and use it in GitHub Desktop.
Simple navigator + bottom nav bar example
import 'package:flutter/material.dart';
class LoginScreen extends StatefulWidget {
@override
_LoginScreenState createState() => _LoginScreenState();
}
class _LoginScreenState extends State<LoginScreen> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: RaisedButton(
child: Text("login"),
onPressed: () => Navigator.of(context).pushNamed("home"),
),
),
);
}
}
import 'package:flutter/material.dart';
import 'login.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: LoginScreen(),
routes: {
"home": (_) => MyHomePage(),
},
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key}) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _currentIndex = 0;
_handleTap(int index) => setState(() => _currentIndex = index);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Home"),
),
body: Center(
child: HomeView(currentView: CurrentView.values[_currentIndex]),
),
bottomNavigationBar: BottomNavigationBar(
currentIndex: _currentIndex,
onTap: _handleTap,
items: [
BottomNavigationBarItem(
icon: Icon(Icons.access_alarm),
title: Text("A"),
),
BottomNavigationBarItem(
icon: Icon(Icons.backspace),
title: Text("B"),
),
BottomNavigationBarItem(
icon: Icon(Icons.cached),
title: Text("C"),
),
],
),
);
}
}
enum CurrentView {
a,
b,
c,
}
class HomeView extends StatefulWidget {
final CurrentView currentView;
HomeView({this.currentView});
@override
_HomeViewState createState() => _HomeViewState();
}
class _HomeViewState extends State<HomeView> {
@override
Widget build(BuildContext context) {
switch (widget.currentView) {
case CurrentView.a:
return Text("View A!");
case CurrentView.b:
return Text("View B!");
break;
case CurrentView.c:
return Text("View C!");
break;
default:
return Text("Default!");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment