我试图在每个选项卡中使用NestedScrollView
++ with ,但是水平滑动时引发异常SliverAppBar
:TabView
Scrollbar
在通知 AnimationController 的状态侦听器时引发了以下断言: PrimaryScrollController 当前附加到多个 ScrollPosition。
我尝试用 包裹Scrollbar
,PrimaryScrollController
异常消失但外部控制器和内部控制器不再同步;我尝试添加ScrollController
到NestedScrollView
然后使用NotificationListener
,但在快速滑倒时造成奇怪的滞后:
NotificationListener<ScrollNotification>(
onNotification: (ScrollNotification notification) {
//print(notification.metrics.axisDirection);
if (notification.metrics.extentAfter > 0 &&
notification.metrics.extentBefore > 0 &&
notification.metrics.axisDirection == AxisDirection.down) {
double pixelnow = notification.metrics.pixels;
double _jmpTo =
_scrollController.offset + (pixelnow - _scrollLocation);
//print("pixel now : $pixelnow");
//print("jumpto: $_jmpTo");
if (_scrollController.position.maxScrollExtent >= _scrollController.offset
&& _scrollController.offset >= _scrollController.position.minScrollExtent ){
_scrollController.jumpTo(_jmpTo);
//_scrollController.jumpTo(value)
}
_scrollLocation = pixelnow;
}
return false;
},
我的原始代码在这里:
import 'package:flutter/material.dart';
void main() => runApp(MaterialApp(home: Scaffold(body: CommunityPage())));
class CommunityPage extends StatefulWidget {
CommunityPage({Key? key}) : super(key: key);
@override
_CommunityPageState createState() => _CommunityPageState();
}
class _CommunityPageState extends State<CommunityPage> {
//ScrollController _scrollController = ScrollController();
//final TabController _tabController = TabController();
final _tabs = <String>["Tab 1", "Tab 2", "Tab 3"];
@override
Widget build(BuildContext context) {
return Scaffold(
body: DefaultTabController(
length: _tabs.length, // This is the number of tabs.
child: NestedScrollView(
headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
// These are the slivers that show up in the "outer" scroll view.
return <Widget>[
SliverOverlapAbsorber(
handle:
NestedScrollView.sliverOverlapAbsorberHandleFor(context),
sliver: SliverAppBar(
automaticallyImplyLeading: false,
title: const Text("Community",style: TextStyle(color: Colors.black)),
//centerTitle: false,
pinned: true,
floating: true,
snap: true,
backgroundColor: Colors.grey[50],
expandedHeight: 85.0,
bottom: TabBar(
tabs: _tabs.map((String name) => Tab(text: name)).toList(),
),
),
),
];
},
body: TabBarView(
children: _tabs.map((String name) {
return Builder(
builder: (BuildContext context) {
return Scrollbar(
child: CustomScrollView(
key: PageStorageKey<String>(name),
slivers: <Widget>[
SliverOverlapInjector(
handle:
NestedScrollView.sliverOverlapAbsorberHandleFor(
context),
),
SliverFixedExtentList(
itemExtent: 50.0,
delegate: SliverChildBuilderDelegate(
(BuildContext context, int index) {
return ListTile(
title: Text('Item $index'),
);
},
childCount: 30,
),
),
],
),
);
},
);
}).toList(),
),
),
),
);
}
}
当我放弃时一切都好Scrollbar
,甚至RefreshIndicator
工作正常,这真的让我很困惑......