|
| 1 | +// ignore_for_file: prefer_const_constructors, unused_local_variable, file_names, depend_on_referenced_packages |
| 2 | + |
| 3 | +import 'package:flutter/widgets.dart'; |
| 4 | +import 'package:flutter_state_notifier/flutter_state_notifier.dart'; |
| 5 | +import 'package:stream_feeds/stream_feeds.dart'; |
| 6 | + |
| 7 | +late StreamFeedsClient client; |
| 8 | +late Feed feed; |
| 9 | + |
| 10 | +Future<void> gettingStarted() async { |
| 11 | + // Import the package |
| 12 | + // import 'package:stream_feeds/stream_feeds.dart'; |
| 13 | + |
| 14 | + // Initialize the client |
| 15 | + final client = StreamFeedsClient( |
| 16 | + apiKey: '<your_api_key>', |
| 17 | + user: User(id: 'john'), |
| 18 | + tokenProvider: TokenProvider.static(UserToken('<user_token>')), |
| 19 | + ); |
| 20 | + await client.connect(); |
| 21 | + |
| 22 | + // Create a feed (or get its data if exists) |
| 23 | + final feed = client.feed(group: 'user', id: 'john'); |
| 24 | + final result = await feed.getOrCreate(); |
| 25 | + |
| 26 | + // Add activity |
| 27 | + final activity = await feed.addActivity( |
| 28 | + request: FeedAddActivityRequest(type: 'post', text: 'Hello, Stream Feeds!'), |
| 29 | + ); |
| 30 | +} |
| 31 | + |
| 32 | +Future<void> socialMediaFeed() async { |
| 33 | + // Create a timeline feed |
| 34 | + final timeline = client.feed(group: 'timeline', id: 'john'); |
| 35 | + await timeline.getOrCreate(); |
| 36 | + |
| 37 | + // Add a reaction to activity |
| 38 | + await timeline.addReaction( |
| 39 | + activityId: 'activity_123', |
| 40 | + request: AddReactionRequest(type: 'like'), |
| 41 | + ); |
| 42 | + |
| 43 | + // Add a comment to activity |
| 44 | + await timeline.addComment( |
| 45 | + request: AddCommentRequest( |
| 46 | + comment: 'Great post!', |
| 47 | + objectId: 'activity_123', |
| 48 | + objectType: 'activity', |
| 49 | + ), |
| 50 | + ); |
| 51 | + |
| 52 | + // Add a reaction to comment |
| 53 | + final activity = client.activity( |
| 54 | + activityId: 'activity_123', |
| 55 | + fid: FeedId(group: 'timeline', id: 'john'), |
| 56 | + ); |
| 57 | + await activity.addCommentReaction( |
| 58 | + commentId: 'commentId', |
| 59 | + request: AddCommentReactionRequest(type: 'like'), |
| 60 | + ); |
| 61 | +} |
| 62 | + |
| 63 | +Future<void> notificationFeed() async { |
| 64 | + // Create a notification feed |
| 65 | + final notifications = client.feed(group: 'notification', id: 'john'); |
| 66 | + await notifications.getOrCreate(); |
| 67 | + |
| 68 | + // Mark notifications as read |
| 69 | + await notifications.markActivity( |
| 70 | + request: MarkActivityRequest(markAllRead: true), |
| 71 | + ); |
| 72 | +} |
| 73 | + |
| 74 | +Future<void> polls() async { |
| 75 | + // Create a poll |
| 76 | + final feedId = FeedId(group: 'timeline', id: 'john'); |
| 77 | + final feed = client.feedFromId(feedId); |
| 78 | + final result = await feed.createPoll( |
| 79 | + request: CreatePollRequest( |
| 80 | + name: "What's your favorite color?", |
| 81 | + options: const [ |
| 82 | + PollOptionInput(text: 'Red'), |
| 83 | + PollOptionInput(text: 'Blue'), |
| 84 | + PollOptionInput(text: 'Green'), |
| 85 | + ], |
| 86 | + ), |
| 87 | + activityType: 'poll', |
| 88 | + ); |
| 89 | + |
| 90 | + // Vote on a poll |
| 91 | + final activityData = result.getOrThrow(); |
| 92 | + final activity = client.activity(activityId: activityData.id, fid: feedId); |
| 93 | + await activity.castPollVote( |
| 94 | + CastPollVoteRequest(vote: VoteData(optionId: 'option_456')), |
| 95 | + ); |
| 96 | +} |
| 97 | + |
| 98 | +Future<void> customActivityTypes() async { |
| 99 | + final workoutActivity = await feed.addActivity( |
| 100 | + request: FeedAddActivityRequest( |
| 101 | + text: 'Just finished my run', |
| 102 | + custom: {'distance': 5.2, 'duration': 1800, 'calories': 450}, |
| 103 | + type: 'workout', |
| 104 | + ), |
| 105 | + ); |
| 106 | +} |
| 107 | + |
| 108 | +// Real-time updates with StateLayer |
| 109 | + |
| 110 | +// imports |
| 111 | +// import 'package:flutter/widgets.dart'; |
| 112 | +// import 'package:flutter_state_notifier/flutter_state_notifier.dart'; |
| 113 | +// import 'package:stream_feeds/stream_feeds.dart'; |
| 114 | +class FeedView extends StatefulWidget { |
| 115 | + const FeedView({super.key, required this.feed}); |
| 116 | + final Feed feed; |
| 117 | + |
| 118 | + @override |
| 119 | + State<FeedView> createState() => _FeedViewState(); |
| 120 | +} |
| 121 | + |
| 122 | +class _FeedViewState extends State<FeedView> { |
| 123 | + @override |
| 124 | + void initState() { |
| 125 | + super.initState(); |
| 126 | + widget.feed.getOrCreate(); |
| 127 | + } |
| 128 | + |
| 129 | + @override |
| 130 | + void dispose() { |
| 131 | + widget.feed.dispose(); |
| 132 | + super.dispose(); |
| 133 | + } |
| 134 | + |
| 135 | + @override |
| 136 | + Widget build(BuildContext context) { |
| 137 | + return StateNotifierBuilder( |
| 138 | + stateNotifier: widget.feed.notifier, |
| 139 | + builder: (BuildContext context, FeedState state, Widget? child) { |
| 140 | + return Text(state.feed?.name ?? ''); |
| 141 | + }, |
| 142 | + ); |
| 143 | + } |
| 144 | +} |
0 commit comments