Flutter自定义TabBar插件ttab_bar的使用
TTAB_BAR
- Created by Rahmatulloh Kholmatov
- Supported by Flucen
Features
- A more customizable and minimalistic design, Flexible design and more efficiency.
Supported platforms
.
.
.
.
.
Parameters
- tabs: Type - List<String>. Tab bar elements in String.
.
- tabBarColor: Type - Color. For tabBar background color.
.
- tappedTabTitleColor: Type - Color. For tapped tab title color.
.
- unTappedTabTitleColor: Type - Color. For untapped tab title color.
.
- initialTappedIndex: Type - int. For initial tapped tab index.
.
Installing
- ‘Add this command to terminal:’
- For flutter projects
flutter pub add ttab_bar
- For dart projects
dart pub add ttab_bar
![](https://i.ibb.co/PrWWzfp/i-Phone-14-1-1.jpg)
Author
-
Rahmatulloh Kholmatov.
-
Youtube channel:
使用 ttab_bar 插件的完整示例
下面是一个完整的示例代码,展示如何在 Flutter 应用程序中使用 ttab_bar
插件。
main.dart
import 'package:flutter/material.dart';
import 'package:ttab_bar/ttab_bar.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: DefaultTabController(
length: 3,
child: Scaffold(
appBar: AppBar(
title: Text('ttab_bar 示例'),
bottom: TabBar(
tabs: [
Tab(text: '首页'),
Tab(text: '消息'),
Tab(text: '设置'),
],
),
),
body: TabBarView(
children: [
Center(child: Text('这是首页')),
Center(child: Text('这是消息页')),
Center(child: Text('这是设置页')),
],
),
),
),
);
}
}
ttab_bar_example.dart
import 'package:flutter/material.dart';
import 'package:ttab_bar/ttab_bar.dart';
class TTabBarExample extends StatefulWidget {
@override
_TTabBarExampleState createState() => _TTabBarExampleState();
}
class _TTabBarExampleState extends State<TTabBarExample> {
int _currentIndex = 0;
void _onItemTapped(int index) {
setState(() {
_currentIndex = index;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('ttab_bar 自定义示例'),
),
body: Center(
child: Text('当前选中的标签为: ${_currentIndex == 0 ? "首页" : _currentIndex == 1 ? "消息" : "设置"}'),
),
bottomNavigationBar: TTabBar(
tabs: ["首页", "消息", "设置"],
initialTappedIndex: _currentIndex,
onTap: (index) {
_onItemTapped(index);
},
),
);
}
}
void main() {
runApp(MaterialApp(
home: TTabBarExample(),
));
}
以上代码展示了如何在 Flutter 中使用 ttab_bar
插件创建一个自定义的 TabBar。第一个示例展示了如何使用默认的 TabBar
,第二个示例则展示了如何使用 ttab_bar
插件创建自定义的 TabBar。
</section>
更多关于Flutter自定义TabBar插件ttab_bar的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter自定义TabBar插件ttab_bar的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,下面是一个关于如何在Flutter中使用自定义TabBar插件ttab_bar
的代码示例。假设你已经在pubspec.yaml
文件中添加了ttab_bar
依赖并运行了flutter pub get
。
首先,确保你的pubspec.yaml
中包含如下依赖:
dependencies:
flutter:
sdk: flutter
ttab_bar: ^最新版本号 # 请替换为实际的最新版本号
然后,你可以按照以下步骤在你的Flutter应用中使用ttab_bar
。
示例代码
import 'package:flutter/material.dart';
import 'package:ttab_bar/ttab_bar.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'TTabBar Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> with SingleTickerProviderStateMixin {
late TabController _tabController;
@override
void initState() {
super.initState();
_tabController = TabController(length: 3, vsync: this);
}
@override
void dispose() {
_tabController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('TTabBar Demo'),
bottom: PreferredSize(
preferredSize: Size.fromHeight(kToolbarHeight),
child: TTabBar(
controller: _tabController,
tabs: [
TTab(
icon: Icon(Icons.home),
title: Text('Home'),
),
TTab(
icon: Icon(Icons.search),
title: Text('Search'),
),
TTab(
icon: Icon(Icons.account_circle),
title: Text('Profile'),
),
],
indicator: TTabIndicator(
color: Colors.blue,
weight: 2.0,
padding: EdgeInsets.symmetric(horizontal: 8.0),
),
labelStyle: TextStyle(fontSize: 14),
unselectedLabelStyle: TextStyle(fontSize: 14, color: Colors.grey),
),
),
),
body: TabBarView(
controller: _tabController,
children: [
Center(child: Text('Home Tab')),
Center(child: Text('Search Tab')),
Center(child: Text('Profile Tab')),
],
),
);
}
}
代码解释
-
依赖导入:首先导入必要的包,包括
flutter/material.dart
和ttab_bar/ttab_bar.dart
。 -
应用入口:在
MyApp
类中,设置应用的主题和主页。 -
主页:
MyHomePage
是一个有状态组件,它管理一个TabController
来控制TabBar的选中状态。 -
初始化:在
initState
方法中初始化TabController
。 -
资源释放:在
dispose
方法中释放TabController
以避免内存泄漏。 -
构建UI:在
build
方法中构建整个UI,包括一个带有自定义TTabBar
的AppBar
和一个与TTabBar
关联的TabBarView
。 -
自定义TabBar:
TTabBar
接受多个TTab
作为标签,并允许你自定义指示器(indicator)、标签样式等。 -
TabBarView:
TabBarView
显示与TTabBar
标签相对应的视图。
注意事项
- 确保
ttab_bar
插件的最新版本与Flutter SDK兼容。 - 根据实际需求调整
TTabBar
和TTab
的参数。
这个示例展示了如何使用ttab_bar
插件创建一个自定义的TabBar布局。你可以根据项目的具体需求进一步定制和扩展这个示例。