Flutter TabBar height Icons - flutter

import 'package:flutter/material.dart';
class TestTab extends StatelessWidget {
final List<Tab> myTabs = <Tab>[
Tab(
child: Image.asset(
'assets/icons/project/proj_001.png',
),
),
...
Tab(
icon: Image.asset(
'assets/icons/project/all.png',
height: 100,
),
),
...
Tab(
icon: Image.asset(
'assets/icons/project/proj_009.png',
),
),
];
#override
Widget build(BuildContext context) {
return DefaultTabController(
initialIndex: 5,
length: myTabs.length,
child: Scaffold(
appBar: PreferredSize(
preferredSize: Size.fromHeight(150.0),
child: AppBar(
bottom: TabBar(
isScrollable: true,
tabs: myTabs,
),
),
),
body: TabBarView(
children: myTabs.map((Tab tab) {
return Center(
child: Text(
'Test',
style: const TextStyle(fontSize: 36),
),
);
}).toList(),
),
),
);
}
}
I try to increase the icon height, but it doesn't work. It's as if he selects all the parameters himself. Does not allow reducing the width or increasing the height of elements.
Screen
Please tell me how you can increase the height of elements?

if u want set the icon size small then u can do like that
Tab(
text: "Category List",
icon: Icon(Icons.home,size: 15,),
),
Tab(
text: "Product List",
icon: Icon(Icons.view_list,size: 15,),
),
Tab(
text: "Contact Us",
icon: Icon(Icons.contacts,size: 15,),
),
Tab(
text: "Darshan Timing",
icon: Icon(Icons.access_time,size: 15,),
)
here size: 15, will make the icon size as you want

You gotta make custom TabBar for customization. Something like this.
CustomTabbar
import 'package:flutter/material.dart';
class ChangeTextSizeTabbar extends StatefulWidget {
#override
ChangeTextSizeTabbarState createState() {
return new ChangeTextSizeTabbarState();
}
}
class ChangeTextSizeTabbarState extends State<ChangeTextSizeTabbar> {
#override
Widget build(BuildContext context) {
return DefaultTabController(
length: 3,
child: Scaffold(
appBar: AppBar(
title: Text("Change Text Size Tabbar Example"),
bottom: TabBar(
tabs: <Tab>[
Tab(
child: Image.asset(
'assets/icons/project/proj_001.png',
height : 100,
width : 100,
),
),
Tab(
icon: Image.asset(
'assets/icons/project/all.png',
height : 100,
width : 100,
),
),
Tab(
icon: Image.asset(
'assets/icons/project/proj_009.png',
height : 100,
width : 100,
),
),
] ),
),
body: TabBarView(
children: <Widget>[
Container(),
Container(),
Container(),
],
),
),
);
}
}
main.dart
import 'package:flutter/material.dart';
import 'change_text_size_tabbar_task-3.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.blue,
),
//home: MyHomePage(title: 'Flutter Demo Home Page'),
home: ChangeTextSizeTabbar(),
);
}
}

Related

How to open a new screen within the same tab and keep showing the TabBar

I would like when I click on button "Nova Reserva", it opens a new screen, but in the same tab, without losing the TabBar.
APP
enter image description here
Current
enter image description here
Code TabBarView
TabBarView(
controller: _tabController,
children: const [
HomeTab(),
ResearchesTab(),
SchedulesTab(),
Center(
child: Text('MENSAGENS'),
),
Center(
child: Text('CADASTROS'),
),
],
),
Code TabBar
child: TabBar(
physics: const BouncingScrollPhysics(),
controller: _tabController,
isScrollable: true,
indicatorPadding: EdgeInsets.symmetric(
vertical: size.height * .005,
),
indicatorSize: TabBarIndicatorSize.label,
indicator: BoxDecoration(
border: Border(
bottom: BorderSide(
color: CustomColors.orange,
width: size.height * .004,
),
),
),
labelPadding: EdgeInsets.symmetric(
horizontal: size.width * .04,
),
tabs: const [
TabBarTile(
image: 'assets/images/home.png',
label: 'Home',
),
TabBarTile(
image: 'assets/images/pesquisas.png',
label: 'Pesquisas',
),
TabBarTile(
image: 'assets/images/agendamentos.png',
label: 'Agendamentos',
),
TabBarTile(
image: 'assets/images/mensagens.png',
label: 'Mensagens',
),
TabBarTile(
image: 'assets/images/cadastros.png',
label: 'Cadastros',
),
],
),
In the button I'm using navigation with GetX, but I've also tried with MaterialPageRoute and I wasn't successful.
My objective
enter image description here
Create a Boolean variable that will change the tab bar's body according to its value. Change the tab bar's body content by changing the flag of the Boolean value.
Complete Code : -
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({super.key});
static const String _title = 'Flutter Code Sample';
#override
Widget build(BuildContext context) {
return const MaterialApp(
debugShowCheckedModeBanner: false,
title: _title,
home: MyStatelessWidget(),
);
}
}
class MyStatelessWidget extends StatefulWidget {
const MyStatelessWidget({super.key});
#override
State<MyStatelessWidget> createState() => _MyStatelessWidgetState();
}
class _MyStatelessWidgetState extends State<MyStatelessWidget> {
bool buttonOnePressed = false; // Declare the Boolean variable
#override
Widget build(BuildContext context) {
return DefaultTabController(
initialIndex: 1,
length: 3,
child: Scaffold(
appBar: AppBar(
title: const Text('TabBar Widget'),
bottom: const TabBar(
tabs: <Widget>[
Tab(
text: "Tab 1",
),
Tab(
text: "Tab 2",
),
Tab(
text: "Tab 3",
),
],
),
),
body: TabBarView(
children: <Widget>[
const Center(
child: Text("Tab 1"),
),
buttonOnePressed // Display widgets according to Boolean variable
? const Center(
child: Text("From Button 1"),
)
: Center(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: () {
setState(() {
buttonOnePressed = true; // change Boolean value
});
},
child: const Text("Button 1"),
),
const SizedBox(width: 30),
ElevatedButton(
onPressed: () {}, child: const Text("Button 2"))
],
),
),
const Center(
child: Text("It's sunny here"),
),
],
),
),
);
}
}
Output : -

How can I set the home button in the middle of the bottom bar to the default value?

I made a bottom bar using the DefaultTabController, but the home button was set to the middle. By the way, the default value is on the menu. How can I change the default value to the home button?
this is main.dart
import 'package:flutter/material.dart';
import 'package:contact/widgets/bottom_bar.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget{
MyAppState createState() => MyAppState();
}
class MyAppState extends State<MyApp> {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: "flutter_project",
debugShowCheckedModeBanner: false,
theme: ThemeData(
primaryColor: Colors.white,
),
home: DefaultTabController(
length: 3,
child: Scaffold(
body: TabBarView(
physics: NeverScrollableScrollPhysics(),
children: [
Container(),
Container(),
Container(),
],
),
bottomNavigationBar: Bottom(),
),
),
);
}
}
Anyone, please help me
this is bottom_bar.dart
import 'package:flutter/material.dart';
class Bottom extends StatelessWidget {
#override
Widget build(BuildContext context){
return Container(
child: Container(
height: 70,
child: TabBar(
labelColor: Colors.amber,
unselectedLabelColor: Colors.grey,
indicatorColor: Colors.transparent,
tabs: const <Widget>[
Tab(
icon: Icon(
Icons.menu,
size: 18,
),
child: Text(
'menu',
style: TextStyle(fontSize: 9),
),
),
Tab(
icon: Icon(
Icons.home,
size: 18,
),
child: Text(
'home',
style: TextStyle(fontSize: 9),
),
),
Tab(
icon: Icon(
Icons.person_outline,
size: 18,
),
child: Text(
'user',
style: TextStyle(fontSize: 9),
),
),
],
),
),
);
}
}
I think I need to use the index, but I don't know what to do
Use initialIndex in DefaultTabController.

Flutter app bar layout: action buttons in the same row with tab bar?

Let's say I would like to create an app bar with two action buttons at the start and in the end while having a tab bar right in the middle, all in the same row. Here is the code I worked on to get the tab bar working:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
highlightColor: Colors.transparent,
splashColor: Colors.transparent,
hoverColor: Colors.transparent,
),
title: 'Flutter Demo',
home: DefaultTabController(
length: 2,
child: Scaffold(
appBar: AppBar(
leading: Icon(
Icons.menu,
),
automaticallyImplyLeading: false,
backgroundColor: Colors.white,
flexibleSpace: new Column(
mainAxisAlignment: MainAxisAlignment.end,
children: [
TabBar(
indicatorColor: Colors.pink[100],
tabs: [
Tab(text: 'Dogs'),
Tab(text: 'Cats'),
],
labelColor: Colors.black,
), //tabbar
], //chilren on Tabbar
), //new Column
), //appbar
body: TabBarView(
children: [
Center(child: Text('DOGS')),
Center(child: Text('CATS')),
],
),
),
),
);
}
}
appbar current
app bar expected, with two action buttons (and the indicator position needs a bit more work:
But how could I add the two action buttons without having the tab bar overlaying them? Using grid maybe?
try to put tabbar into title:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
highlightColor: Colors.transparent,
splashColor: Colors.transparent,
hoverColor: Colors.transparent,
),
title: 'Flutter Demo',
home: DefaultTabController(
length: 2,
child: Scaffold(
appBar: AppBar(
leading: Icon(
Icons.menu,
color: Colors.black,
),
actions: [
Icon(
Icons.settings,
color: Colors.black,
),
],
automaticallyImplyLeading: false,
backgroundColor: Colors.white,
title: Padding(
padding: EdgeInsets.only(left: 50, right: 50),
child:TabBar(
indicatorColor: Colors.pink[100],
tabs: [
Tab(text: 'Dogs'),
Tab(text: 'Cats'),
],
labelColor: Colors.black,
),),
), //appbar
body: TabBarView(
children: [
Center(child: Text('DOGS')),
Center(child: Text('CATS')),
],
),
),
),
);
}
}

I want to place a custom widget in place of appbar and want a tab view below it

I have built a custom widget("cab") which displays a account login circle and no. of points user have.
I want a tab view just below it . For now I have placed this custom widget inside the tab itself :
bottomNavigationBar: Material(child: TabBar(
tabs: const <Widget>[
Tab( icon: Icon(Icons.add)),
Tab( icon: Icon(Icons.search))
]
),),
body :TabBarView(
children: <Widget>[
Column(
children: <Widget>[
cab
],
),
But I want this widget to be displayed above tab view but not the part of tab view itself . How can I do
this?
Thanks.
Replace the AppBar widget with your own widget below:
class App extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: DefaultTabController(
length: 2,
child: Scaffold(
bottomNavigationBar: TabBar(
labelColor: Colors.orange,
unselectedLabelColor: Colors.grey,
indicatorSize: TabBarIndicatorSize.tab,
indicatorColor: Colors.orange,
tabs: [
Tab(
icon: Icon(Icons.add),
),
Tab(
icon: Icon(Icons.search),
),
],
),
body: Column(
children: <Widget>[
// Remove the AppBar code below and put your "cab" widget
AppBar(
primary: true,
title: Text('Here is your cab'),
centerTitle: true,
backgroundColor: Colors.orange,
),
Expanded(
child: TabBarView(
children: [
Icon(Icons.add),
Icon(Icons.search),
],
),
),
],
),
),
),
);
}
}

Use AppBar Style on a TabBar that is not its child

I want my tab bar to have the same styling as the AppBar.
(using the same icon color, text color and background color)
My layout features a tabbed section to the left and another column to the right. Whithout additional styling the TabBar icons are not visible because both they and the background are white.
So what would be the most convinient way to apply the AppBar style to the TabBar? Thanks for any suggestions!
import 'package:flutter/material.dart';
void main() => runApp(new MyApp());
class MyApp extends StatefulWidget {
#override
State<StatefulWidget> createState() => MyAppState();
}
class MyAppState extends State<MyApp> with TickerProviderStateMixin {
TabController _tabController;
#override
void initState() {
super.initState();
_tabController = TabController(
vsync: this,
length: 3,
initialIndex: 0,
);
}
#override
Widget build(BuildContext context) {
double bigSize = IconTheme.of(context).size * 4.0;
return MaterialApp(
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
appBar: AppBar(
title: Text('Flutter TabBar Style Problem'),
),
body: Row(
children: <Widget>[
Expanded(
child: Column(
children: [
SizedBox(
height: 36.0,
child: TabBar(
controller: _tabController,
tabs: [
Tab(icon: Icon(Icons.cake)),
Tab(icon: Icon(Icons.train)),
Tab(icon: Icon(Icons.info)),
],
),
),
Flexible(
child: TabBarView(
controller: _tabController,
children: [
Icon(Icons.cake, size: bigSize),
Icon(Icons.train, size: bigSize),
Icon(Icons.info, size: bigSize),
],
),
),
],
),
),
Expanded(
child: Container(
color: Colors.amber.shade400,
alignment: FractionalOffset.center,
child: Text('Another Column'),
),
),
],
),
),
);
}
}
When you place a TabBar as the bottom: widget of the AppBar you get a lot of things for free. It gets the background color and a Material widget that has a special feature. It forces the indicator to use white color when the default indicator color is the same as the background color (give a look here).
So when you place a TabBar outside the AppBar you have to handle everything yourself.
But it seems pretty easy wrapping the TabBar in a Material and giving it the primaryColor (the same the framework does here).
So at the end is just replacing your SizedBox by a Material and setting the color. And you get the ink splash for free.
Material(
color: Theme.of(context).primaryColor,
child: TabBar(
controller: _tabController,
tabs: [
Tab(icon: Icon(Icons.cake)),
Tab(icon: Icon(Icons.train)),
Tab(icon: Icon(Icons.info)),
],
),
),
What appBar interally do is inserting a custom DefaultTextStyle that points to Theme.textTheme.title
You can do the exact same thing by wrapping a part of your widget tree into a custom DefaultTextStyle.
DefaultTextStyle(
style: Theme.of(context).textTheme.title,
child: Text('Title')
);
As for the background color, it is availble in another field of Theme.
Container(
color: Theme.of(context).primaryColor,
);
Change Your Code to Something Like This:
class ProductsAdminPage extends StatelessWidget {
#override
Widget build(BuildContext context) {
// Scaffold is inside DefaultTabController because DefaultTabController needs access to Scaffold
return DefaultTabController(
// Number of tabs
length: 2,
child: Scaffold(
drawer: Drawer(
child: Column(
children: <Widget>[
AppBar(
automaticallyImplyLeading: false,
title: Text('Choose'),
),
ListTile(
title: Text('All Products'),
onTap: () {
Navigator.pushReplacement(context, MaterialPageRoute(builder: (BuildContext context) => ProductsPage()));
},
)
],
),
),
appBar: AppBar(
title: Text('Manage Products'),
// TabBar is added as the bottom widget of the appBar
bottom: TabBar(
tabs: <Widget>[
// No of Tab() Widgets Should be equal to length
Tab(
icon: Icon(Icons.create),
text: 'Create Product',
),
Tab(
icon: Icon(Icons.list),
text: 'My Products',
),
],
),
),
body: TabBarView(
// No of pages should be equal to length
children: <Widget>[ProductCreatePage(), ProductListPage()],
),
),
);
}
}
ThemeData(
appBarTheme: AppBarTheme(
...
),
tabBarTheme: TabBarTheme(
labelColor: Colors.black,
unselectedLabelColor: Colors.grey,
indicatorSize: TabBarIndicatorSize.label,
labelStyle: TextStyle(
fontSize: 20.0,
color: Colors.green,
),
unselectedLabelStyle: TextStyle(
fontSize: 20.0,
color: Colors.green,
),
),
);
We can customise theme in App Level theme as our app bar and then every where in app, tabs will use same style as we define in theme.
I hope it is the easiest approach...