In course of my exploration of the flutter framework I came across a problem, I didn't know how to solve. Can I use TabBar in combination with TabBarView as a child of other widgets?
Asked
Active
Viewed 3.1k times
2 Answers
51
Turns out it works. This is the relevant code snippet:
new Container(
decoration: new BoxDecoration(color: Theme.of(context).primaryColor),
child: new TabBar(
controller: _controller,
tabs: [
new Tab(
icon: const Icon(Icons.home),
text: 'Address',
),
new Tab(
icon: const Icon(Icons.my_location),
text: 'Location',
),
],
),
),
new Container(
height: 80.0,
child: new TabBarView(
controller: _controller,
children: <Widget>[
new Card(
child: new ListTile(
leading: const Icon(Icons.home),
title: new TextField(
decoration: const InputDecoration(hintText: 'Search for address...'),
),
),
),
new Card(
child: new ListTile(
leading: const Icon(Icons.location_on),
title: new Text('Latitude: 48.09342\nLongitude: 11.23403'),
trailing: new IconButton(icon: const Icon(Icons.my_location), onPressed: () {}),
),
),
],
),
),
And this is a working example:
import 'package:flutter/material.dart';
void main() {
runApp(new MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
home: new MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> with SingleTickerProviderStateMixin {
TabController _controller;
@override
void initState() {
super.initState();
_controller = new TabController(length: 2, vsync: this);
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text('TestProject'),
),
body: new ListView(
children: <Widget>[
new Card(
child: new ListTile(
title: const Text('Some information'),
),
),
new Container(
decoration: new BoxDecoration(color: Theme.of(context).primaryColor),
child: new TabBar(
controller: _controller,
tabs: [
new Tab(
icon: const Icon(Icons.home),
text: 'Address',
),
new Tab(
icon: const Icon(Icons.my_location),
text: 'Location',
),
],
),
),
new Container(
height: 80.0,
child: new TabBarView(
controller: _controller,
children: <Widget>[
new Card(
child: new ListTile(
leading: const Icon(Icons.home),
title: new TextField(
decoration: const InputDecoration(hintText: 'Search for address...'),
),
),
),
new Card(
child: new ListTile(
leading: const Icon(Icons.location_on),
title: new Text('Latitude: 48.09342\nLongitude: 11.23403'),
trailing: new IconButton(icon: const Icon(Icons.my_location), onPressed: () {}),
),
),
],
),
),
new Card(
child: new ListTile(
title: const Text('Some more information'),
),
),
new RaisedButton(
color: Theme.of(context).primaryColor,
onPressed: () {},
child: const Text(
'Search for POIs',
style: const TextStyle(color: Colors.white),
),
),
],
),
);
}
}

Rainer Wittmann
- 7,528
- 4
- 20
- 34
-
10Is there a way to use a tabbarview inside a container without having to set a specific height? I'm getting a stuck because the children in my tabbarviews are variable height. – Negora Jun 07 '18 at 19:55
-
@Negora Ever find an answer? – htxryan Dec 02 '18 at 03:02
-
it's possible but I lost my source somehow :( it can be done! – Negora Feb 01 '19 at 01:47
-
1@Negora true we don't want container height there...I'm still struggling how to get rid off it – stuckedunderflow Feb 12 '19 at 03:59
-
1You can replace `Container` with `Expanded`. – Suroor Ahmmad Jul 27 '19 at 09:14
6
If you have variable height in TabBarView
, you can use Expanded
.
body:
...
new Expanded(
child: new TabBarView(
controller: _controller,
children: <Widget>[
new Card(
child: new ListTile(
leading: const Icon(Icons.home),
title: new TextField(
decoration: const InputDecoration(hintText: 'Search for address...'),
),
),
),//Card
....
....
],//Widget
),//TabBarView
),//Expanded

Suroor Ahmmad
- 1,110
- 4
- 23
- 33