New Flutter coder here, so my existing knowledge of how various components interact is limited. I’ve read through/attempted various tutorials (including several posted here), but am having trouble implementing with my existing code to achieve the desired result.
My existing code displays the following: current view
What I’m trying to achieve is the following: desired view
As pictured, I’d like to create a custom indicator for the top-most TabBar. Any help would be greatly appreciated. Thank you!
HomePage code:
import 'package:fallout/plans.dart';
import 'package:fallout/recipes.dart';
import 'package:flutter/material.dart';
import 'package:flutter_glow/flutter_glow.dart';
class HomePage extends StatefulWidget {
const HomePage({super.key});
@override
_HomePageState createState() => _HomePageState();
}
const myColor = Color(0xFFF8EA00);
class _HomePageState extends State<HomePage> {
@override
Widget build(BuildContext context) {
return DefaultTabController(
length: 5,
child: Scaffold(
appBar: AppBar(
backgroundColor: Colors.black,
title: const Center(
child: TabBar(
indicatorColor: Colors.black,
indicatorWeight: 2,
dividerColor: myColor,
isScrollable: true,
tabs: <Widget>[
Tab(
child: GlowText(
blurRadius: 3,
'HOME',
style: TextStyle(color: myColor, fontSize: 18.0),
),
),
Tab(
child: GlowText(
blurRadius: 3,
'PLANS',
style: TextStyle(color: myColor, fontSize: 18.0),
),
),
Tab(
child: GlowText(
blurRadius: 3,
'RECIPES',
style: TextStyle(color: myColor, fontSize: 18.0),
),
),
Tab(
child: GlowText(
blurRadius: 3,
'CREATURES',
style: TextStyle(color: myColor, fontSize: 18.0),
),
),
Tab(
child: GlowText(
blurRadius: 3,
'PLANTS',
style: TextStyle(color: myColor, fontSize: 18.0),
),
),
],
),
),
),
body: TabBarView(
children: <Widget>[
const Text('Home'),
PlansTabs(0xffff5722),
RecipesTabs(0xffff5722),
const Text('Creatures'),
const Text('Plants'),
],
),
),
);
}
}
Plans Tab Code:
import 'package:flutter/material.dart';
import 'package:flutter_glow/flutter_glow.dart';
class PlansTabs extends StatefulWidget {
PlansTabs(this.colorVal);
int colorVal;
_PlansTabsState createState() => _PlansTabsState();
}
class _PlansTabsState extends State<PlansTabs>
with SingleTickerProviderStateMixin {
late TabController _tabController;
@override
void initState() {
super.initState();
_tabController = new TabController(vsync: this, length: 4);
_tabController.addListener(_handleTabSelection);
}
void _handleTabSelection() {
setState(() {
widget.colorVal = 0xffff5722;
});
}
@override
Widget build(BuildContext context) {
return DefaultTabController(
length: 4,
child: Scaffold(
appBar: AppBar(
backgroundColor: Colors.black,
toolbarHeight: double.minPositive,
bottom: TabBar(
controller: _tabController,
isScrollable: true,
indicatorColor: Color(0xFFF8EA00),
indicatorWeight: 1,
dividerColor: Colors.black,
tabs: const <Widget>[
Tab(
child: GlowText(
blurRadius: 3,
'ARMOR',
style: TextStyle(color: Color(0xFFF8EA00), fontSize: 18.0),
),
),
Tab(
child: GlowText(
blurRadius: 3,
'C.A.M.P.',
style: TextStyle(color: Color(0xFFF8EA00), fontSize: 18.0),
),
),
Tab(
child: GlowText(
blurRadius: 3,
'MODS',
style: TextStyle(color: Color(0xFFF8EA00), fontSize: 18.0),
),
),
Tab(
child: GlowText(
blurRadius: 3,
'WEAPONS',
style: TextStyle(color: Color(0xFFF8EA00), fontSize: 18.0),
),
),
],
),
),
body: TabBarView(
controller: _tabController,
children: <Widget>[
//PlansTabs(0xffff5722),
Container(1
height: 200.0,
child: Center(child: Text('Armor Text')),
),
Container(
height: 200.0,
child: Center(child: Text('C.A.M.P. Text')),
),
Container(
height: 200.0,
child: Center(child: Text('Mods Text')),
),
Container(
height: 200.0,
child: Center(child: Text('Weapons Text')),
),
],
),
),
);
}
}
I’ve tried referencing the following solutions, but can’t seem to integrate it properly with my code:
2
Answers
I think it’s not possible to achieve the desired style within Flutter built-in
TabBar
widget. You’ve to make your own Custom TabBar widget. I’ve tried many things before to style tabbar. Hope you’ll get a hint from this.demo:
code: