Do I have to use Tabbar, TabBarView and TabbarController only with Scaffold and AppBar ?
I want each widget that shows some content to have 2-3 tabs to switch a view (so I want tabs inside Column or Container). I don't want use Scaffold for that, because my my app is designed in a way, that I would have to nest Scaffolds what is not recommended by documentation. So I prepared something like this:
@immutable
class HomeScreen extends StatelessWidget {
const HomeScreen({Key? key}) : super(key: key);
final List<Tab> _homeScreenTabs = const <Tab>[
Tab(text: 'Tab1', icon: Icon(Icons.camera)),
Tab(text: 'Tab2', icon: Icon(Icons.camera)),
Tab(text: 'Tab3', icon: Icon(Icons.camera)),
];
@override
Widget build(BuildContext context) {
return DefaultTabController(
length: _homeScreenTabs.length,
child: SafeArea(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
TabBar(
labelColor: Colors.blue,
tabs: _homeScreenTabs,
),
Expanded(
child: TabBarView(
children: _homeScreenTabs.map((Tab tab) {
final String label = tab.text?.toLowerCase() ?? "";
return Center(
child: Text(
'This is the home $label',
style:
const TextStyle(color: Colors.black87, fontSize: 18),
),
);
}).toList(),
),
),
],
),
),
);
}
}
It works but question is: is it correct to use TabBar in that way ? Documentation says only about using inside Scaffold.