Skip to content

Instantly share code, notes, and snippets.

@kranfix
Last active April 10, 2020 06:52
Show Gist options
  • Save kranfix/32dca6442c57ebf3e1dbdc02f431072f to your computer and use it in GitHub Desktop.
Save kranfix/32dca6442c57ebf3e1dbdc02f431072f to your computer and use it in GitHub Desktop.
Example of a Flutter app with screens with different appbars
/// Example of a Flutter app with screens with different appbars
/// gist: https://gist.github.com/kranfix/32dca6442c57ebf3e1dbdc02f431072f
/// dartpad: https://dartpad.dev/32dca6442c57ebf3e1dbdc02f431072f
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _selectedIndex = 0;
static const List<Widget> _widgetOptions = <Widget>[
Screen1(),
Screen2(),
];
void _onItemTapped(int index) {
setState(() {
_selectedIndex = index;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: _widgetOptions.elementAt(_selectedIndex),
bottomNavigationBar: BottomNavigationBar(
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.home),
title: Text('Home'),
),
BottomNavigationBarItem(
icon: Icon(Icons.business),
title: Text('Business'),
),
],
currentIndex: _selectedIndex,
selectedItemColor: Colors.amber[800],
onTap: _onItemTapped,
),
);
}
}
class Screen1 extends StatelessWidget {
const Screen1();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Screen 1's appbar"),
bottom: PreferredSize(
preferredSize: const Size.fromHeight(48.0),
child: Theme(
data: Theme.of(context).copyWith(accentColor: Colors.white),
child: Container(
height: 48.0,
alignment: Alignment.center,
child: Text("This is the bottom of the appbar"),
),
),
),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'Screen 1',
),
],
),
),
);
}
}
class Screen2 extends StatelessWidget {
const Screen2();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Screen 2's appbar"),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'Screen 2',
),
],
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment