1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
| import 'package:flutter/material.dart';
const url = 'http:
class TestPage extends StatefulWidget { @override _TestPageState createState() => _TestPageState(); }
class _TestPageState extends State<TestPage> { var tabTitle = [ '页面1', '页面2', '页面3', ];
@override Widget build(BuildContext context) { return new DefaultTabController( length: tabTitle.length, child: Scaffold( body: new NestedScrollView( headerSliverBuilder: (context, bool) { return [ SliverAppBar( expandedHeight: 200.0, floating: true, pinned: true, flexibleSpace: FlexibleSpaceBar( centerTitle: true, title: Text( "我是可以跟着滑动的title", ), background: Image.network( url, fit: BoxFit.cover, )), ), new SliverPersistentHeader( delegate: new SliverTabBarDelegate( new TabBar( tabs: tabTitle.map((f) => Tab(text: f)).toList(), indicatorColor: Colors.red, unselectedLabelColor: Colors.black, labelColor: Colors.red, ), color: Colors.white, ), pinned: true, ), ]; }, body: TabBarView( children: tabTitle .map((s) => ListView.builder( itemBuilder: (context, int) => Text("123"), itemCount: 50, )) .toList(), ), ), )); } }
class SliverTabBarDelegate extends SliverPersistentHeaderDelegate { final TabBar widget; final Color color;
const SliverTabBarDelegate(this.widget, {this.color}) : assert(widget != null);
@override Widget build( BuildContext context, double shrinkOffset, bool overlapsContent) { return new Container( child: widget, color: color, ); }
@override bool shouldRebuild(SliverTabBarDelegate oldDelegate) { return false; }
@override double get maxExtent => widget.preferredSize.height;
@override double get minExtent => widget.preferredSize.height; }
|