Flutter水平选择器插件horizontal_picker_tabs的使用
Flutter水平选择器插件horizontal_picker_tabs的使用
特性


开始使用
在 pubspec.yaml
文件中添加以下依赖项:
dependencies:
horizontal_picker_tabs: ^版本号
然后运行以下命令以安装依赖:
dart pub add horizontal_picker_tabs
使用方法
以下是完整的示例代码,展示如何使用 horizontal_picker_tabs
插件:
import 'package:flutter/material.dart';
import 'package:horizontal_picker_tabs/horizontal_picker_tabs.dart';
import 'package:horizontal_picker_tabs/picker_tabs.dart';
void main() {
runApp(MaterialApp(home: MyApp()));
}
class MyApp extends StatefulWidget {
List<String> tabs = ["选项1", "选项2", "选项3", "选项4", "选项5"]; // 定义选项标签
MyApp({Key? key}) : super(key: key);
[@override](/user/override)
State<StatefulWidget> createState() {
return MyAppState();
}
}
class MyAppState extends State<MyApp> with SingleTickerProviderStateMixin {
late TabController _tabController;
int currentPage = 0;
TextStyle labelStyle =
const TextStyle(fontSize: 16, fontWeight: FontWeight.w600); // 选中标签样式
TextStyle unselectedLabelStyle = const TextStyle(
fontSize: 16,
fontWeight: FontWeight.w600,
); // 未选中标签样式
[@override](/user/override)
void initState() {
super.initState();
_tabController = TabController(vsync: this, length: widget.tabs.length)
..addListener(() {
if (currentPage != _tabController.index) {
setState(() {
currentPage = _tabController.index;
});
}
});
}
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
resizeToAvoidBottomInset: false,
backgroundColor: Colors.black,
body: Stack(children: [
PickerTabBarView(
controller: _tabController,
children: getTabViewList(), // 渲染每个选项对应的页面
),
// 装饰底部卡尺
Positioned(
bottom: 0,
left: 0,
right: 0,
child: Container(
height: 100,
color: Colors.black54,
),
),
// 装饰底部图标
const Positioned(
bottom: 14,
left: 0,
right: 0,
child: Image(
height: 20,
fit: BoxFit.fill,
image: AssetImage("assets/images/lm_horizontal_picker_bottom_icon.png"),
),
),
// 装饰滑块
Positioned(
bottom: 14,
left: 0,
right: 0,
child: Center(
child: Container(
margin: const EdgeInsets.only(top: 2),
width: 2,
height: 16,
color: const Color(0xFF7F5EFF),
),
),
),
// 水平选项卡栏
Column(
mainAxisAlignment: MainAxisAlignment.end,
children: [
HorizontalPickerTabBar(
controller: _tabController,
padding: const EdgeInsets.only(top: 15, bottom: 5),
labelColor: Colors.white, // 选中标签颜色
labelStyle: labelStyle,
unselectedLabelColor: const Color(0x99FFFFFF), // 未选中标签颜色
unselectedLabelStyle: unselectedLabelStyle,
tabs: widget.tabs, // 设置选项标签
),
],
)
]),
);
}
[@override](/user/override)
void dispose() {
// 释放 TabController
_tabController.dispose();
super.dispose();
}
getTabViewList() {
List<Widget> list = [];
for (int i = 0; i < widget.tabs.length; i++) {
list.add(Image.asset("assets/images/img_$i.webp", fit: BoxFit.cover)); // 渲染每个选项的页面内容
}
return list;
}
}
更多关于Flutter水平选择器插件horizontal_picker_tabs的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复
更多关于Flutter水平选择器插件horizontal_picker_tabs的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
horizontal_picker_tabs
是一个用于 Flutter 的水平选择器插件,它允许用户通过水平滑动来选择不同的选项。这个插件通常用于需要用户从一组选项中进行选择的场景,比如日期选择、时间选择、或者简单的选项选择。
安装
首先,你需要在 pubspec.yaml
文件中添加 horizontal_picker_tabs
插件的依赖:
dependencies:
flutter:
sdk: flutter
horizontal_picker_tabs: ^1.0.0 # 请使用最新版本
然后运行 flutter pub get
来安装依赖。
基本用法
以下是一个简单的示例,展示如何使用 horizontal_picker_tabs
插件:
import 'package:flutter/material.dart';
import 'package:horizontal_picker_tabs/horizontal_picker_tabs.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Horizontal Picker Tabs Example'),
),
body: Center(
child: HorizontalPickerTabs(
tabs: ['Option 1', 'Option 2', 'Option 3', 'Option 4'],
onTabChanged: (index) {
print('Selected tab: $index');
},
),
),
),
);
}
}
参数说明
tabs
: 一个字符串列表,表示每个选项卡的标签。onTabChanged
: 当用户选择不同的选项卡时触发的回调函数,返回当前选中的选项卡的索引。initialIndex
: 初始选中的选项卡索引,默认为0
。tabWidth
: 每个选项卡的宽度,默认为100.0
。tabHeight
: 选项卡的高度,默认为50.0
。tabColor
: 选项卡的背景颜色,默认为Colors.blue
。selectedTabColor
: 选中选项卡的背景颜色,默认为Colors.blueAccent
。textStyle
: 选项卡文本的样式,默认为TextStyle(color: Colors.white)
。selectedTextStyle
: 选中选项卡文本的样式,默认为TextStyle(color: Colors.white, fontWeight: FontWeight.bold)
。
自定义样式
你可以通过调整 tabWidth
、tabHeight
、tabColor
、selectedTabColor
、textStyle
和 selectedTextStyle
等参数来自定义水平选择器的外观。
HorizontalPickerTabs(
tabs: ['Option 1', 'Option 2', 'Option 3', 'Option 4'],
onTabChanged: (index) {
print('Selected tab: $index');
},
initialIndex: 1,
tabWidth: 120.0,
tabHeight: 60.0,
tabColor: Colors.grey[300],
selectedTabColor: Colors.blue,
textStyle: TextStyle(color: Colors.black),
selectedTextStyle: TextStyle(color: Colors.white, fontWeight: FontWeight.bold),
)