flutter入门系列教程<三>:tabbar的高度自适用,支持无限滚动 ...

打印 上一主题 下一主题

主题 1043|帖子 1043|积分 3129

配景

在之前的文章中,简介了tabbar组件的利用,它通常用于顶部放置tabbar标签页头,内容全部都是TabbarView的全部内容,且内容通常是占满屏幕的(尽可能大),如下:

但是偶然候我们需要在文章的中心部分利用tabbar,之前也简介了封装的方法,当时的思绪是给tabbarView限定高度、或者利用expand组件包裹,但这样也不是很灵活。
由于,如果tabbarView下面另有其他组件,那么tabbarView的高度就被限定死了。
那么,能否实现tabbarView无论在哪里,上面下面是否有组件时,其高度都能自适用呢?
自定义tabbar

由于tabbarView组件的特性使然,它必须有固定的高度、或者声明为占据尽可能大的高度,所以如果要让tabbar高度自适用,那就得自己封装,即:不利用tabbarView组件,结果图如下:

自定义tabbar源码

以下仅为示例代码,但足可以应付开发中的大部分需求,仅供参考:
  1. import 'package:flutter/material.dart';
  2. class CustomTabBarExample extends StatefulWidget {
  3.   const CustomTabBarExample({super.key});
  4.   @override
  5.   State<CustomTabBarExample> createState() => _CustomTabBarExampleState();
  6. }
  7. class _CustomTabBarExampleState extends State<CustomTabBarExample> {
  8.   int _selectedTabIndex = 0;
  9.   @override
  10.   Widget build(BuildContext context) {
  11.     return DefaultTabController(
  12.       length: 3, // Tab 的数量
  13.       child: Scaffold(
  14.         appBar: AppBar(
  15.           title: const Text('自适用的tabbar'),
  16.         ),
  17.         body: SingleChildScrollView(
  18.           child: Column(
  19.             children: [
  20.               // 顶部部分,可自定义内容
  21.               Container(
  22.                 height: 100,
  23.                 color: Colors.blue,
  24.                 child: const Center(
  25.                   child: Text('Top Section'),
  26.                 ),
  27.               ),
  28.               // TabBar
  29.               TabBar(
  30.                 onTap: (index) {
  31.                   setState(() {
  32.                     _selectedTabIndex = index;
  33.                   });
  34.                 },
  35.                 tabs: const [
  36.                   Tab(text: 'Tab 1'),
  37.                   Tab(text: 'Tab 2'),
  38.                   Tab(text: 'Tab 3'),
  39.                 ],
  40.               ),
  41.               // 根据选中的 Tab 索引显示不同的内容
  42.               _buildTabContent(_selectedTabIndex),
  43.               // 底部部分,可自定义内容
  44.               Container(
  45.                 height: 100,
  46.                 color: Colors.orange,
  47.                 child: const Center(
  48.                   child: Text('Bottom Section'),
  49.                 ),
  50.               ),
  51.             ],
  52.           ),
  53.         ),
  54.       ),
  55.     );
  56.   }
  57.   Widget _buildTabContent(int index) {
  58.     switch (index) {
  59.       case 0:
  60.         return Container(
  61.           height: 70,
  62.           color: Colors.red,
  63.           child: const Center(
  64.             child: Text('Tab 1 Content'),
  65.           ),
  66.         );
  67.       case 1:
  68.         return Container(
  69.           height: 800,
  70.           color: Colors.green,
  71.           child: const Center(
  72.             child: Text('Tab 2 Content'),
  73.           ),
  74.         );
  75.       case 2:
  76.         return Container(
  77.           height: 150,
  78.           color: Colors.yellow,
  79.           child: const Center(
  80.             child: Text('Tab 3 Content'),
  81.           ),
  82.         );
  83.       default:
  84.         return Container();
  85.     }
  86.   }
  87. }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

麻花痒

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表