I am trying to create a tabbed bar layout screen without the AppBar though. I have already referred to the solution on this link: how to create the tab bar without app bar in flutter? but it is not working for me. Here is what my screen looks like when I place TabBar in the appbar:
parameter:
My TabBar has moved to the top left corner under the status bar and its all squeezed in one corner. It's almost as if it's not there at all.
When I use the AppBar class but only pass the bottom:
parameter here is what happens:
There is an ugly space on top of the TabBar which is obviously meant for the AppBar title. Here is my code:
return new Scaffold( appBar: new TabBar( tabs: widget._tabs.map((_Page page){ return Text(page.tabTitle); }).toList(), controller: _tabController, isScrollable: true, ), backgroundColor: Colors.white, body: new TabBarView( controller: _tabController, children: widget._tabs.map((_Page page){ return new SafeArea( top:false, bottom: false, child: (page.page == Pages.cart?new CartHomeScreen():_lunchesLayout()) ); }).toList() ), );
How can I just have TabBar without that space on top and is it possible to make the two tab items and their indicators to stretch and fill the side spaces?
TabBar with labels without AppBar Instead, we can use PreferredSize widget. Since TabBar has preferredSize , we should use it to decide the height. Yes!
You have your own custom Menu button to open/close drawer. You don't want to use AppBar as well. In that case you can use GlobalKey<ScaffoldState>() object to open Drawer.
Your first screenshot actually shows it working just fine - the issue is that 'fine' isn't quite what you expect. The default text color is white for tabbar, so your labels aren't showing and instead just the bottom line is, which is what you see at the top left. Also, TabBar is a preferred size widget already, but it doesn't have the same height as an AppBar so if that's what you're going for, it won't look like it.
Here's an example that makes it look like the app bar. kToolbarHeight
is the same constant that AppBar uses.
import 'package:flutter/material.dart'; void main() => runApp(new MyApp()); class MyApp extends StatefulWidget { @override State<StatefulWidget> createState() => new MyAppState(); } class MyAppState extends State<MyApp> { @override Widget build(BuildContext context) { return new MaterialApp( title: 'msc', home: new DefaultTabController( length: 2, child: new Scaffold( appBar: new PreferredSize( preferredSize: Size.fromHeight(kToolbarHeight), child: new Container( color: Colors.green, child: new SafeArea( child: Column( children: <Widget>[ new Expanded(child: new Container()), new TabBar( tabs: [new Text("Lunches"), new Text("Cart")], ), ], ), ), ), ), body: new TabBarView( children: <Widget>[ new Column( children: <Widget>[new Text("Lunches Page")], ), new Column( children: <Widget>[new Text("Cart Page")], ) ], ), ), ), ); } }
Which results in this:
Edit:
As noted in the comments and from this github issue, you could also use the flexibleSpace property of the AppBar to hold the tabbar to basically the same effect:
return new DefaultTabController( length: 3, child: new Scaffold( appBar: new AppBar( flexibleSpace: new Column( mainAxisAlignment: MainAxisAlignment.end, children: [ new TabBar( tabs: [ new Tab(icon: new Icon(Icons.directions_car)), new Tab(icon: new Icon(Icons.directions_transit)), new Tab(icon: new Icon(Icons.directions_bike)), ], ), ], ), ), ), );
Follow below code
import 'package:flutter/material.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo', theme: ThemeData( primarySwatch: Colors.blue, visualDensity: VisualDensity.adaptivePlatformDensity, ), home: MyHomePage(), ); } } class MyHomePage extends StatefulWidget { @override _MyHomePageState createState() => _MyHomePageState(); } class _MyHomePageState extends State<MyHomePage> with SingleTickerProviderStateMixin { TabController _tabController; @override void initState() { _tabController = new TabController(length: 2, vsync: this); super.initState(); } @override Widget build(BuildContext context) { return Scaffold( body: Container( child: Column( crossAxisAlignment: CrossAxisAlignment.center, children: [ Container( height: MediaQuery.of(context).size.height / 2, child: Center( child: Text( "Tabbar with out Appbar", style: TextStyle( color: Colors.white, fontWeight: FontWeight.bold), ), ), color: Colors.blue, ), TabBar( unselectedLabelColor: Colors.black, labelColor: Colors.red, tabs: [ Tab( text: '1st tab', ), Tab( text: '2 nd tab', ) ], controller: _tabController, indicatorSize: TabBarIndicatorSize.tab, ), Expanded( child: TabBarView( children: [ Container(child: Center(child: Text('people'))), Text('Person') ], controller: _tabController, ), ), ], ), ), ); } }
view the result
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