12.5 Tab 页控制栏
实现步骤:
第 1 步:控制器
lib/pages/goods/product_details/controller.dart
1 2
| class ProductDetailsController extends GetxController with GetSingleTickerProviderStateMixin {
|
GetSingleTickerProviderStateMixin
是 TickerProvider 的实现,
当需要使用 Animation controller 时,需要在控制器初始化时传递一个 vsync 参数,此时需要用到 TickerProvider
1 2 3 4
| late TabController tabController;
int tabIndex = 0;
|
1 2 3 4 5 6 7 8
| _initData() async { await _loadProduct();
tabController = TabController(length: 3, vsync: this);
update(["product_details"]); }
|
1 2 3 4 5 6
| void onTapBarTap(int index) { tabIndex = index; tabController.animateTo(index); update(["product_tab"]); }
|
1 2 3 4 5
| @override void onClose() { super.onClose(); tabController.dispose(); }
|
第 2 步:视图
lib/pages/goods/product_details/view.dart
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| Widget _buildTabBarItem(String textString, int index) { return ButtonWidget.textFilled( textString, onTap: () => controller.onTapBarTap(index), borderRadius: 17, textColor: controller.tabIndex == index ? AppColors.onPrimary : AppColors.secondary, bgColor: controller.tabIndex == index ? AppColors.primary : Colors.transparent, ).tight( width: 100.w, height: 35.h, ); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| Widget _buildTabBar() { return GetBuilder<ProductDetailsController>( tag: tag, id: "product_tab", builder: (_) { return <Widget>[ _buildTabBarItem(LocaleKeys.gDetailTabProduct.tr, 0), _buildTabBarItem(LocaleKeys.gDetailTabDetails.tr, 1), _buildTabBarItem(LocaleKeys.gDetailTabReviews.tr, 2), ].toRow( mainAxisSize: MainAxisSize.max, mainAxisAlignment: MainAxisAlignment.center, ); }, ); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| Widget _buildTabView() { return Expanded( child: Padding( padding: EdgeInsets.fromLTRB(20.w, 0.w, 20.w, 0.w), child: TabBarView( controller: controller.tabController, children: [ TabProductView(uniqueTag: uniqueTag), TabDetailView(uniqueTag: uniqueTag), TabReviewsView(uniqueTag: uniqueTag), ], ), ), ); }
|
提交代码到 git