Flutter scrolling horizontal widget in TabBar - flutter

I want to scroll a widget in a TabBar horizontal, and when I reach the end of the widget I want to scroll to the next tab. Currently I can only scroll to the next tab if I swipe outside of the Scroll-Widget.
What is the best option in flutter to scroll directly to the next tab?
My current code:
class _ScrollTestPageState extends State<ScrollTestPage> with SingleTickerProviderStateMixin {
TabController _tabController;
#override
void initState() {
super.initState();
_tabController = TabController(vsync: this, length: 3);
_tabController.addListener(() {});
}
#override
void dispose() {
super.dispose();
_tabController.dispose();
}
#override
Widget build(BuildContext context) {
return DefaultTabController(
length: 3,
child: Scaffold(
appBar: AppBar(
bottom: TabBar(
indicatorColor: Colors.red,
controller: _tabController,
tabs: [
Tab(text: 'Page 1'),
Tab(text: 'Page 2'),
Tab(text: 'Page 3'),
],
),
),
body: TabBarView(
controller: _tabController,
children: [
Center(
child: Icon(Icons.ac_unit),
),
// Scroll-Tab
Center(
child: Container(
color: Colors.grey,
child: SingleChildScrollView(
scrollDirection: Axis.horizontal,
physics: PageScrollPhysics(),
child: Row(
children: [
FlutterLogo(
size: 600,
),
],
),
),
),
),
Center(
child: Icon(Icons.cached_rounded),
),
],
),
),
);
}
}

Related

How to Move This TabBar Position from Upper to Middle One in Flutter?

I was trying to move the TabBar based on picture below. I wanted to move its position right under the piano picture in the middle. I am confused of which place should I put the TabBar. Here is my complete codes:
class MusicAmbient extends StatefulWidget {
const MusicAmbient({super.key});
#override
State<MusicAmbient> createState() => _MusicAmbientState();
}
class _MusicAmbientState extends State<MusicAmbient> with SingleTickerProviderStateMixin {
late final TabController controller = TabController(length: 2, vsync: this);
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
bottom: TabBar(
controller: controller,
tabs: const [
Tab(text: "Epic"),
Tab(text: "Relax"),
],
),
),
body: Container(
color: Colors.black,
child: SafeArea(
child: Container(
height: MediaQuery.of(context).size.height,
color: const Color.fromARGB(115, 55, 55, 55),
child: Stack(
children: [
//Piano Background & Tabs
Align(
alignment: Alignment.topLeft,
child: Wrap(
children: [
ImageButton(path: bgamb, callback: () => Get.back(), type: IconType.NONE),
Container(
height: MediaQuery.of(context).size.height,
color: const Color.fromARGB(115, 55, 55, 55),
child: TabBarView(
controller: controller,
children: [
MusicEpic(),
MusicRelax(),
],
),
),
],
),
),
],
),
),
),
),
);
}
}
Is there a way to achieve it correctly in Flutter? Any tips and tricks will be appreciated.

Flutter implement tab

I'm trying to make a page design like this:
But I'm having difficulties on implementing the tab bar. When the user click on 'Detail', then the detail view is showed under the tab bar and so on. Is there any widget to use?
try CupertinoSlidingSegmentedControl:
child: CupertinoSlidingSegmentedControl(
children: {
0: Text("FLIGHTS"),
1: Text("TRAINS"),
2: Text("HOTELS")
},
onValueChanged: (value)
{
selectedValue = value;
setState(() {
});
},
groupValue: selectedValue,
),
import 'package:flutter/material.dart';
class TabBarDemo extends StatelessWidget {
const TabBarDemo({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return MaterialApp(
home: DefaultTabController(
length: 3,
child: Scaffold(
appBar: AppBar(
title: const Text('Tabs Demo'),
),
body: SingleChildScrollView(
child: Column(
children: [
Container(
height: 400,
child: const TabBarView(
children: [
Icon(Icons.directions_car),// create image widget here using listview builder
Icon(Icons.directions_transit),
Icon(Icons.directions_bike),
],
),
),
Container(
height: 80,
color: Colors.blue,
child: const TabBar(
tabs: [
Tab(icon: Icon(Icons.directions_car)),
Tab(icon: Icon(Icons.directions_transit)),
Tab(icon: Icon(Icons.directions_bike)),
],
),
),
Container(
height: 400,
child: TabBarView(
children: [
Container(color: Colors.red,), // create data widgets here using listview builder
Container(color: Colors.green,),
Container(color: Colors.yellow,),
],
),
),
]
),
),
),
),
);
}
}

How to set the indicator color to more than one tab at a time in Tab bar view flutter?

I have a tab bar view with 3 tabs, Email, Profile, Complete. Now there is a button in Email like such:
1. Email Tab
widget.theTabController.animateTo(
(widget.theTabController.index + 1),
);
When this button is clicked the tab changes and so does the indicator color changes.
But is there any way I could keep the indicator color of the Email tab same as the Profile tab like such:
Below is the code of my tab bar view.
Tabbar view
class SignupEmail extends StatefulWidget {
#override
_SignupEmailState createState() => _SignupEmailState();
}
class _SignupEmailState extends State<SignupEmail>
with SingleTickerProviderStateMixin {
bool entryPermission = false;
final List<Tab> myTabs = <Tab>[
new Tab(text: 'EMAIL'),
new Tab(text: 'PROFILE'),
new Tab(text: 'COMPLETE'),
];
TabController _tabController;
void initState() {
super.initState();
_tabController = new TabController(vsync: this, length: myTabs.length);
_tabController.addListener(() {
print("Selected Index: " + _tabController.index.toString());
});
}
#override
void dispose() {
_tabController.dispose();
super.dispose();
}
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
backgroundColor: Colors.white,
body: SingleChildScrollView(
child: Padding(
padding:
const EdgeInsets.only(left: 16, right: 16, top: 50, bottom: 0),
child: Container(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
MainTitle(),
Container(
child: DefaultTabController(
length: myTabs.length,
child: Column(
children: [
Container(
// height: 50,
width: double.infinity,
child: IgnorePointer(
child: TabBar(
controller: _tabController,
unselectedLabelColor:
Color.fromRGBO(0, 0, 0, 0.87),
labelColor: Color(0xff9E9E9E),
indicatorColor: Color.fromRGBO(98, 0, 238, 1),
tabs: myTabs,
),
),
),
SizedBox(
height: 28,
),
Container(
width: double.infinity,
height: 500,
child: TabBarView(
// physics: NeverScrollableScrollPhysics(),
controller: _tabController,
children: [
EmailTab(
theTabController: _tabController,
),
ProfileTab(
theTabController: _tabController,
),
CompleteEmail()
]),
),
],
)),
),
],
),
),
),
),
),
);
}
}
I think the only way is to build a tab navigation by yourself. I recently had a similar issue, when I wanted the tab color to match the screen color.
I simply created a stack with
Positioned(
left: safeAreaPos.dx,
width: safeAreaPos.dx + safeAreaSize.width,
bottom: 0,
child: Row(
children: [
for (var i = 0; i < 4; i++)
TabButton(i, context)
],
and render my tab
Widget TabButton(int tabIndex, BuildContext context) {
final List<String> labels = ['A', 'B', 'C', 'D'];
final List<double> labelSizes = [12, 16, 16, 12];
return SizedBox(
height: 50, width: safeAreaSize.width / 4,
child:
FlatButton(
color: bgTabColors[tabIndex],
padding: EdgeInsets.all(0),
textColor: CupertinoColors.white,
child: Text(labels[tabIndex], style: TextStyle(fontSize: labelSizes[tabIndex]),),
onPressed: () =>
BlocProvider.of<ProjectKanbanBloc>(context).add(ProjectKanbanTabRequested(tabIndex)),
)
);
}
}

Display TabBar in body of scaffold

I'm want to display a tabbar as it is used with the appbar, but in the middle of the scaffold body. Is it possible?
I have this code, but the TabBarView breaks the ui. If I comment the TabBarView, the TabBars are displayed correctly. What is wrong with the code? In case it's possible to use it this way..
#override
void initState() {
super.initState();
_tabController = TabController(length: 2, vsync: this);
}
#override
Widget build(BuildContext context) {
return Scaffold(
body: ListView(
children: <Widget>[
Text('Some other random content'),
TabBar(
controller: _tabController,
isScrollable: true,
tabs: <Widget>[
Tab(
child: Text(
'Posts',
style: TextStyle(
fontSize: 18.0,
color: Colors.black87,
),
),
),
Tab(
child: Text(
'Fotos',
style: TextStyle(
fontSize: 18.0,
color: Colors.black87,
),
),
),
],
),
TabBarView(
controller: _tabController,
children: <Widget>[
Center(
child: Text("User"),
),
Center(
child: Text("Email"),
),
],
),
],
),
);
}
Just Change ListView to Column and add SingleChildScrollView
SingleChildScrollView(
child: Column(
children: <Widget>[]
),
)
Hey i guess it's already too late but maybe it helps others.
If you just want to have your tabbar e.g. in the middle of the screen to divide your screen in 2 sections (top: content, bottom: tabbar) you can place your scaffold inside a column, e.g. following:
#override
Widget build(BuildContext context) {
return Column(
children: [
Expanded(
flex: 1,
child: Center(
child: Text("Header"),
),
),
Expanded(
flex: 1,
child: DefaultTabController(
length: 3,
child: Scaffold(
appBar: AppBar(
flexibleSpace: TabBar(
tabs: [
Tab(text: "First"),
Tab(text: "Second"),
Tab(text: "Third"),
],
),
),
body: TabBarView(
children: [
Center(child: Text("First screen")),
Center(child: Text("Second screen")),
Center(child: Text("Third screen")),
],
),
),
),
),
],
);
}
Let me know if this fits your needs / if you need further help :-)

how to implement a sliverAppBar with a tabBar

the flutter document show a demo for SliverAppBar + TabBar + TabBarView with ListView use NestedScrollView, and it's a bit complex, so I wonder is there a simply and clear way to implement it. I tried this:
CustomScrollView
slivers:
SliverAPPBar
bottom: TabBar
TabBarView
children: MyWidget(list or plain widget)
got error:
flutter: The following assertion was thrown building Scrollable(axisDirection: right, physics:
flutter: A RenderViewport expected a child of type RenderSliver but received a child of type _RenderExcludableScrollSemantics.
flutter: RenderObjects expect specific types of children because they coordinate with their children during layout and paint. For example, a RenderSliver cannot be the child of a RenderBox because a RenderSliver does not understand the RenderBox layout protocol.
and
flutter: Another exception was thrown: 'package:flutter/src/widgets/framework.dart': Failed assertion: line 3497 pos 14: 'owner._debugCurrentBuildTarget == this': is not true.
HERE IS MY CODE:
import 'package:flutter/material.dart';
main(List<String> args) {
runApp(MyScrollTabListApp());
}
class MyScrollTabListApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(title: "aa", home: MyScrollTabListHomePage());
}
}
class MyScrollTabListHomePage extends StatefulWidget {
#override
MyScrollTabListHomePageState createState() {
return new MyScrollTabListHomePageState();
}
}
class MyScrollTabListHomePageState extends State<MyScrollTabListHomePage>
with SingleTickerProviderStateMixin {
final int _listItemCount = 300;
final int _tabCount = 8;
TabController _tabController;
#override
void initState() {
_tabController = TabController(length: _tabCount, vsync: this);
super.initState();
}
#override
Widget build(BuildContext context) {
return Scaffold(
body: CustomScrollView(
slivers: <Widget>[
SliverAppBar(
expandedHeight: 240.0,
title: Text("Title"),
pinned: true,
bottom: TabBar(
controller: _tabController,
isScrollable: true,
tabs: List<Tab>.generate(_tabCount, (int i) {
return Tab(text: "TAB$i");
}),
),
),
TabBarView(
controller: _tabController,
children: List<Widget>.generate(_tabCount, (int i) {
return Text('line $i');
}),
),
],
),
);
}
}
and for the official demo, it use struct like this
DefaultTabController
NestedScrollView
headerSliverBuilder
SliverOverlapAbsorber
handle
SliverAppBar
TabBarView
CustomScrollView
SliverOverlapInjector
handle
SliverPadding
Use NestedScrollView, here is the working code.
#override
Widget build(BuildContext context) {
return Scaffold(
body: DefaultTabController(
length: 2,
child: NestedScrollView(
headerSliverBuilder: (context, value) {
return [
SliverAppBar(
bottom: TabBar(
tabs: [
Tab(icon: Icon(Icons.call), text: "Call"),
Tab(icon: Icon(Icons.message), text: "Message"),
],
),
),
];
},
body: TabBarView(
children: [
CallPage(),
MessagePage(),
],
),
),
),
);
}
Here is an example for TabView with SilverAppBar
class SilverAppBarWithTabBarScreen extends StatefulWidget {
#override
_SilverAppBarWithTabBarState createState() => _SilverAppBarWithTabBarState();
}
class _SilverAppBarWithTabBarState extends State<SilverAppBarWithTabBarScreen>
with SingleTickerProviderStateMixin {
TabController controller;
#override
void initState() {
super.initState();
controller = new TabController(length: 3, vsync: this);
}
#override
Widget build(BuildContext context) {
return new Scaffold(
body: new CustomScrollView(
slivers: <Widget>[
new SliverAppBar(
title: Text("Silver AppBar With ToolBar"),
pinned: true,
expandedHeight: 160.0,
bottom: new TabBar(
tabs: [
new Tab(text: 'Tab 1'),
new Tab(text: 'Tab 2'),
new Tab(text: 'Tab 3'),
],
controller: controller,
),
),
new SliverList(
new SliverFillRemaining(
child: TabBarView(
controller: controller,
children: <Widget>[
Text("Tab 1"),
Text("Tab 2"),
Text("Tab 3"),
],
),
),
],
),
);
}
}
Yeah. You can use NestedScrollView To Achieve Tabs. Here is Some Addition Code To That.
class AppView extends StatelessWidget {
final double _minValue = 8.0;
#override
Widget build(BuildContext context) {
final textTheme = Theme.of(context).textTheme;
return Scaffold(
appBar: MyAppBar(),
drawer: DrawerDialog(),
body: DefaultTabController(
length: 3,
child: SafeArea(
child: NestedScrollView(
body: TabBarView(
children: [Text("Page 1"), Text("Page 2"), Text("Page 3")],
),
headerSliverBuilder:
(BuildContext context, bool innerBoxIsScrolled) => [
SliverPadding(
padding: EdgeInsets.all(_minValue * 2.5),
sliver: SliverToBoxAdapter(
child: Text(
"Hiding Header",
style: textTheme.headline6,
textAlign: TextAlign.center,
),
),
),
SliverAppBar(
backgroundColor: Colors.grey[100],
pinned: true,
elevation: 12.0,
leading: Container(),
titleSpacing: 0.0,
toolbarHeight: 10,
bottom: TabBar(tabs: [
Tab(
child: Text(
"All",
style: textTheme.subtitle2,
),
),
Tab(
child: Text(
"Categories",
style: textTheme.subtitle2,
),
),
Tab(
child: Text(
"Upcoming",
style: textTheme.subtitle2,
),
),
]),
),
],
),
),
),
);
}
}
You can Also Implement it by Providing _tabController to Both TabBar() and TabBarView So It will bind.
And For the children of TabBarView if you're using ListView then give it physics: NeverScrollablePhysics() so it won't move, kindly not You have to give dynamic height to the Container of ListView So it will load all Children's.
class _HomeState extends State<Home> with SingleTickerProviderStateMixin {
TabController _tabController;
#override
void initState() {
_tabController = TabController(length: 2, vsync: this);
super.initState();
}
#override
Widget build(BuildContext context) {
return Scaffold(
body: CustomScrollView(
slivers: [
SliverAppBar(
floating: true,
expandedHeight: 50,
title: Column(
children: [
Row(
children: [
Text('Hello, User'),
Spacer(),
InkWell(
child: Icon(Icons.map_rounded),
),
],
),
],
),
),
SliverList(
delegate: SliverChildListDelegate([
_tabSection(context),
])),
],
));
}
Widget _tabSection(BuildContext context) {
final height = MediaQuery.of(context).size.height;
final width = MediaQuery.of(context).size.width;
double mainAxisHeight = height > width ? height : width;
return DefaultTabController(
length: 2,
child: Column(mainAxisSize: MainAxisSize.min, children: <Widget>[
Container(
height: 48,
decoration: BoxDecoration(
color: Colors.green,
borderRadius: BorderRadius.only(
bottomRight: Radius.circular(10),
bottomLeft: Radius.circular(10))),
child: TabBar(
indicatorColor: Colors.white,
indicator: UnderlineTabIndicator(
borderSide: BorderSide(color: Colors.white, width: 5.0),
insets: EdgeInsets.symmetric(horizontal: 40),
),
labelColor: Colors.white,
unselectedLabelColor: Colors.grey[300],
tabs: [
Tab(
iconMargin: EdgeInsets.only(top: 5),
text: "Tab Bar 1",
),
Tab(
iconMargin: EdgeInsets.only(top: 5),
text: "Tab bar 2",
),
]),
),
Container(
height: 200 * 6 // 200 will be Card Size and 6 is number of Cards
child: TabBarView(controller: _tabController, children: [
tabDetails(),
tabDetails(),
]))
]));
}
tabDetails() {
final height = MediaQuery.of(context).size.height;
final width = MediaQuery.of(context).size.width;
double mainAxisHeight = height > width ? height : width;
return Container(
padding: EdgeInsets.symmetric(horizontal: 15),
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [
Colors.red[100],
Colors.red[200],
])),
child: ListView(
physics: NeverScrollableScrollPhysics(), // This will disable LitView'Scroll so only Scroll is possible by TabBarView Scroll.
children: [
SizedBox(height: 10),
Container(
height:140,
width: width,
child: ListView.builder(
scrollDirection: Axis.vertical,
itemCount: 6,
itemBuilder: (BuildContext context, int indexChild) {
return Row(
children: [
MyListTile(
name: "Name",
),
SizedBox(width: 5),
],
);
},
),
),
SizedBox(height: 1000),
],
),
);
}
}
If you want to use CustomScrollView, you can make use of SliverToBoxAdapter:
Widget build(BuildContext context) {
return Scaffold(
body: DefaultTabController(
length: 2,
child: CustomScrollView(
slivers: [
SliverAppBar(
title: Text('SliverAppBar'),
bottom: TabBar(
tabs: [
Tab(icon: Icon(Icons.call), text: "Call"),
Tab(icon: Icon(Icons.message), text: "Message"),
],
),
),
SliverToBoxAdapter(
child: SizedBox(
height: MediaQuery.of(context).size.height,
child: TabBarView(
children: [
Container(color: Colors.red),
Container(color: Colors.blue),
],
),
),
),
],
),
),
);
}