Tabs 标签页
标签页组件,用于组织和展示多个相关页面内容,用户可以通过点击标签在不同页面间切换。
何时使用
当需要展示多个平级页面内容,且希望用户能够在这些页面间自由切换时使用。
代码演示
import 'package:antd_flutter_example/demo_block.dart';
import 'package:flutter/widgets.dart';
import 'package:trionesdev_antd_mobile/trionesdev_antd_mobile.dart';
class TabsPage extends StatefulWidget {
const TabsPage({super.key});
@override
State<StatefulWidget> createState() => _TabsPageState();
}
class _TabsPageState extends State<TabsPage> {
final List<AntTabItemStruct> _tabItems = [
AntTabItemStruct(key: "1", label: Text("Tab1"),content: Text("Tab1 Content")),
AntTabItemStruct(key: "2", label: Text("Tab2"),content: Text("Tab2 Content")),
AntTabItemStruct(key: "3", label: Text("Tab3"),content: Text("Tab3 Content")),
];
final List<AntTabItemStruct> _tabItems2 = [
AntTabItemStruct(key: "1", label: Container(padding: EdgeInsets.symmetric(horizontal: 20),child: Text("Tab1" ),),content: Text("Tab1 Content 1")),
AntTabItemStruct(key: "2", label: Container(padding: EdgeInsets.symmetric(horizontal: 20),child: Text("Tab2" ),),content: Text("Tab1 Content 2")),
AntTabItemStruct(key: "3", label: Container(padding: EdgeInsets.symmetric(horizontal: 20),child: Text("Tab3" ),),content: Text("Tab1 Content 3")),
AntTabItemStruct(key: "4", label: Container(padding: EdgeInsets.symmetric(horizontal: 20),child: Text("Tab4" ),),content: Text("Tab1 Content 4")),
AntTabItemStruct(key: "5", label: Container(padding: EdgeInsets.symmetric(horizontal: 20),child: Text("Tab5" ),),content: Text("Tab1 Content 5")),
AntTabItemStruct(key: "6", label: Container(padding: EdgeInsets.symmetric(horizontal: 20),child: Text("Tab6" ),),content: Text("Tab1 Content 6")),
AntTabItemStruct(key: "7", label: Container(padding: EdgeInsets.symmetric(horizontal: 20),child: Text("Tab7" ),),content: Text("Tab1 Content 7")),
AntTabItemStruct(key: "8", label: Container(padding: EdgeInsets.symmetric(horizontal: 20),child: Text("Tab8" ),),content: Text("Tab1 Content 8")),
AntTabItemStruct(key: "9", label: Container(padding: EdgeInsets.symmetric(horizontal: 20),child: Text("Tab9" ),),content: Text("Tab1 Content 9")),
AntTabItemStruct(key: "10", label: Container(padding: EdgeInsets.symmetric(horizontal: 20),child: Text("Tab10" ),),content: Text("Tab1 Content 10")),
];
@override
Widget build(BuildContext context) {
return AntScaffold(
appBar: AntAppBar(
title: Text("Tabs 标签页"),
),
body: SingleChildScrollView(
child: Column(
children: [
DemoBlock(
title: "基本用法",
child: Container(
height: 100,
child: AntTabs(
items: _tabItems,
),
),
),
DemoBlock(
title: "滚动展示",
child: Container(
height: 100,
child: AntTabs(
items: _tabItems2,
stretch: false,
),
),
),
DemoBlock(
title: "Widget方式",
child: Container(
height: 100,
child: AntTabs(
stretch: true,
children: [
AntTab(
antKey: "tab1",
label: Text("Tab1"),
child: Text("Tab1 Content"),
),
AntTab(
antKey: "tab2",
label: Text("Tab2"),
child: Text("Tab2 Content"),
),
],
),
),
),
DemoBlock(
title: "默认激活Tab",
child: Container(
height: 100,
child: AntTabs(
stretch: true,
defaultActiveKey: "tab2",
children: [
AntTab(
antKey: "tab1",
label: Text("Tab1"),
child: Text("Tab1 Content"),
),
AntTab(
antKey: "tab2",
label: Text("Tab2"),
child: Text("Tab2 Content"),
),
],
),
),
),
],
),
),
);
}
}
API
AntTabs
标签页的属性说明如下:
| 属性 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| defaultActiveKey | 默认激活的tabKey | String? | null |
| activeKey | 当前激活的tabKey | String? | null |
| items | 子项 | List<AntTabItemStruct>? | null |
| stretch | 是否拉伸 | bool | true |
| children | 子项 | List<AntTab>? | null |
| decoration | 装饰 | BoxDecoration? | null |
| tabDecoration | tab项的样式 | BoxDecoration? | null |
| style | 样式 | StateStyle? | null |
| styles | 自定义样式 | AntTabStyles? | null |
| itemBuilder | 自定义tab项 | Widget Function(AntTabItemStruct item)? | null |
AntTabItemStruct
| 属性 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| key | 标识(必填) | String | - |
| labelText | 标签文本 | String? | null |
| label | 标签 | Widget? | null |
| content | 页面内容 | Widget? | null |
AntTab
标签项的属性说明如下:
| 属性 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| antKey | 标识(必填) | String | - |
| labelText | 标签文本 | String? | null |
| label | 标签 | Widget? | null |
| child | 子组件 | Widget? | null |
| decoration | 装饰 | BoxDecoration? | null |
| onTab | tab点击事件 | Function(AntTabState? state)? | null |
| style | 样式 | StateStyle? | null |
| activeStyle | 激活样式 | StateStyle? | null |
| bodyStyle | 内容样式 | StateStyle? | null |