loris/lib/pages/timeline/timeline.dart

88 lines
2.0 KiB
Dart
Raw Normal View History

2022-06-16 20:30:02 +00:00
import 'package:flutter/material.dart';
2022-07-04 17:37:53 +00:00
import 'package:localization/localization.dart';
import 'package:loris/partials/thread.dart';
2022-07-04 20:39:25 +00:00
import '../../business_logic/timeline/timeline.dart' as tl;
2022-06-16 20:30:02 +00:00
2022-07-04 17:37:53 +00:00
class Timeline extends StatefulWidget {
const Timeline({Key? key}) : super(key: key);
@override
State<Timeline> createState() => _TimelineState();
}
class _TimelineState extends State<Timeline> {
2022-08-01 10:01:19 +00:00
String? id;
2022-07-04 17:37:53 +00:00
final controller = ScrollController();
List<Widget> children = [];
bool loading = false;
@override
void initState() {
super.initState();
fetchMore();
controller.addListener(() {
if (controller.position.maxScrollExtent <= controller.offset &&
!loading) {
fetchMore();
}
});
}
Future fetchMore() async {
loading = true;
2022-08-01 08:29:43 +00:00
2022-08-01 10:01:19 +00:00
final model = await tl.getTimelineFromServer(id);
id = model.posts[model.posts.length - 1].id;
2022-08-01 08:29:43 +00:00
2022-07-04 17:37:53 +00:00
setState(() {
if (children.isNotEmpty) {
children.removeAt(children.length - 1);
}
2022-08-01 08:29:43 +00:00
children.addAll([
Thread(
model: model,
)
]);
2022-07-04 17:37:53 +00:00
children.add(
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
TextButton.icon(
onPressed: () {
fetchMore();
},
icon: const Icon(Icons.more_horiz),
label: Text("load-more".i18n()),
)
],
),
);
loading = false;
});
}
@override
void dispose() {
controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return ListView.separated(
physics: const AlwaysScrollableScrollPhysics(),
controller: controller,
itemBuilder: (context, index) {
return children[index];
},
separatorBuilder: (context, index) {
return const Divider(
color: Colors.transparent,
);
},
itemCount: children.length,
2022-07-03 13:47:24 +00:00
padding: const EdgeInsets.fromLTRB(24, 0, 24, 64),
addAutomaticKeepAlives: false,
2022-07-04 17:37:53 +00:00
);
}
2022-06-16 20:30:02 +00:00
}