import 'dart:math'; import 'package:flutter/material.dart'; import 'package:jiameng_repast/base/base_packutils.dart'; const url = 'http://www.pptbz.com/pptpic/UploadFiles_6909/201203/2012031220134655.jpg'; class TestPage extends StatefulWidget { static const String rTestPage = "rTestPage"; @override _TestPageState createState() => _TestPageState(); } class _TestPageState extends State<TestPage> with SingleTickerProviderStateMixin { TabController _tabController; var tabTitle = [ '页面1', '页面2', '页面3', ]; Widget _buildSliverList() { return SliverList( delegate: SliverChildBuilderDelegate( (BuildContext context, int index) { return Container( child: Text("1111111111111"), height: 100, ); }, childCount: 20, ), ); } Widget _buildPersistentHeader() => SliverPersistentHeader( pinned: false, delegate: _SliverAppBarTabDelegate( child: PreferredSize( preferredSize: Size.fromHeight(45.0), child: Container( color: Colors.white, child: TabBar( controller: _tabController, tabs: tabTitle.map((f) => Tab(text: f)).toList(), indicatorColor: Colors.red, unselectedLabelColor: Colors.black, labelColor: Colors.red, ), ), ), )); Widget _buildPersistentHeader2() => SliverPersistentHeader( pinned: true, delegate: SliverDelegate( minHeight: 50.0, maxHeight: 50.0, child: Container( child: Text("222222222222"), height: 50, color: Colors.red.withOpacity(0.5), ), )); @override void initState() { _tabController = TabController(initialIndex: 0, length: tabTitle.length, vsync: this); super.initState(); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text( "榜单", style: pingFangBold(18.sp, color: ZColors.co333333), ), centerTitle: true, elevation: 0, ), body: new CustomScrollView( slivers: <Widget>[ _buildPersistentHeader(), _buildPersistentHeader2(), // _buildSliverList(), SliverFillRemaining( child: TabBarView( controller: _tabController, children: <Widget>[ Center(child: Text('Content of Home')), Center(child: Text('Content of Home')), Center(child: Text('Content of Profile')), ], ), ), // new SliverFillRemaining( // child: TabBarView( // children: tabTitle // .map((s) => ListView.builder( // itemBuilder: (context, int) => Text("123"), // itemCount: 50, // )) // .toList(), // ), // ) ], ), ); } } class SliverDelegate extends SliverPersistentHeaderDelegate { SliverDelegate({ @required this.minHeight, @required this.maxHeight, @required this.child, }); final double minHeight; //最小高度 final double maxHeight; //最大高度 final Widget child; //子节点 @override double get minExtent => minHeight; @override double get maxExtent => max(maxHeight, minHeight); @override Widget build(BuildContext context, double shrinkOffset, bool overlapsContent) { return new SizedBox.expand(child: child); } @override //是否需要重建 bool shouldRebuild(SliverDelegate oldDelegate) { return maxHeight != oldDelegate.maxHeight || minHeight != oldDelegate.minHeight || child != oldDelegate.child; } } class _SliverAppBarTabDelegate extends SliverPersistentHeaderDelegate { final PreferredSize child; _SliverAppBarTabDelegate({this.child}); @override Widget build(BuildContext context, double shrinkOffset, bool overlapsContent) { return SizedBox.expand(child: child); } @override double get maxExtent => child.preferredSize.height; @override double get minExtent => child.preferredSize.height; @override bool shouldRebuild(SliverPersistentHeaderDelegate oldDelegate) { return false; } }
flutter TabBar 和TabBarVIew 并且 TabBar 实现滑动隐藏
最新推荐文章于 2022-02-17 16:11:46 发布