I'm having a Tab Layout at top of the screen and Bottom Navigation view at bottom of the screen.
I need to have a container in the middle and swap the views which is similar to fragment transactions in Android.
How to acheive this in Flutter ?
home: DefaultTabController(
length: prefList.length,
child: Scaffold(
appBar: AppBar(
backgroundColor: const Color(0xff00234a),
bottom: TabBar(
tabs: tabList,
isScrollable: true,
indicatorColor: const Color(0xffffffff),
),
title: Text('Test Demo'),
centerTitle: true,
),
body: Container(
child: TabBarView(
children: tabList,
controller: controller,
),
),
// Set the bottom navigation bar
bottomNavigationBar: Material(
// set the color of the bottom navigation bar
color: const Color(0xff00234a),
// set the tab bar as the child of bottom navigation bar
child: TabBar(
tabs: <Tab>[
Tab(
icon: Icon(Icons.favorite),
),
Tab(
icon: Icon(Icons.adb),
),
Tab(
icon: Icon(Icons.airplay),
),
Tab(
icon: Icon(Icons.gamepad),
),
Tab(
icon: Icon(Icons.videogame_asset),
),
],
// setup the controller
controller: controller,
),
),
),
),
);
}
I need to replace the TabView widget with something like Container layout in Android which is used to replace different fragments !
from flutter documentation you can achieve a bottom nav using this
int _selectedIndex = 0;
static const TextStyle optionStyle = TextStyle(fontSize: 30, fontWeight: FontWeight.bold);
static const List<Widget> _widgetOptions = <Widget>[
Text(
'Index 0: Home',
style: optionStyle,
),
Text(
'Index 1: Business',
style: optionStyle,
),
Text(
'Index 2: School',
style: optionStyle,
),
];
void _onItemTapped(int index) {
setState(() {
_selectedIndex = index;
});
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('BottomNavigationBar Sample'),
),
body: Center(
child: _widgetOptions.elementAt(_selectedIndex),
),
bottomNavigationBar: BottomNavigationBar(
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.home),
title: Text('Home'),
),
BottomNavigationBarItem(
icon: Icon(Icons.business),
title: Text('Business'),
),
BottomNavigationBarItem(
icon: Icon(Icons.school),
title: Text('School'),
),
],
currentIndex: _selectedIndex,
selectedItemColor: Colors.amber[800],
onTap: _onItemTapped,
),
);
}
for tabbed screen
//lifted from a project im doing
#override
Widget build(BuildContext context) {
return DefaultTabController(
length: 4,
child: Scaffold(
appBar: AppBar(centerTitle: true,
title: Text("tabs", style: TextStyle(fontWeight: FontWeight.bold, color: Colors.white)),
bottom: TabBar(
indicatorColor: Colors.white,
tabs: <Widget>[
Tab(
icon: Image.asset(
"assets/icons/tools.png",
height: 38,
),
child: Text("s1",style: TextStyle(fontSize: 10),),
),
Tab(
icon: Image.asset(
"assets/icons/il.png",
height: 38,
),
child: Text("s2",style: TextStyle(fontSize: 10)),
),
Tab(
icon: Icon(Icons.lock),
child: Text("s3"),
),
Tab(
icon: Icon(Icons.lock),
child: Text("s4"),
),
],
),
),
body: TabBarView(children: <Widget>[
Screen1(),
Screen2(),
Screen3(),
Screen4()
],),
),
);
Related
I need to do this view(tab bar)
i dont know how to do such outline
You have t define the TabBar in the Appbar and the content of the tabs in the body with TabBarView:
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(
icon: Icon(Icons.cloud_outlined),
),
Tab(
icon: Icon(Icons.beach_access_sharp),
),
Tab(
icon: Icon(Icons.brightness_5_sharp),
),
],
),
),
body: const TabBarView(
children: <Widget>[
Center(
child: Text('It\'s cloudy here'),
),
Center(
child: Text('It\'s rainy here'),
),
Center(
child: Text('It\'s sunny here'),
),
],
),
),
);
}
enter image description here
DefaultTabController(
length: 3,
initialIndex: 1,
child: Scaffold(
bottomNavigationBar: BottomNavigationBar(
type: BottomNavigationBarType.fixed,
backgroundColor: Colors.grey[900],
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.offline_bolt),
title: Text('Shorts'),
),
BottomNavigationBarItem(
icon: Icon(Icons.graphic_eq),
title: Text('Activity'),
),
BottomNavigationBarItem(
icon: Icon(Icons.cloud_upload),
title: Text('Upload'),
),
BottomNavigationBarItem(
icon: Icon(Icons.email),
title: Text('Messages'),
),
BottomNavigationBarItem(
icon: Icon(Icons.menu),
title: Text('More'),
),
],
currentIndex: _selectedIndex,
selectedItemColor: Colors.white,
unselectedItemColor: Colors.grey,
onTap: _onItemTapped,
),
body: NestedScrollView(
controller: _scrollViewController,
headerSliverBuilder: (BuildContext context, bool boxIsScrolled) {
return <Widget>[
SliverAppBar(
centerTitle: true,
leading: IconButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => HeadingCircle()));
},
icon: Icon(Icons.account_circle),
),
actions: [
IconButton(
color: Colors.grey,
icon: Icon(Icons.search),
onPressed: () {},
),
],
backgroundColor: Colors.grey[900],
title: Text(
'dribbble',
style: TextStyle(fontFamily: 'Pacifico'),
),
pinned: true,
floating: true,
forceElevated: boxIsScrolled,
bottom: TabBar(
tabs: <Widget>[
Tab(
text: "Following",
),
Tab(
text: "Popular",
),
Tab(
text: "Recent",
)
],
controller: _tabController,
),
),
];
},
body: TabBarView(
children: <Widget>[
Tab(
child: Text('1'),
),
Tab(
child: Text('2'),
),
Tab(
child: Text('3'),
),
],
controller: _tabController,
),
),
),
);
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),
),
],
),
);
}
}