TapBarView adjust height according to content in it - flutter

i am trying to display items in Listview at first tab that I created. However when the tab is invisible unless I wrap TabBarView with a fixed sized container. What should I do to make this items visible without giving a fixed height to parent container?
I want red container to be dynamic according to content in it.
https://zapp.run/edit/zg5w062ig5x0?theme=dark&lazy=false
Unless the link goes down.
import 'package:flutter/material.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: 'Tab inside body widget',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.green,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Tabs Example'),
),
body: Container(
child: Column(crossAxisAlignment: CrossAxisAlignment.stretch, children: <Widget>[
SizedBox(height: 20.0),
Text('Tabs Inside Body', textAlign: TextAlign.center, style: TextStyle(fontSize: 22)),
DefaultTabController(
length: 4, // length of tabs
initialIndex: 0,
child: Column(crossAxisAlignment: CrossAxisAlignment.stretch, children: <Widget>[
Container(
child: TabBar(
labelColor: Colors.green,
unselectedLabelColor: Colors.black,
tabs: [
Tab(text: 'Tab 1'),
Tab(text: 'Tab 2'),
Tab(text: 'Tab 3'),
Tab(text: 'Tab 4'),
],
),
),
Container(
height: 400, //height of TabBarView
decoration: BoxDecoration(
color: Colors.red,
border: Border(top: BorderSide(color: Colors.grey, width: 0.5))
),
child: TabBarView(children: <Widget>[
Container(
child: Center(
child: Text('Display Tab 1', style: TextStyle(fontSize: 22, fontWeight: FontWeight.bold)),
),
),
Container(
child: Center(
child: Text('Display Tab 2', style: TextStyle(fontSize: 22, fontWeight: FontWeight.bold)),
),
),
Container(
child: Center(
child: Text('Display Tab 3', style: TextStyle(fontSize: 22, fontWeight: FontWeight.bold)),
),
),
Container(
child: Center(
child: Text('Display Tab 4', style: TextStyle(fontSize: 22, fontWeight: FontWeight.bold)),
),
),
])
)
])
),
]),
),
);
}
}

Whenever using a Widget like Column, Row or any other Widgets that has parent Widget with infinite height or width, use Flex or Expanded Widget as per your requirement
Sample Code : -
Expanded(child: TabBarView(children: [],))
Complete Code : -
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Tab inside body widget',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.green,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key});
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Tabs Example'),
),
body: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
const SizedBox(height: 20.0),
const Text('Tabs Inside Body',
textAlign: TextAlign.center, style: TextStyle(fontSize: 22)),
DefaultTabController(
length: 4, // length of tabs
initialIndex: 0,
child: Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
const TabBar(
labelColor: Colors.green,
unselectedLabelColor: Colors.black,
tabs: [
Tab(text: 'Tab 1'),
Tab(text: 'Tab 2'),
Tab(text: 'Tab 3'),
Tab(text: 'Tab 4'),
],
),
Expanded(
child: Container(
decoration: const BoxDecoration(
color: Colors.red,
border: Border(
top: BorderSide(
color: Colors.grey, width: 0.5))),
child: const TabBarView(children: <Widget>[
Center(
child: Text('Display Tab 1',
style: TextStyle(
fontSize: 22,
fontWeight: FontWeight.bold)),
),
Center(
child: Text('Display Tab 2',
style: TextStyle(
fontSize: 22,
fontWeight: FontWeight.bold)),
),
Center(
child: Text('Display Tab 3',
style: TextStyle(
fontSize: 22,
fontWeight: FontWeight.bold)),
),
Center(
child: Text('Display Tab 4',
style: TextStyle(
fontSize: 22,
fontWeight: FontWeight.bold)),
),
])),
)
]),
)),
]),
);
}
}
Output : -

Related

Flutter cannot use Google fonts in text

When I try to use google_fonts I get an error like this. I also imported the library and added " google_fonts: ^3.0.1" to the "pub.yml" file.
" I added.
void main() {
runApp(BenimUyg());
}
class BenimUyg extends StatelessWidget {
const BenimUyg({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
backgroundColor: Colors.brown[300],
body: SafeArea(
child: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: const <Widget>[
CircleAvatar(
backgroundColor: Colors.lime,
backgroundImage: AssetImage("assets/images/kahve.jpg"),
radius: 70,
),
Text(
"Flutter Kahvecisi",
style: TextStyle(
fontSize: 40,
fontFamily: "Courgette",
color: Colors.brown,
),
),
// ignore: unnecessary_const
Text(
"Çok Yakındasınız",
style: GoogleFonts.pacifico(
textStyle: TextStyle(
color: Colors.lime,
),
),
),
],
),
),
),
),
);
}
}
The issue is coming from children: const <Widget>[ remove const.
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[ // here
CircleAvatar(
backgroundColor: Colors.lime,
backgroundImage: AssetImage("assets/images/kahve.jpg"),
radius: 70,
),
class BenimUyg extends StatelessWidget {
const BenimUyg({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
backgroundColor: Colors.brown[300],
body: SafeArea(
child: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
const CircleAvatar(
backgroundColor: Colors.lime,
backgroundImage: AssetImage("assets/images/kahve.jpg"),
radius: 70,
),
const Text(
"Flutter Kahvecisi",
style: TextStyle(
fontSize: 40,
fontFamily: "Courgette",
color: Colors.brown,
),
),
// ignore: unnecessary_const
Text(
"Çok Yakındasınız",
style: GoogleFonts.pacifico(
textStyle: const TextStyle(
color: Colors.lime,
),
),
),
],
),
),
),
),
);
}
}

How can I add the contact details in the prototype to my code?

The page is loading fine but, how can I add the contact details in the center of prototype to the center of the Appbar and the bottom navigation bar? Can anyone tell me how can I add that..
Updated the full code as requested in the comments.
class _MyHomePageState extends State<MyHomePage>
with SingleTickerProviderStateMixin {
String title = 'Tabar / BottomNavigationBar';
TabController _tabController;
#override
void initState() {
super.initState();
_tabController = TabController(length: 5, vsync: this);
}
#override
void dispose() {
super.dispose();
_tabController.dispose();
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
bottom: PreferredSize(child: Container(),
preferredSize: Size.fromHeight(45.0)
),
backgroundColor: Colors.white,
title: Image.asset('images/logo1.jpg',
height: 50.0,
width: 70.0),
),
body: TabBarView(
controller: _tabController,
physics: NeverScrollableScrollPhysics(),
children: [
TopTabBar(),
Center(
child: Text('Accounts',
style: TextStyle(fontSize: 25.0)
),
),
Center(
child: Text('Service',
style: TextStyle(fontSize: 25.0)
),
),
Center(
child: Text('Products',
style: TextStyle(fontSize: 25.0)
),
),
Center(
child: Text('Log Out',
style: TextStyle(fontSize: 25.0)
),
),
],
),
bottomNavigationBar: Container(
color: Colors.amber,
child: TabBar(
controller: _tabController,
unselectedLabelColor: Colors.black,
indicator: UnderlineTabIndicator(
borderSide: BorderSide(width: 0.0),
),
labelColor: Colors.white,
labelStyle: TextStyle(fontSize: 12.0),
tabs: [
Tab(
icon: Icon(Icons.home),
text: 'Home',
),
Tab(
icon: Icon(Icons.account_balance),
text: 'Accounts',
),
Tab(
icon: Icon(Icons.home_repair_service),
text: 'Service',
),
Tab(
icon: Icon(Icons.account_circle_rounded),
text: 'Products',
),
Tab(
icon: Icon(Icons.logout),
text: 'Log Out',
),
],
),
),
);
}
}
class TopTabBar extends StatefulWidget {
TopTabBar({Key key}) : super(key: key);
#override
_TopTabBarState createState() => _TopTabBarState();
}
class _TopTabBarState extends State<TopTabBar>
with SingleTickerProviderStateMixin {
TabController _tabController;
#override
void initState() {
super.initState();
_tabController = TabController(length: 3, vsync: this);
}
#override
void dispose(){
super.dispose();
_tabController.dispose();
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: PreferredSize(
preferredSize: Size.fromHeight(kToolbarHeight),
child: Container(
color: Colors.amber,
child: TabBar(
controller: _tabController,
indicator: UnderlineTabIndicator(
borderSide: BorderSide(width: 0.0),
),
indicatorColor: Colors.white,
unselectedLabelColor: Colors.black,
labelColor: Colors.white,
indicatorWeight: 5.0,
tabs: [
Tab(
text: 'Contact Us'
),
Tab(
text: 'Nearest Branch'
),
Tab(
text: 'Nearest ATM'
),
],
),
),
),
);
}
}
This is my prototype and the interface generated by the code
body: Column(
children: [
TabBarView(
controller: _tabController,
physics: NeverScrollableScrollPhysics(),
children: [
TopTabBar(),
Center(
child: Text('Accounts',
style: TextStyle(fontSize: 25.0)
),
),
Center(
child: Text('Service',
style: TextStyle(fontSize: 25.0)
),
),
Center(
child: Text('Products',
style: TextStyle(fontSize: 25.0)
),
),
Center(
child: Text('Log Out',
style: TextStyle(fontSize: 25.0)
),
),
],
),
Expanded(
child: Center(ContactDetailsWidget())
),
],
)
Add the above code on the body, Here the remaining space will use by your custom widget ContactDetailsWidget where you can display anything

Loading different PDF document using ListView Builder in Flutter

I am a newbie in Flutter and in programming as well.
I have created a ListView with several Cards inside each card points to separate PDF documents which are loaded using the package 'advance_pdf_viewer'. I have done all this hardcoded. But I would like to generate the Cards and the pdf document dynamically. How can I do that?
Here are my existing Codes:
Main.dart file:
import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'c9Bban.dart';
import 'c9Bbanbakoron.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'HSC Textbooks',
theme: ThemeData(
primarySwatch: Colors.cyan,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
#override
void initState() {
super.initState();
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(
'১ম - ১০ম শ্রেণী পাঠ্যবই',
style: TextStyle(
//fontSize: 14,
fontFamily: 'Baloo Da',
),
),
centerTitle: true,
),
body: ListView(children: [
Card(
child: Row(children: <Widget>[
Expanded(
flex: 8, // 20%
child: Container(
child: ListTile(
leading: ConstrainedBox(
constraints: BoxConstraints(
maxWidth: 44,
maxHeight: 44,
),
child: Image.asset('images/bangla bakaron.jpg',
fit: BoxFit.cover),
),
title: Text(
'বাংলা ব্যাকরণ',
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.bold,
fontFamily: 'Baloo da',
),
),
subtitle: Text(
'জাতীয় শিক্ষাক্রম ও পাঠ্যপুস্তক বোর্ড',
style: TextStyle(
fontSize: 12,
fontFamily: 'Baloo da',
),
),
),
),
),
Expanded(
flex: 4, // 20%
child: GestureDetector(
onTap: () {
Navigator.push(context,
MaterialPageRoute(builder: (context) => C9Bbanbakoron()));
},
child: Container(
decoration: BoxDecoration(
border: Border.all(color: Colors.grey[300])),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Icon(FontAwesomeIcons.book, color: Colors.green),
SizedBox(height: 5),
Text('বাংলা ও ইংরেজি ভার্সন',
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 14,
)),
],
),
),
),
),
]),
),
Card(
child: Row(children: <Widget>[
Expanded(
flex: 8, // 20%
child: Container(
child: ListTile(
leading: ConstrainedBox(
constraints: BoxConstraints(
maxWidth: 44,
maxHeight: 44,
),
child: Image.asset('images/BGS.jpg', fit: BoxFit.cover),
),
title: Text(
'বাংলাদেশ ও বিশ্বপরিচয়',
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.bold,
fontFamily: 'Baloo da',
),
),
subtitle: Text(
'জাতীয় শিক্ষাক্রম ও পাঠ্যপুস্তক বোর্ড',
style: TextStyle(
fontSize: 12,
fontFamily: 'Baloo da',
),
),
),
),
),
Expanded(
flex: 2, // 20%
child: GestureDetector(
onTap: () {
Navigator.push(context,
MaterialPageRoute(builder: (context) => C9Bbgs()));
},
child: Container(
decoration: BoxDecoration(
border: Border.all(color: Colors.grey[300])
),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Icon(FontAwesomeIcons.book, color: Colors.green),
SizedBox(height: 5),
Text('বাংলা ভার্সন',
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 14,
)
),
],
),
),
),
),
Expanded(
flex: 2, // 20%
child: GestureDetector(
onTap: () {
Navigator.push(context,
MaterialPageRoute(builder: (context) => C9Ebgs()));
},
child: Container(
decoration: BoxDecoration(
border: Border.all(color: Colors.grey[300])
),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Icon(FontAwesomeIcons.book, color: Colors.green),
SizedBox(height: 5),
Text('ইংরেজি ভার্সন',
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 14,
)
),
],
),
),
),
),
]),
),
]),
);
}
}
c9Bban.dart file (one of the separate files for loading separate PDF documents. Rest of the files are exactly the same except the PDF URL is different):
import 'package:flutter/material.dart';
import 'package:advance_pdf_viewer/advance_pdf_viewer.dart';
class C9Bban extends StatefulWidget {
#override
_MyC9Bban createState() => _MyC9Bban();
}
class _MyC9Bban extends State<C9Bban> {
bool _isLoading = true;
PDFDocument document;
#override
void initState() {
super.initState();
loadDocument();
}
loadDocument() async {
document = await PDFDocument.fromURL('https://storage.googleapis.com/school_books/Class9/ban/Bangla%20Sahitto.pdf');
setState(() => _isLoading = false);
}
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
toolbarHeight: 20,
),
body: Center(
child: _isLoading
? Center(child: CircularProgressIndicator())
: PDFViewer(
document: document,
zoomSteps: 1,
),
),
bottomNavigationBar: BottomAppBar(
child: Container(
height: 85.0,
),
),
),
);
}
}
So far I was able to create this which works for creating the ListView Cards dynamically.
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:advance_pdf_viewer/advance_pdf_viewer.dart';
import 'c9Bban.dart';
import 'c9Bbansohopath.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'HSC Textbooks',
theme: ThemeData(
primarySwatch: Colors.cyan,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
#override
Widget build(BuildContext context) {
List booklist = [
Books(
image: 'Bangla.jpg',
bookname: 'Bangla',
banbookname: 'C9Bban',
),
Books(
image: 'bangla shapath.jpg',
bookname: 'Bangla Sahapath',
banbookname: 'C9Bbansohopath',
),
];
void updateBook(index) {
Navigator.push(context, MaterialPageRoute(builder: (context) => booklist[index].banbookname));
}
return Scaffold(
appBar: AppBar(
title: Text('Book App'),
centerTitle: true,
),
body: ListView.builder(
itemCount: booklist.length,
itemBuilder: (context, index) {
return Card(
child: Row(children: <Widget>[
Expanded(
flex: 8, // 20%
child: Container(
child: ListTile(
leading: ConstrainedBox(
constraints: BoxConstraints(
maxWidth: 44,
maxHeight: 44,
),
child:
Image.asset('images/islam.jpg', fit: BoxFit.cover),
),
title: Text(
booklist[index].bookname,
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.bold,
fontFamily: 'Baloo da',
),
),
subtitle: Text(
'জাতীয় শিক্ষাক্রম ও পাঠ্যপুস্তক বোর্ড',
style: TextStyle(
fontSize: 12,
fontFamily: 'Baloo da',
),
),
),
),
),
Expanded(
flex: 2, // 20%
child: GestureDetector(
onTap: () {
updateBook(index);
},
child: Container(
decoration: BoxDecoration(
border: Border.all(color: Colors.grey[300])),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Icon(FontAwesomeIcons.book, color: Colors.green),
SizedBox(height: 5),
Text('বাংলা ভার্সন',
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 14,
)),
],
),
),
),
),
Expanded(
flex: 2, // 20%
child: GestureDetector(
onTap: () {
updateBook(index);
},
child: Container(
decoration: BoxDecoration(
border: Border.all(color: Colors.grey[300])),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Icon(FontAwesomeIcons.book, color: Colors.green),
SizedBox(height: 5),
Text('ইংরেজি ভার্সন',
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 14,
)),
],
),
),
),
),
]),
);
}),
);
}
}
class Books {
String image;
String bookname;
String banbookname;
String engbook;
Books({this.image, this.bookname, this.banbookname});
}

I want BottomNavigationBar as well as TabBar in flutter without having appBar

I want BottomNavigationBar as well as TabBar in flutter without having appBar, Individually both widgets are working but when combined, it is saying The following assertion was thrown during performLayout():
RenderCustomMultiChildLayoutBox object was given an infinite size during layout.
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Haate Khori',
theme: ThemeData.light().copyWith(
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage>
with SingleTickerProviderStateMixin {
int _selectedIndex = 0;
static const TextStyle optionStyle =
TextStyle(fontSize: 30, fontWeight: FontWeight.bold);
static List<Widget> _widgetOptions = <Widget>[
Donation(),
Text(
'EXPENSES',
style: optionStyle,
),
Text(
'ACTIVITIES',
style: optionStyle,
),
Text(
'GALLERY',
style: optionStyle,
),
Text(
'DONATE US',
style: optionStyle,
),
];
void _onItemTapped(int index) {
setState(() {
_selectedIndex = index;
});
}
#override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
body: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Container(
child: Padding(
padding: const EdgeInsets.all(40.0),
child: SizedBox(
height: 70.0,
),
),
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: [Colors.white, Colors.teal], // whitish to gray
stops: [0.0, 0.8]),
),
),
Container(
padding: EdgeInsets.all(10.0),
child: Text(
'Title goes here',
style: TextStyle(color: Colors.white, fontSize: 25.0),
),
color: Colors.teal,
),
_widgetOptions.elementAt(_selectedIndex),
],
),
bottomNavigationBar: BottomNavigationBar(
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.home),
title: Text('Home'),
backgroundColor: Colors.teal,
),
BottomNavigationBarItem(
icon: Icon(Icons.receipt),
title: Text('Expenses'),
backgroundColor: Colors.teal,
),
BottomNavigationBarItem(
icon: Icon(Icons.fiber_smart_record),
title: Text('Activities'),
backgroundColor: Colors.teal,
),
BottomNavigationBarItem(
icon: Icon(Icons.collections),
title: Text('Gallery'),
backgroundColor: Colors.teal,
),
BottomNavigationBarItem(
icon: Icon(Icons.favorite),
title: Text('Donate us'),
backgroundColor: Colors.teal,
),
],
currentIndex: _selectedIndex,
selectedItemColor: Colors.white,
unselectedItemColor: Colors.white,
onTap: _onItemTapped,
),
),
);
}
}
class Donation extends StatefulWidget {
#override
_DonationState createState() => _DonationState();
}
class _DonationState extends State<Donation>
with SingleTickerProviderStateMixin {
TabController tabController;
#override
void initState() {
// TODO: implement initState
super.initState();
tabController = TabController(length: 2, vsync: this);
}
#override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
backgroundColor: Colors.white,
body: Column(
children: <Widget>[
SizedBox(
height: 10.0,
),
TabBar(
labelColor: Colors.teal,
controller: tabController,
tabs: <Widget>[
Tab(
child: Text('Tab1'),
),
Tab(
child: Text('Tab2'),
)
],
),
Expanded(
child: Container(
child: TabBarView(
controller: tabController,
children: <Widget>[
Text(
'Hello',
style: TextStyle(fontSize: 20.0),
textAlign: TextAlign.center,
),
Text(
'World',
style: TextStyle(
fontSize: 20.0,
),
textAlign: TextAlign.center,
),
],
),
),
),
],
),
),
);
}
}
Target : What I want
&
This is what I am getting
I have attached a code snippet which is from my unComplete web app same can be useful for you I haven't used TabBar in the scaffold tab instead created a row inside the scaffold and used it, you can look into my code and can figure out something for you hope it helps.
import 'package:flutter/material.dart';
import 'package:transportWeb/provider/dimensions.dart';
import 'package:transportWeb/screens/mainScreenViews/aboutUs.dart';
import 'package:transportWeb/screens/mainScreenViews/contactUs.dart';
import 'package:transportWeb/screens/mainScreenViews/home.dart';
import 'package:url_launcher/url_launcher.dart';
import '../constants.dart';
class MainScreen extends StatefulWidget {
#override
_MainScreenState createState() => _MainScreenState();
}
class _MainScreenState extends State<MainScreen>
with SingleTickerProviderStateMixin {
TabController _tabController;
var _currentDateTime = DateTime.now();
var _currentHours;
#override
void initState() {
super.initState();
_currentHours = _currentDateTime.hour;
print(_currentHours);
_tabController = TabController(length: 3, vsync: this, initialIndex: 1);
}
#override
void dispose() {
// TODO: implement dispose
super.dispose();
_tabController.dispose();
}
#override
Widget build(BuildContext context) {
Dimensions(context);
return Scaffold(
floatingActionButton: FloatingActionButton(
tooltip: "Request Call",
backgroundColor: Colors.deepPurpleAccent,
onPressed: () {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text(
"Enter your Phone no",
style: kh3,
),
);
},
);
},
child: Icon(
Icons.phone,
color: Colors.white,
size: 30,
),
),
body: ListView(
children: [
Container(
padding: EdgeInsets.symmetric(horizontal: 20, vertical: 0),
height: 70,
decoration: BoxDecoration(
color: Colors.transparent,
border: Border(
bottom: BorderSide(
color: Colors.grey.shade200,
width: 3,
),
),
),
child: Row(
crossAxisAlignment: CrossAxisAlignment.end,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Padding(
padding: const EdgeInsets.only(bottom: 15),
child: RichText(
text: TextSpan(
style: kh2.copyWith(letterSpacing: 10),
children: <TextSpan>[
TextSpan(text: "JSSR "),
TextSpan(text: "Road"),
TextSpan(
text: "L",
style: kh2.copyWith(color: Colors.red),
),
TextSpan(text: "ines"),
],
),
),
),
Expanded(
child: SizedBox(
width: 0,
height: 0,
),
),
TabBar(
isScrollable: true,
indicatorColor: Colors.black,
indicatorSize: TabBarIndicatorSize.label,
labelColor: Colors.black,
unselectedLabelColor: Colors.grey.withOpacity(0.6),
controller: _tabController,
tabs: [
Tab(
child: Padding(
padding: const EdgeInsets.only(bottom: 10),
child: Text(
"HOME",
style: kh5,
),
),
),
Tab(
child: Padding(
padding: const EdgeInsets.only(bottom: 10),
child: Text(
"ABOUT US",
style: kh5,
),
),
),
Tab(
child: Padding(
padding: const EdgeInsets.only(bottom: 10),
child: Text(
"CONTACT US",
style: kh5,
),
),
),
],
),
GestureDetector(
onTap: () {
launch(
"https://pub.dev/packages/url_launcher#-installing-tab-");
},
child: Container(
margin: EdgeInsets.only(left: 15, bottom: 12),
padding: EdgeInsets.symmetric(vertical: 10, horizontal: 10),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(5),
color: Colors.deepPurpleAccent,
),
child: Text(
"DOWNLOAD APP",
style: kh5.copyWith(color: Colors.white),
),
),
),
],
),
),
Container(
width: double.maxFinite,
height: double.maxFinite,
child: TabBarView(
controller: _tabController,
children: [
Home(),
AboutUs(),
ContactUs(),
],
),
)
],
),
);
}
}

How to change tab bar highlighted color

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"))
])
],
),
)