gpt4 book ai didi

flutter - 在customScrollView中滚动到底部时如何检测和运行加载功能

转载 作者:行者123 更新时间:2023-12-03 04:35:56 27 4
gpt4 key购买 nike

当用户滚动到customScrollView中列表的底部时,我尝试运行更多负载。
我正在使用粘性 header 进行某种排序,因此必须使用带有flutter_sticky_header包的条子。
这是页面代码

class SoldEntryScreen extends StatelessWidget {
const SoldEntryScreen({
Key key,
this.isDataLoading,
this.isNextPageAvailable,
this.transactions,
this.loadNextPage,
this.noError,
});

final bool isDataLoading;
final bool isNextPageAvailable;
final transactions;
final Function loadNextPage;
final bool noError;

@override
Widget build(BuildContext context) {
var history = {};

transactions.forEach((element) {
final formatedDate = Utils.dateFormater(element.createdAt);
if ((history[formatedDate['dateClass']] == null))
return (history[formatedDate['dateClass']] = [element]);
return history[formatedDate['dateClass']].add(element);
});
print(history);
return AppScaffold(
title: 'List Example',
slivers: [
SliverToBoxAdapter(
child: SoldEntryPage(),
),
_StickyHeaderDateSort(history: history),
],
);
}
}

class _StickyHeaderList extends StatelessWidget {
const _StickyHeaderList({
Key key,
this.keyTitle,
this.keyData,
}) : super(key: key);

final keyTitle;
final keyData;

@override
Widget build(BuildContext context) {
print(keyData);
return SliverStickyHeader(
header: Header(title: keyTitle),
sliver: SliverToBoxAdapter(
child: Container(
child: ListView.separated(
shrinkWrap: true,
physics: ClampingScrollPhysics(),
itemCount: keyData.length,
itemBuilder: (context, index) {
return MoneyTransactionModelListItem(
itemIndex: index, transaction: keyData[index]);
},
separatorBuilder: (BuildContext context, int index) =>
Divider(color: Theme.of(context).dividerColor),
),
)
),
);
}
}

class _StickyHeaderDateSort extends StatelessWidget {
const _StickyHeaderDateSort({
Key key,
this.history,
}) : super(key: key);

final history;

@override
Widget build(BuildContext context) {
return SliverStickyHeader(
header: HeaderDatePicker(),
sliver: SliverToBoxAdapter(
child: ShrinkWrappingViewport(
offset: ViewportOffset.zero(),
slivers: [
for (var key in history.keys)
_StickyHeaderList(keyTitle: key, keyData: history[key])
],
),
),
);
}
}
看起来像这样
enter image description here
这是 AppScaffold小部件
class AppScaffold extends StatelessWidget {
const AppScaffold({
Key key,
@required this.title,
@required this.slivers,
this.reverse = false,
}) : super(key: key);

final String title;
final List<Widget> slivers;
final bool reverse;

@override
Widget build(BuildContext context) {
return DefaultStickyHeaderController(
child: CustomScrollView(
slivers: slivers,
reverse: reverse,
),
);
}
}

最佳答案

这是一个ListView的基本示例,当其底部被击中时,它会加载更多元素。基本上,每次滚动都会通知NotificationListener<ScrollNotification>并为您提供scrollNotification的想法,您可以使用它来查看自己是否在ListView的底部。
这是代码:

import 'package:flutter/material.dart';

main() {
runApp(MyApp());
}

class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
List<Container> containerList = [
Container(
color: Colors.red,
height: 200,
),
Container(
color: Colors.blue,
height: 200,
),
Container(
color: Colors.green,
height: 200,
),
Container(
color: Colors.yellow,
height: 200,
),
];
bool isUpdating = false;

Future<void> addNewWidgets() async {
isUpdating = true;

Future.delayed(Duration(seconds: 1), () {
setState(() {
containerList.addAll([
Container(
color: Colors.red,
height: 200,
),
Container(
color: Colors.blue,
height: 200,
),
Container(
color: Colors.green,
height: 200,
),
Container(
color: Colors.yellow,
height: 200,
),
]);
});

print('state changed ${containerList.length}');
isUpdating = false;
});
}

@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: NotificationListener<ScrollNotification>(
onNotification: (scrollNotification) {
if (scrollNotification.metrics.pixels >=
scrollNotification.metrics.maxScrollExtent &&
!isUpdating) {
addNewWidgets();
}
return true;
},
child: ListView.builder(
itemCount: containerList.length,
itemBuilder: (BuildContext context, int index) {
return containerList[index];
},
),
),
),
);
}
}
我没有使用sticky_header,但是我敢肯定您将能够使用此方法使其工作。

关于flutter - 在customScrollView中滚动到底部时如何检测和运行加载功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64119812/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com