dev
dart 79 lines 2.27 KB
Raw
1 import 'package:flutter/material.dart';
2
3 class TabViewWrapper extends StatefulWidget {
4 const TabViewWrapper({
5 super.key,
6 required this.tabs,
7 required this.views,
8 this.tabBarPadding = const EdgeInsets.only(right: 24),
9 this.labelStyle,
10 this.unselectedLabelStyle,
11 this.indicatorColor,
12 }) : assert(tabs.length == views.length, 'Tabs and views must be of equal length.');
13
14 final List<Tab> tabs;
15 final List<Widget> views;
16 final EdgeInsets tabBarPadding;
17 final TextStyle? labelStyle;
18 final TextStyle? unselectedLabelStyle;
19 final Color? indicatorColor;
20
21 @override
22 State<TabViewWrapper> createState() => _TabViewWrapperState();
23 }
24
25 class _TabViewWrapperState extends State<TabViewWrapper> with SingleTickerProviderStateMixin {
26 late final TabController _tabController;
27
28 @override
29 void initState() {
30 super.initState();
31 _tabController = TabController(length: widget.tabs.length, vsync: this);
32 }
33
34 @override
35 void dispose() {
36 _tabController.dispose();
37 super.dispose();
38 }
39
40 @override
41 Widget build(BuildContext context) {
42 final textStyle = TextStyle(
43 fontSize: 18,
44 fontFamily: 'Lato',
45 fontWeight: FontWeight.w600,
46 color: Theme.of(context).colorScheme.onSurface);
47
48 return Column(
49 children: [
50 Align(
51 alignment: Alignment.centerLeft,
52 child: TabBar(
53 controller: _tabController,
54 isScrollable: true,
55 splashFactory: NoSplash.splashFactory,
56 indicatorSize: TabBarIndicatorSize.label,
57 labelStyle: widget.labelStyle ?? textStyle,
58 unselectedLabelStyle: widget.unselectedLabelStyle ??
59 textStyle.copyWith(color: textStyle.color?.withAlpha(150)),
60 labelColor: widget.labelStyle?.color ?? textStyle.color,
61 indicatorColor: widget.indicatorColor,
62 indicatorPadding: EdgeInsets.zero,
63 labelPadding: widget.tabBarPadding,
64 tabAlignment: TabAlignment.start,
65 dividerColor: Colors.transparent,
66 padding: EdgeInsets.zero,
67 tabs: widget.tabs,
68 ),
69 ),
70 Expanded(
71 child: TabBarView(
72 controller: _tabController,
73 children: widget.views,
74 ),
75 ),
76 ],
77 );
78 }
79 }