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 13ba6ada07
commit 3615342c64

View file

@ -4,15 +4,14 @@
import 'dart:async'; import 'dart:async';
import 'package:collection/collection.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_timeline_interface/flutter_timeline_interface.dart'; 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,
@ -24,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;
@ -34,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;
@ -73,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;
@ -119,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'
@ -149,7 +148,7 @@ class _TimelineScreenState extends State<TimelineScreen> {
); );
} }
var categories = service.postService.categories; var categories = widget.service.postService.categories;
return Column( return Column(
crossAxisAlignment: CrossAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start,
@ -255,7 +254,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,
@ -273,11 +272,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();
}, },
@ -286,12 +285,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,
), ),
), ),
@ -327,8 +328,8 @@ 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.fetchCategories(); await widget.service.postService.fetchCategories();
await service.postService.fetchPosts(category); await widget.service.postService.fetchPosts(category);
setState(() { setState(() {
isLoading = false; isLoading = false;
}); });