|
| 1 | +import 'package:collection/collection.dart'; |
| 2 | +import 'package:flutter/material.dart'; |
| 3 | +import 'package:flutter_state_notifier/flutter_state_notifier.dart'; |
| 4 | +import 'package:stream_feeds/stream_feeds.dart'; |
| 5 | + |
| 6 | +/// Get your own credentials from Stream. |
| 7 | +/// For more detailed documentation and examples, visit: |
| 8 | +/// https://getstream.io/activity-feeds/docs/flutter/ |
| 9 | +const apiKey = ''; |
| 10 | +const userId = ''; |
| 11 | +const userToken = ''; |
| 12 | + |
| 13 | +void main() { |
| 14 | + runApp(const MyApp()); |
| 15 | +} |
| 16 | + |
| 17 | +class MyApp extends StatelessWidget { |
| 18 | + const MyApp({super.key}); |
| 19 | + |
| 20 | + @override |
| 21 | + Widget build(BuildContext context) { |
| 22 | + return MaterialApp(title: 'Stream Feeds Example', home: const MyHomePage()); |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +class MyHomePage extends StatefulWidget { |
| 27 | + const MyHomePage({super.key}); |
| 28 | + |
| 29 | + @override |
| 30 | + State<MyHomePage> createState() => _MyHomePageState(); |
| 31 | +} |
| 32 | + |
| 33 | +class _MyHomePageState extends State<MyHomePage> { |
| 34 | + late StreamFeedsClient client; |
| 35 | + late Future<void> connectionFuture; |
| 36 | + |
| 37 | + @override |
| 38 | + void initState() { |
| 39 | + super.initState(); |
| 40 | + client = StreamFeedsClient( |
| 41 | + apiKey: apiKey, |
| 42 | + user: User(id: userId), |
| 43 | + tokenProvider: TokenProvider.static(UserToken(userToken)), |
| 44 | + ); |
| 45 | + connectionFuture = client.connect(); |
| 46 | + } |
| 47 | + |
| 48 | + @override |
| 49 | + void dispose() { |
| 50 | + client.disconnect(); |
| 51 | + super.dispose(); |
| 52 | + } |
| 53 | + |
| 54 | + @override |
| 55 | + Widget build(BuildContext context) { |
| 56 | + return Scaffold( |
| 57 | + appBar: AppBar(title: const Text('Stream Feeds Example')), |
| 58 | + body: FutureBuilder( |
| 59 | + future: connectionFuture, |
| 60 | + builder: (context, snapshot) { |
| 61 | + switch (snapshot.connectionState) { |
| 62 | + case ConnectionState.done: |
| 63 | + return MyTimeLine(client: client); |
| 64 | + default: |
| 65 | + return const Center(child: CircularProgressIndicator()); |
| 66 | + } |
| 67 | + }, |
| 68 | + ), |
| 69 | + ); |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +class MyTimeLine extends StatefulWidget { |
| 74 | + const MyTimeLine({super.key, required this.client}); |
| 75 | + final StreamFeedsClient client; |
| 76 | + |
| 77 | + @override |
| 78 | + State<MyTimeLine> createState() => _MyTimeLineState(); |
| 79 | +} |
| 80 | + |
| 81 | +class _MyTimeLineState extends State<MyTimeLine> { |
| 82 | + late final Feed feed; |
| 83 | + |
| 84 | + @override |
| 85 | + void initState() { |
| 86 | + super.initState(); |
| 87 | + feed = widget.client.feedFromId(FeedId.timeline(userId)); |
| 88 | + feed.getOrCreate(); |
| 89 | + } |
| 90 | + |
| 91 | + @override |
| 92 | + void dispose() { |
| 93 | + feed.dispose(); |
| 94 | + super.dispose(); |
| 95 | + } |
| 96 | + |
| 97 | + @override |
| 98 | + Widget build(BuildContext context) { |
| 99 | + return StateNotifierBuilder( |
| 100 | + stateNotifier: feed.notifier, |
| 101 | + builder: (context, state, child) { |
| 102 | + return ListView.separated( |
| 103 | + itemCount: state.activities.length + 1, |
| 104 | + separatorBuilder: (context, index) => const Divider(), |
| 105 | + itemBuilder: (context, index) { |
| 106 | + if (index == state.activities.length) { |
| 107 | + if (state.canLoadMoreActivities) { |
| 108 | + return TextButton( |
| 109 | + onPressed: () { |
| 110 | + feed.queryMoreActivities(); |
| 111 | + }, |
| 112 | + child: const Text('Load more'), |
| 113 | + ); |
| 114 | + } else { |
| 115 | + return const Text('No more activities'); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + return ActivityItem(activity: state.activities[index]); |
| 120 | + }, |
| 121 | + ); |
| 122 | + }, |
| 123 | + ); |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +class ActivityItem extends StatelessWidget { |
| 128 | + const ActivityItem({super.key, required this.activity}); |
| 129 | + |
| 130 | + final ActivityData activity; |
| 131 | + |
| 132 | + @override |
| 133 | + Widget build(BuildContext context) { |
| 134 | + // Simple activity item with the activity text and an image if it has one |
| 135 | + final userImage = activity.user.image; |
| 136 | + final imageAttachment = activity.attachments.firstWhereOrNull( |
| 137 | + (attachment) => attachment.imageUrl != null, |
| 138 | + ); |
| 139 | + |
| 140 | + return Padding( |
| 141 | + padding: const EdgeInsets.all(8.0), |
| 142 | + child: Row( |
| 143 | + crossAxisAlignment: CrossAxisAlignment.start, |
| 144 | + children: [ |
| 145 | + SizedBox( |
| 146 | + width: 50, |
| 147 | + height: 50, |
| 148 | + child: CircleAvatar( |
| 149 | + backgroundImage: |
| 150 | + userImage != null ? NetworkImage(userImage) : null, |
| 151 | + child: Container( |
| 152 | + decoration: BoxDecoration( |
| 153 | + color: Colors.white.withValues(alpha: 0.3), |
| 154 | + shape: BoxShape.circle, |
| 155 | + ), |
| 156 | + child: Center( |
| 157 | + child: Text(activity.user.name?.characters.first ?? ''), |
| 158 | + ), |
| 159 | + ), |
| 160 | + ), |
| 161 | + ), |
| 162 | + SizedBox(width: 16), |
| 163 | + Expanded( |
| 164 | + child: Column( |
| 165 | + crossAxisAlignment: CrossAxisAlignment.start, |
| 166 | + mainAxisSize: MainAxisSize.min, |
| 167 | + children: [ |
| 168 | + if (imageAttachment != null) |
| 169 | + SizedBox( |
| 170 | + height: 200, |
| 171 | + child: Image.network(imageAttachment.imageUrl!), |
| 172 | + ), |
| 173 | + Text(activity.text ?? ''), |
| 174 | + ], |
| 175 | + ), |
| 176 | + ), |
| 177 | + ], |
| 178 | + ), |
| 179 | + ); |
| 180 | + } |
| 181 | +} |
0 commit comments