一个简单的解决方案是这个包extended_nested_scroll_view。
这是我实现您想要的滚动行为的方式(滚动到底部时隐藏多个选项卡,滚动到顶部时显示AppBar):
(它没有经过打磨,但你可以使用它)
import 'package:flutter/material.dart';
class Test extends StatelessWidget {
@override
Widget build(BuildContext context) {
return NestedScrollView(
headerSliverBuilder: (context, isScrolled) {
return [SliverAppBar(
leading: IconButton(
icon: Icon(Icons.menu),
onPressed: () {},
),
title: Text('Pets'),
centerTitle: false,
actions: [
IconButton(
icon: Icon(Icons.share),
onPressed: () {},
),
IconButton(
icon: Icon(Icons.search),
onPressed: () {},
),
IconButton(
icon: Icon(Icons.pets),
onPressed: () {},
),
],
)];
},
body: DefaultTabController(
length: 3,
child: Scaffold(
appBar: TabBar(
labelColor: Colors.black,
tabs: [
Tab(text: 'Dogs',),
Tab(text: 'Cats'),
Tab(text: 'Birds'),
],
),
body: TabBarView(
children: [
Center(child: Icon(Icons.pets, color: Colors.blue)),
Center(child: Icon(Icons.pets, color: Colors.green)),
Center(child: Icon(Icons.pets, color: Colors.red)),
],
),
),
)
);
}
}
结果:
我在NestedScrollView 中使用了一个脚手架和一个SliverAppBar,有了这些,你几乎可以得到两个单独的AppBar,而SliverAppBar 在向上滚动时会隐藏起来。
为了保留每个选项卡的滚动位置,我将使用 3 个独立的ScrollController。如果您在这些方面需要更多帮助,请写评论;)