fix: don't use TimelineService as state

Otherwise the widget won't use a new service if it has been provided by
the parent. It really isn't state anyway.
This commit is contained in:
Bart Ribbers 2025-04-22 14:31:10 +02:00
parent 93e1b35a7a
commit 48f77e30e0

View file

@ -9,9 +9,9 @@ import 'package:flutter_timeline_interface/flutter_timeline_interface.dart';
import 'package:flutter_timeline_view/flutter_timeline_view.dart'; import 'package:flutter_timeline_view/flutter_timeline_view.dart';
class TimelineScreen extends StatefulWidget { class TimelineScreen extends StatefulWidget {
const TimelineScreen({ TimelineScreen({
this.userId = 'test_user', this.userId = 'test_user',
this.service, TimelineService? service,
this.options = const TimelineOptions(), this.options = const TimelineOptions(),
this.onPostTap, this.onPostTap,
this.scrollController, this.scrollController,
@ -23,7 +23,10 @@ class TimelineScreen extends StatefulWidget {
this.filterEnabled = false, this.filterEnabled = false,
this.allowAllDeletion = false, this.allowAllDeletion = false,
super.key, super.key,
}); }) : service = service ??
TimelineService(
postService: LocalTimelinePostService(),
);
/// The user id of the current user /// The user id of the current user
final String userId; final String userId;
@ -33,7 +36,7 @@ class TimelineScreen extends StatefulWidget {
final bool allowAllDeletion; final bool allowAllDeletion;
/// The service to use for fetching and manipulating posts /// The service to use for fetching and manipulating posts
final TimelineService? service; final TimelineService service;
/// All the configuration options for the timelinescreens and widgets /// All the configuration options for the timelinescreens and widgets
final TimelineOptions options; final TimelineOptions options;
@ -72,10 +75,6 @@ class _TimelineScreenState extends State<TimelineScreen> {
late var textFieldController = TextEditingController( late var textFieldController = TextEditingController(
text: widget.options.filterOptions.initialFilterWord, text: widget.options.filterOptions.initialFilterWord,
); );
late var service = widget.service ??
TimelineService(
postService: LocalTimelinePostService(),
);
bool isLoading = true; bool isLoading = true;
@ -118,14 +117,15 @@ class _TimelineScreenState extends State<TimelineScreen> {
// Build the list of posts // Build the list of posts
return ListenableBuilder( return ListenableBuilder(
listenable: service.postService, listenable: widget.service.postService,
builder: (context, _) { builder: (context, _) {
if (!context.mounted) return const SizedBox(); if (!context.mounted) return const SizedBox();
var posts = widget.posts ?? service.postService.getPosts(category); var posts =
widget.posts ?? widget.service.postService.getPosts(category);
if (widget.filterEnabled && filterWord != null) { if (widget.filterEnabled && filterWord != null) {
if (service.postService is TimelineFilterService) { if (widget.service.postService is TimelineFilterService) {
posts = (service.postService as TimelineFilterService) posts = (widget.service.postService as TimelineFilterService)
.filterPosts(filterWord!, {}); .filterPosts(filterWord!, {});
} else { } else {
debugPrint('Timeline service needs to mixin' debugPrint('Timeline service needs to mixin'
@ -247,7 +247,7 @@ class _TimelineScreenState extends State<TimelineScreen> {
padding: widget.options.paddings.postPadding, padding: widget.options.paddings.postPadding,
child: widget.postWidgetBuilder?.call(post) ?? child: widget.postWidgetBuilder?.call(post) ??
TimelinePostWidget( TimelinePostWidget(
service: service, service: widget.service,
userId: widget.userId, userId: widget.userId,
options: widget.options, options: widget.options,
allowAllDeletion: widget.allowAllDeletion, allowAllDeletion: widget.allowAllDeletion,
@ -265,11 +265,11 @@ class _TimelineScreenState extends State<TimelineScreen> {
builder: (context) => Scaffold( builder: (context) => Scaffold(
body: TimelinePostScreen( body: TimelinePostScreen(
userId: 'test_user', userId: 'test_user',
service: service, service: widget.service,
options: widget.options, options: widget.options,
post: post, post: post,
onPostDelete: () { onPostDelete: () {
service.postService widget.service.postService
.deletePost(post); .deletePost(post);
Navigator.of(context).pop(); Navigator.of(context).pop();
}, },
@ -278,12 +278,14 @@ class _TimelineScreenState extends State<TimelineScreen> {
), ),
); );
}, },
onTapLike: () async => service.postService onTapLike: () async => widget
.service.postService
.likePost(widget.userId, post), .likePost(widget.userId, post),
onTapUnlike: () async => service.postService onTapUnlike: () async => widget
.service.postService
.unlikePost(widget.userId, post), .unlikePost(widget.userId, post),
onPostDelete: () async => onPostDelete: () async =>
service.postService.deletePost(post), widget.service.postService.deletePost(post),
onUserTap: widget.onUserTap, onUserTap: widget.onUserTap,
), ),
), ),
@ -319,7 +321,7 @@ class _TimelineScreenState extends State<TimelineScreen> {
Future<void> loadPosts() async { Future<void> loadPosts() async {
if (widget.posts != null || !context.mounted) return; if (widget.posts != null || !context.mounted) return;
try { try {
await service.postService.fetchPosts(category); await widget.service.postService.fetchPosts(category);
setState(() { setState(() {
isLoading = false; isLoading = false;
}); });