I need to implement the following layout in Flutter.
When the user scrolls, I want the entire layout to scroll (hiding the header and tab bar). However, I can't nest a TabBarView inside a ListView since the TabBarView doesn't have a bounded height and ListViews don't provide a bounded height to their children.
I've already seen these questions, but all of them have unsatisfactory answers for this use case:
It is simple to add scrollable on flutter. Just create SingleChildScrollView widget as a TabBarView and put all the contents as it's child.
Just change Column widget to ListView widget — and that's all. Your container becomes scrollable.
isScrollable property Null safety Whether this tab bar can be scrolled horizontally. If isScrollable is true, then each tab is as wide as needed for its label and the entire TabBar is scrollable. Otherwise each tab gets an equal share of the available space.
On top of Miguel Ruvio's answer replacing the ListView in the body with the TabBarView gets you almost all of the way per D.R.'s comment. I did get some overflow issues when one of my widgets in the was wrapped in a column. Replacing that with ListView per this example
import 'package:flutter/material.dart';
void main() {
runApp(MaterialApp(home: Tabs()));
}
class Tabs extends StatefulWidget {
@override
_RoomTabsState createState() => _RoomTabsState();
}
class _RoomTabsState extends State<Tabs> with TickerProviderStateMixin {
var _scrollViewController;
var _tabController;
@override
void initState() {
super.initState();
_scrollViewController = ScrollController();
_tabController = TabController(vsync: this, length: 2);
}
@override
Widget build(BuildContext context) {
return NestedScrollView(
controller: _scrollViewController,
headerSliverBuilder: (context, bool) => [
SliverAppBar(
bottom: TabBar(
controller: _tabController,
tabs: [
Tab(text: "All"),
Tab(text: "Living room"),
],
),
),
],
body: TabBarView(
controller: _tabController,
children: [
ListView(children: [
Text("test"),
Text("test"),
Text("test"),
Text("test"),
Text("test"),
Text("test"),
Text("test"),
Text("test"),
Text("test"),
Text("test"),
Text("test"),
Text("test"),
Text("test"),
Text("test"),
Text("test"),
Text("test"),
Text("test"),
Text("test"),
Text("test"),
Text("test"),
]),
Text("test"),
],
),
);
}
}
comming from this github issue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With