如何在Flutter中将TabBar添加到SliverAppBar?到目前为止,当我bottom
向SliverAppBar 添加a 时,title
会固定到这些选项卡,而我希望它位于这些选项卡上方。
有任何想法吗?
我的代码:
class _MyHomePageState extends Statewith SingleTickerProviderStateMixin{ TabController controller; @override void initState() { super.initState(); controller = new TabController(length: 3, vsync: this); } @override Widget build(BuildContext context) { return new Scaffold( body: new CustomScrollView( slivers: [ new SliverAppBar( pinned: true, flexibleSpace: new FlexibleSpaceBar( title: new Text("Some title"), ), expandedHeight: 160.0, bottom: new TabBar(tabs: [ new Tab(text: 'Tab 1'), new Tab(text: 'Tab 2'), new Tab(text: 'Tab 3'), ], controller: controller, ), ), new SliverList( delegate: new SliverChildBuilderDelegate( (context, idx) { return new ListTile( title: new Text("$idx"), ); }, childCount: 20, ), ), ], ), ); } }
diegoveloper.. 19
我有同样的问题星期前,我可以解决使用SliverAppBar
,FlexibleSpaceBar
和SliverPersistentHeader
。
检查我的代码
@override Widget build(BuildContext context) { return Scaffold( body: DefaultTabController( length: 2, child: NestedScrollView( headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) { return[ SliverAppBar( expandedHeight: 200.0, floating: false, pinned: true, flexibleSpace: FlexibleSpaceBar( centerTitle: true, title: Text("Collapsing Toolbar", style: TextStyle( color: Colors.white, fontSize: 16.0, )), background: Image.network( "https://images.pexels.com/photos/396547/pexels-photo-396547.jpeg?auto=compress&cs=tinysrgb&h=350", fit: BoxFit.cover, )), ), SliverPersistentHeader( delegate: _SliverAppBarDelegate( TabBar( labelColor: Colors.black87, unselectedLabelColor: Colors.grey, tabs: [ Tab(icon: Icon(Icons.info), text: "Tab 1"), Tab(icon: Icon(Icons.lightbulb_outline), text: "Tab 2"), ], ), ), pinned: true, ), ]; }, body: Center( child: Text("Sample text"), ), ), ), ); }
这是SliverPersistentHeader的实现
class _SliverAppBarDelegate extends SliverPersistentHeaderDelegate { _SliverAppBarDelegate(this._tabBar); final TabBar _tabBar; @override double get minExtent => _tabBar.preferredSize.height; @override double get maxExtent => _tabBar.preferredSize.height; @override Widget build( BuildContext context, double shrinkOffset, bool overlapsContent) { return new Container( child: _tabBar, ); } @override bool shouldRebuild(_SliverAppBarDelegate oldDelegate) { return false; } }
如果您要检查,我写了一篇关于它的文章:
https://medium.com/@diegoveloper/flutter-collapsing-toolbar-sliver-app-bar-14b858e87abe
我有同样的问题星期前,我可以解决使用SliverAppBar
,FlexibleSpaceBar
和SliverPersistentHeader
。
检查我的代码
@override Widget build(BuildContext context) { return Scaffold( body: DefaultTabController( length: 2, child: NestedScrollView( headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) { return[ SliverAppBar( expandedHeight: 200.0, floating: false, pinned: true, flexibleSpace: FlexibleSpaceBar( centerTitle: true, title: Text("Collapsing Toolbar", style: TextStyle( color: Colors.white, fontSize: 16.0, )), background: Image.network( "https://images.pexels.com/photos/396547/pexels-photo-396547.jpeg?auto=compress&cs=tinysrgb&h=350", fit: BoxFit.cover, )), ), SliverPersistentHeader( delegate: _SliverAppBarDelegate( TabBar( labelColor: Colors.black87, unselectedLabelColor: Colors.grey, tabs: [ Tab(icon: Icon(Icons.info), text: "Tab 1"), Tab(icon: Icon(Icons.lightbulb_outline), text: "Tab 2"), ], ), ), pinned: true, ), ]; }, body: Center( child: Text("Sample text"), ), ), ), ); }
这是SliverPersistentHeader的实现
class _SliverAppBarDelegate extends SliverPersistentHeaderDelegate { _SliverAppBarDelegate(this._tabBar); final TabBar _tabBar; @override double get minExtent => _tabBar.preferredSize.height; @override double get maxExtent => _tabBar.preferredSize.height; @override Widget build( BuildContext context, double shrinkOffset, bool overlapsContent) { return new Container( child: _tabBar, ); } @override bool shouldRebuild(_SliverAppBarDelegate oldDelegate) { return false; } }
如果您要检查,我写了一篇关于它的文章:
https://medium.com/@diegoveloper/flutter-collapsing-toolbar-sliver-app-bar-14b858e87abe