Note:
Solution from stackoverflow about this question not solved my issue
My full code
import 'package:flutter/material.dart';
class BookingHistory extends StatefulWidget {
BookingHistory({Key key}) : super(key: key);
#override
_BookingHistoryState createState() => _BookingHistoryState();
}
class _BookingHistoryState extends State<BookingHistory> {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: DefaultTabController(
length: 3,
child: Scaffold(
// backgroundColor: Colors.white,
appBar: AppBar(
flexibleSpace: Container(
color: Colors.green,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
TabBar(indicatorColor: Colors.blue,
labelColor: Colors.red,
unselectedLabelColor: Colors.green,
tabs: [
Tab(
child: Text(
"Completed",
style: TextStyle(color: Colors.white),
),
),
Tab(
child: Text(
"Requested",
style: TextStyle(color: Colors.white),
),
),
Tab(
child: Text(
"Accepted",
style: TextStyle(color: Colors.white),
),
)
])
],
),
),
),
body: TabBarView(children: [
Container(
child: Center(
child: Text("i am tab 1"),
),
),
Container(
child: Center(
child: Text("i am tab 2"),
),
),
Container(
child: Center(
child: Text("i am tab 3"),
),
)
]),
)),
);
}
}
Answer
To make effecting indicatorColor,labelColor or unselectedLabelColor we need wrap Widget with Material Widget(Solution suggested by Ravinder Kumar) or if we need only to change indicatorColor color then use indicatorColor: Colors.blueAccent color with ending with name Accent (Solution suggested by Amazing Aidan)
Bizarrely, changing the code to
indicatorColor: Colors.blueAccent
works but not Colors.blue
Wrap your TabBar inside Material widget,
Material(
color: Colors.green,
child:
TabBar(
indicatorColor: Colors.red,
labelColor: Colors.red,
unselectedLabelColor: Colors.green,
.....
Output:
Complete Code
class BookingHistory extends StatefulWidget {
BookingHistory({Key key}) : super(key: key);
#override
_BookingHistoryState createState() => _BookingHistoryState();
}
class _BookingHistoryState extends State<BookingHistory> {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: DefaultTabController(
length: 3,
child: Scaffold(
// backgroundColor: Colors.white,
appBar: AppBar(
flexibleSpace: Container(
color: Colors.green,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
Material(
color: Colors.green,
child:
TabBar(
indicatorColor: Colors.red,
labelColor: Colors.red,
unselectedLabelColor: Colors.green,
tabs: [
Tab(
child: Text(
"Completed",
style: TextStyle(color: Colors.white),
),
),
Tab(
child: Text(
"Requested",
style: TextStyle(color: Colors.white),
),
),
Tab(
child: Text(
"Accepted",
style: TextStyle(color: Colors.white),
),
)
]))
],
),
),
),
body: TabBarView(children: [
Container(
child: Center(
child: Text("i am tab 1"),
),
),
Container(
child: Center(
child: Text("i am tab 2"),
),
),
Container(
child: Center(
child: Text("i am tab 3"),
),
)
]),
)),
);
}
}
UPDATE: strangely any colors other than seems to work, for example use indicatorColor: Colors.black,
The Container in the AppBar is unncessary, instead directly set backgroundColor: Colors.green in AppBar.
And in the TabBar set indicatorColor, labelColor, unselectedLabelColor properly.
In each Tab you don't need to specify TextStyle. It will override labelColor and unselectedLabelColor from the TabBar.
AppBar(
backgroundColor: Colors.green,
flexibleSpace: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
TabBar(
indicatorColor: Colors.white,
labelColor: Colors.white,
unselectedLabelColor: Colors.white70,
tabs: [
Tab(child: Text("Completed")),
Tab(child: Text("Requested")),
Tab(child: Text("Accepted"))
])
],
),
)
Related
I'm trying to do it but I do not know how to do it
I wanna get rid of green box space.
I've trying with PreferedSize widget but it doesn't work my code is ...
#override
Widget build(BuildContext context) {
return SafeArea(
child: PreferredSize(
preferredSize: Size.fromHeight(SizeConfig.heightMultiplier * 6.2),
// it's pretty narrow size but doesn't work in this screen but others
child: Scaffold(
appBar: AppBar(
centerTitle: true,
title: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'hello',
style: TextStyle(fontWeight: FontWeight.bold),
),
Text(
'world',
),
Text(
'!',
style: TextStyle(fontWeight: FontWeight.bold),
),
],
),
actions: <Widget>[
Icon(
Icons.add,
color: Colors.transparent,
),
],
backgroundColor: Theme.of(context).primaryColor,
bottom: TabBar(
controller: tabController,
indicatorColor: Colors.white,
tabs: myTabs,
),
),
...
I need your help ☑
Wrap the TabBar with a PreferredSize widget of height 0 (or a small height):
bottom: PreferredSize(
preferredSize: Size.fromHeight(0),
child: TabBar(
controller: tabController,
indicatorColor: Colors.white,
tabs: myTabs,
),
),
Inside the 2nd Container where the Text Account and Logout is located, i'd like that this is at the Bottom of the Drawer. I Added the Widget Align and alignment:bottomCenter, but this doesn't work. I also tried is with FractionalOffset but this doesn't work aswell.
What mistake am i making here that the Container is not moved to the Bottom?
Code
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:mealapp/models/global.dart';
import 'package:mealapp/screens/meal_plan/meal_plan.dart';
import 'package:mealapp/screens/shopping_plan/shopping.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:mealapp/bloc/auth/auth_bloc.dart';
import 'package:mealapp/bloc/auth/auth_event.dart';
class HomeScreen extends StatefulWidget {
#override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
#override
Widget build(BuildContext context) {
SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
statusBarColor: Colors.white,
statusBarBrightness: Brightness.light,
statusBarIconBrightness: Brightness.dark,
));
final authBloc = context.bloc<AuthBloc>();
return DefaultTabController(
length: 2,
child: Scaffold(
key: _scaffoldKey,
drawer: Theme(
data: Theme.of(context).copyWith(canvasColor: darkGreyColor),
child: Drawer(
child: ListView(
padding: EdgeInsets.zero,
children: <Widget>[
DrawerHeader(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(20),
),
child: Center(
child: Text(
'Settings',
style: darkTodoTitle,
),
),
),
ListTile(
leading:
Icon(Icons.create, color: Colors.white, size: 25),
onTap: () {},
title: Text(
'Create New List',
style: TextStyle(color: Colors.white, fontSize: 20),
),
),
Container(
child: Column(
children: <Widget>[
Row(
children: <Widget>[
Expanded(
child: Padding(
padding: EdgeInsets.symmetric(horizontal: 10.0),
child: Container(
height: 1.8,
color: Colors.white54,
),
),
),
],
),
ListTile(
leading: Icon(Icons.account_box,
color: Colors.white, size: 25),
onTap: () {},
title: Text(
'Here the Lists you got access or created',
style: TextStyle(color: Colors.white, fontSize: 20),
),
),
],
),
),
Container(
child: Align(alignment: Alignment.bottomCenter,
child: Column(
children: <Widget>[
Row(
children: <Widget>[
Expanded(
child: Padding(
padding: EdgeInsets.symmetric(horizontal: 10.0),
child: Container(
height: 1.8,
color: Colors.white54,
),
),
),
],
),
ListTile(
leading: Icon(Icons.account_box,
color: Colors.white, size: 25),
onTap: () {},
title: Text(
'Account',
style: TextStyle(color: Colors.white, fontSize: 20),
),
),
ListTile(
leading:
Icon(Icons.person, color: Colors.white, size: 25),
onTap: () => authBloc.add(LogoutUser()),
title: Text(
'Logout',
style: TextStyle(color: Colors.white, fontSize: 20),
),
),
],
),
),
),
],
),
),
),
appBar: AppBar(
brightness: Brightness.light,
elevation: 0,
leading: Builder(
builder: (context) => IconButton(
onPressed: () => _scaffoldKey.currentState.openDrawer(),
icon: Icon(
Icons.menu,
color: Colors.blue,
)),
),
title: TabBar(
tabs: [
Tab(
icon: Icon(Icons.fastfood),
),
Tab(
icon: Icon(Icons.local_grocery_store),
),
],
labelColor: darkGreyColor,
unselectedLabelColor: Colors.blue,
indicatorSize: TabBarIndicatorSize.label,
indicatorPadding: EdgeInsets.all(5.0),
indicatorColor: Colors.transparent,
),
backgroundColor: Colors.white,
),
body: TabBarView(
children: [
MealPage(),
ShoppingPage(),
],
),
backgroundColor: Colors.white,
),
);
}
}
You need to wrap ListTile Widget inside Align Widget and provide alignment: FractionalOffset.bottomCenter . This Align Widget should be wrapped with Expanded Widget as you mentioned in the question you missed to wrap Align inside Expanded Widget. Try as follows this will help you.
Expanded(
child: Align(
alignment: FractionalOffset.bottomCenter,
child: ListTile(
leading: Icon(Icons.account_box,
color: Colors.white, size: 25),
onTap: () {},
title: Text(
'Account',
style: TextStyle(
color: Colors.white, fontSize: 20
),
),
),
),
),
I am using Scaffold to show my bottom tab navigator, but it shows too high, is there a way to reduce size of height in my Tab Bar. here is the code
Scaffold(
bottomNavigationBar: Material(
child: new TabBar(
tabs: <Widget>[
Tab(icon: Icon(Icons.home),text: 'Home'),
Tab(icon: Icon(Icons.settings),text: 'Settings')
],
labelColor: Colors.blue,
unselectedLabelColor: Colors.grey,
),
),
body: TabBarView(
children: [
MainScreen(),
FirstPage(),
],
),
)
You can wrap the child: new TabBar() in a container. And also change height of icon and text.
import 'package:flutter/material.dart';
class MyButtomTabBar extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: DefaultTabController(
length: 2,
child: Scaffold(
bottomNavigationBar: Material(
child: Container(
height: 40,
child: new TabBar(
tabs: <Widget>[
Tab(
icon: Icon(
Icons.home,
size: 15,
),
child: Text(
'Home',
style: TextStyle(fontSize: 10),
),
),
Tab(
icon: Icon(
Icons.settings,
size: 15,
),
child: Text(
'Settings',
style: TextStyle(fontSize: 10),
),
),
],
labelColor: Colors.blue,
unselectedLabelColor: Colors.grey,
),
),
),
),
),
);
}
}
Here I want to ask or can I make a tutorial like tabs, focusing center but the left and right tabs are 30% transparent like this, thank you!
Same can be achieved using - unselectedLabelColor: & indicatorColor: of TabBar widget.
Example Code:
#override
Widget build(BuildContext context) {
return DefaultTabController(
length: 6,
child: Scaffold(
appBar: AppBar(
centerTitle: true,
leading: Icon(Icons.person_outline),
title: Text('DASHBOARD',style: TextStyle(fontSize: 16.0),),
bottom: PreferredSize(
child: TabBar(
isScrollable: true,
unselectedLabelColor: Colors.white.withOpacity(0.3),
indicatorColor: Colors.white,
tabs: [
Tab(
child: Text('Tab 1'),
),
Tab(
child: Text('Investment'),
),
Tab(
child: Text('Your Earning'),
),
Tab(
child: Text('Current Balance'),
),
Tab(
child: Text('Tab 5'),
),
Tab(
child: Text('Tab 6'),
)
]),
preferredSize: Size.fromHeight(30.0)),
actions: <Widget>[
Padding(
padding: const EdgeInsets.only(right: 16.0),
child: Icon(Icons.add_alert),
),
],
),
body: TabBarView(
children: <Widget>[
Container(
child: Center(
child: Text('Tab 1'),
),
),
Container(
child: Center(
child: Text('Tab 2'),
),
),
Container(
child: Center(
child: Text('Tab 3'),
),
),
Container(
child: Center(
child: Text('Tab 4'),
),
),
Container(
child: Center(
child: Text('Tab 5'),
),
),
Container(
child: Center(
child: Text('Tab 6'),
),
),
],
)),
);
}
Output:
add isScrollable: true, inside TabBar
like
TabBar(
isScrollable: true,
.
.
.
)
Screenshot:
Code:
TabBar(
isScrollable: true, // Required
unselectedLabelColor: Colors.white30, // Other tabs color
labelPadding: EdgeInsets.symmetric(horizontal: 30), // Space between tabs
indicator: UnderlineTabIndicator(
borderSide: BorderSide(color: Colors.white, width: 2), // Indicator height
insets: EdgeInsets.symmetric(horizontal: 48), // Indicator width
),
tabs: [
Tab(text: 'Total Investment'),
Tab(text: 'Your Earnings'),
Tab(text: 'Current Balance'),
],
)
Placing isScrollable: true, in the TabBar worked for me.
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
#override
Widget build(BuildContext context) {
final _kTabPages = <Widget>[
Center(child: Icon(Icons.cloud, size: 64.0, color: Colors.teal)),
Center(child: Icon(Icons.alarm, size: 64.0, color: Colors.cyan)),
Center(child: Icon(Icons.forum, size: 64.0, color: Colors.blue)),
Center(child: Icon(Icons.forum, size: 64.0, color: Colors.blue)),
Center(child: Icon(Icons.forum, size: 64.0, color: Colors.blue)),
Center(child: Icon(Icons.forum, size: 64.0, color: Colors.blue)),
Center(child: Icon(Icons.forum, size: 64.0, color: Colors.blue)),
Center(child: Icon(Icons.forum, size: 64.0, color: Colors.blue)),
];
final _kTabs = <Tab>[
Tab(text: 'NK'),
Tab(text:'ActiveTools'),
Tab(text:'Coxmate'),
Tab(text:'Concept2'),
Tab(text:'Croker'),
Tab(text:'Hudson'),
Tab(text:'Swift'),
Tab(text:'Rowshop'),
];
return DefaultTabController(
length: _kTabs.length,
child: Scaffold(
appBar: AppBar(
leading:Image.asset("assets/Macarbi Logo.jpg"),//TODO image or icon here
title: Text(widget.title),
actions: <Widget>[
IconButton(
icon: Icon(Icons.search),
onPressed: () {},//TODO do something
),
IconButton(
icon: Icon(Icons.account_circle),
onPressed: () {},//TODO do something
),
],
bottom: TabBar(
isScrollable: true,
tabs: _kTabs,
),
),
body: TabBarView(
children: _kTabPages,
),
),
);
}
I am trying to put the TabBar on the bottom of the app.
It worked so far, yet I can't get the pages to work (TabBarView). It just looks black and unresponsive. The TabBar is unresponsive too. Have I taken the wrong approach?
Currently, it looks like that:
And the code looks like this:
import 'package:flutter/material.dart';
void main() => runApp(Bookkeeper());
class Bookkeeper extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: DefaultTabController(
length: 4,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
verticalDirection: VerticalDirection.up,
mainAxisSize: MainAxisSize.min,
children: [
AppBar(
backgroundColor: Color(0xFF3F5AA6),
title: Container(
padding: EdgeInsets.only(top: 8.0),
child: menu(),
),
),
TabBarView(
children: [
Icon(Icons.directions_car),
Icon(Icons.directions_transit),
Icon(Icons.directions_bike),
Icon(Icons.directions_bike),
],
),
],
),
),
);
}
Widget menu() {
return TabBar(
tabs: [
Tab(
child: Container(
height: 45.0,
child: Column(
children:
[
Icon(Icons.euro_symbol),
Text(
"Transactions",
style: new TextStyle(
height: 1.5,
fontSize: 9.8,
color: Colors.white,
),
),
],
),
),
),
Tab(
child: Container(
height: 45.0,
child: Column(
children:
[
Icon(Icons.assignment),
Text(
"Bills",
style: new TextStyle(
height: 1.5,
fontSize: 9.5,
color: Colors.white,
),
),
],
),
),
),
Tab(
child: Container(
height: 45.0,
child: Column(
children:
[
Icon(Icons.account_balance_wallet),
Text(
"Balance",
style: new TextStyle(
height: 1.5,
fontSize: 9.5,
color: Colors.white,
),
),
],
),
),
),
Tab(
child: Container(
height: 45.0,
child: Column(
children:
[
Icon(Icons.settings),
Text(
"Options",
style: new TextStyle(
height: 1.5,
fontSize: 9.5,
color: Colors.white,
),
),
],
),
),
),
],
);
}
}
Use bottomNavigationBar to position the Tabs at the bottom of the screen
class Bookkeeper extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: DefaultTabController(
length: 4,
child: Scaffold(
appBar: AppBar(
backgroundColor: Color(0xFF3F5AA6),
title: Text("Title text"),
),
bottomNavigationBar: menu(),
body: TabBarView(
children: [
Container(child: Icon(Icons.directions_car)),
Container(child: Icon(Icons.directions_transit)),
Container(child: Icon(Icons.directions_bike)),
Container(child: Icon(Icons.directions_bike)),
],
),
),
),
);
}
Widget menu() {
return Container(
color: Color(0xFF3F5AA6),
child: TabBar(
labelColor: Colors.white,
unselectedLabelColor: Colors.white70,
indicatorSize: TabBarIndicatorSize.tab,
indicatorPadding: EdgeInsets.all(5.0),
indicatorColor: Colors.blue,
tabs: [
Tab(
text: "Transactions",
icon: Icon(Icons.euro_symbol),
),
Tab(
text: "Bills",
icon: Icon(Icons.assignment),
),
Tab(
text: "Balance",
icon: Icon(Icons.account_balance_wallet),
),
Tab(
text: "Options",
icon: Icon(Icons.settings),
),
],
),
);
}
}