I'm trying to build a simple app structure with an appBar
, tabBar
and 3 tabView
pages. Each of these pages will have a different background color. I would like this background color to cover the full screen (ie, the space taken up by the AppBar, too) . Therefore, I need the color of the AppBar to change as the user switches between tabs.
This is how far I've got:
import 'package:flutter/material.dart';
class Home extends StatefulWidget {
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
Color PrimaryColor = Colors.teal[400];
@override
Widget build(BuildContext context) {
return DefaultTabController(
length: 3,
child: Scaffold(
appBar: AppBar(
elevation: 0,
backgroundColor: PrimaryColor,
bottom: TabBar(
isScrollable: false,
indicatorColor: Colors.white,
indicatorWeight: 5,
onTap: (index) {
setState(() {
switch (index) {
case 0:
PrimaryColor = Colors.teal[400];
break;
case 1:
PrimaryColor = Colors.orange[500];
break;
case 2:
PrimaryColor = Colors.pink[500];
break;
default:
}
});
},
tabs: [
Tab(text: ''),
Tab(text: ''),
Tab(text: ''),
],
)),
body: TabBarView(
children: [
Container(
color: Colors.teal[400],
),
Container(
color: Colors.orange[500],
),
Container(
color: Colors.pink[500],
),
],
),
),
);
}
}
This almost achieves the UI that I want, but the background only changes when the tabBar
buttons are pressed, not when the swipe gesture is used to change between tabs.
Would be really grateful for some guidance on how to rectify this. Thank you.