Reputation: 711
I have one main screen with AppBar
which also has bottomNavigationBar
.
Since the AppBar
is set in the main screen's Scaffold
, I am unable to have custom titles for each subscreen.
As you may see, all sub-screens get the same AppBar
since it's set at the Main Screen level.
Is there a workaround for this?
Upvotes: 0
Views: 145
Reputation: 54365
You can copy paste run full code below
You can create a List<AppBar>
and show with appbarList[_selectedIndex]
code snippet
List<AppBar> appbarList = [
AppBar(
title: const Text('Home'),
),
AppBar(
title: const Text('Basic AppBar'),
actions: <Widget>[
// action button
IconButton(
icon: Icon(Icons.stop),
onPressed: () {},
),
// action button
IconButton(
icon: Icon(Icons.event),
onPressed: () {},
), // overflow menu
],
),
AppBar(
title: const Text('School'),
),
];
...
Scaffold(
appBar: appbarList[_selectedIndex],
working demo
full code
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
/// This Widget is the main application widget.
class MyApp extends StatelessWidget {
static const String _title = 'Flutter Code Sample';
@override
Widget build(BuildContext context) {
return MaterialApp(
title: _title,
home: MyStatefulWidget(),
);
}
}
class MyStatefulWidget extends StatefulWidget {
MyStatefulWidget({Key key}) : super(key: key);
@override
_MyStatefulWidgetState createState() => _MyStatefulWidgetState();
}
class _MyStatefulWidgetState extends State<MyStatefulWidget> {
List<AppBar> appbarList = [
AppBar(
title: const Text('Home'),
),
AppBar(
title: const Text('Basic AppBar'),
actions: <Widget>[
// action button
IconButton(
icon: Icon(Icons.stop),
onPressed: () {},
),
// action button
IconButton(
icon: Icon(Icons.event),
onPressed: () {},
), // overflow menu
],
),
AppBar(
title: const Text('School'),
),
];
int _selectedIndex = 0;
static const TextStyle optionStyle =
TextStyle(fontSize: 30, fontWeight: FontWeight.bold);
static const List<Widget> _widgetOptions = <Widget>[
Text(
'Index 0: Home',
style: optionStyle,
),
Text(
'Index 1: Business',
style: optionStyle,
),
Text(
'Index 2: School',
style: optionStyle,
),
];
void _onItemTapped(int index) {
setState(() {
_selectedIndex = index;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: appbarList[_selectedIndex],
body: Center(
child: _widgetOptions.elementAt(_selectedIndex),
),
bottomNavigationBar: BottomNavigationBar(
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.home),
title: Text('Home'),
),
BottomNavigationBarItem(
icon: Icon(Icons.business),
title: Text('Business'),
),
BottomNavigationBarItem(
icon: Icon(Icons.school),
title: Text('School'),
),
],
currentIndex: _selectedIndex,
selectedItemColor: Colors.amber[800],
onTap: _onItemTapped,
),
);
}
}
Upvotes: 1