Skip to content

Commit 515ed5c

Browse files
authored
feat(samples): Add a very basic example for pub.dev (#53)
1 parent d08e4e9 commit 515ed5c

File tree

6 files changed

+323
-0
lines changed

6 files changed

+323
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# For the example app on pub.dev we ignore all native platform folders, just run `flutter create .` to create them.
2+
# Make sure you have internet permissions.
3+
android/
4+
ios/
5+
macos/
6+
windows/
7+
linux/
8+
web/
9+
10+
# Miscellaneous
11+
*.class
12+
*.log
13+
*.pyc
14+
*.swp
15+
.DS_Store
16+
.atom/
17+
.build/
18+
.buildlog/
19+
.history
20+
.svn/
21+
.swiftpm/
22+
migrate_working_dir/
23+
24+
# IntelliJ related
25+
*.iml
26+
*.ipr
27+
*.iws
28+
.idea/
29+
30+
# The .vscode folder contains launch configuration and tasks you configure in
31+
# VS Code which you may wish to be included in version control, so this line
32+
# is commented out by default.
33+
#.vscode/
34+
35+
# Flutter/Dart/Pub related
36+
**/doc/api/
37+
**/ios/Flutter/.last_build_id
38+
.dart_tool/
39+
.flutter-plugins-dependencies
40+
.pub-cache/
41+
.pub/
42+
/build/
43+
/coverage/
44+
45+
# Symbolication related
46+
app.*.symbols
47+
48+
# Obfuscation related
49+
app.*.map.json
50+
51+
# Android Studio will place build artifacts here
52+
/android/app/debug
53+
/android/app/profile
54+
/android/app/release
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# This file tracks properties of this Flutter project.
2+
# Used by Flutter tool to assess capabilities and perform upgrades etc.
3+
#
4+
# This file should be version controlled and should not be manually edited.
5+
6+
version:
7+
revision: "05db9689081f091050f01aed79f04dce0c750154"
8+
channel: "stable"
9+
10+
project_type: app
11+
12+
# Tracks metadata for the flutter migrate command
13+
migration:
14+
platforms:
15+
- platform: root
16+
create_revision: 05db9689081f091050f01aed79f04dce0c750154
17+
base_revision: 05db9689081f091050f01aed79f04dce0c750154
18+
- platform: android
19+
create_revision: 05db9689081f091050f01aed79f04dce0c750154
20+
base_revision: 05db9689081f091050f01aed79f04dce0c750154
21+
- platform: ios
22+
create_revision: 05db9689081f091050f01aed79f04dce0c750154
23+
base_revision: 05db9689081f091050f01aed79f04dce0c750154
24+
- platform: linux
25+
create_revision: 05db9689081f091050f01aed79f04dce0c750154
26+
base_revision: 05db9689081f091050f01aed79f04dce0c750154
27+
- platform: macos
28+
create_revision: 05db9689081f091050f01aed79f04dce0c750154
29+
base_revision: 05db9689081f091050f01aed79f04dce0c750154
30+
- platform: web
31+
create_revision: 05db9689081f091050f01aed79f04dce0c750154
32+
base_revision: 05db9689081f091050f01aed79f04dce0c750154
33+
- platform: windows
34+
create_revision: 05db9689081f091050f01aed79f04dce0c750154
35+
base_revision: 05db9689081f091050f01aed79f04dce0c750154
36+
37+
# User provided section
38+
39+
# List of Local paths (relative to this file) that should be
40+
# ignored by the migrate tool.
41+
#
42+
# Files that are not part of the templates will be ignored by default.
43+
unmanaged_files:
44+
- 'lib/main.dart'
45+
- 'ios/Runner.xcodeproj/project.pbxproj'
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Stream Feeds Example
2+
3+
This is a very basic example of how to use the Stream Feeds SDK.
4+
For a more complete example, you can check the [Stream Feeds sample app](https://github.com/GetStream/stream-feeds-flutter/tree/main/sample_app).
5+
6+
## Getting Started
7+
8+
To run the example, you need to get your own credentials from Stream and update the `lib/main.dart` file.
9+
For more detailed documentation and examples, visit:
10+
[https://getstream.io/activity-feeds/docs/flutter/](https://getstream.io/activity-feeds/docs/flutter/)
11+
12+
Then, you can run the example using the following command:
13+
14+
```bash
15+
flutter create .
16+
flutter run -d chrome
17+
```
18+
19+
For some platforms, such as macOS, you may need to add internet permissions to the app.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
include: package:flutter_lints/flutter.yaml
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
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+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: example
2+
description: "Example for the Stream Feeds SDK"
3+
publish_to: 'none' # Remove this line if you wish to publish to pub.dev
4+
version: 1.0.0+1
5+
6+
environment:
7+
sdk: ^3.6.2
8+
9+
dependencies:
10+
collection: ^1.18.0
11+
flutter:
12+
sdk: flutter
13+
flutter_state_notifier: ^1.0.0
14+
stream_feeds:
15+
path: ../
16+
17+
dev_dependencies:
18+
flutter_test:
19+
sdk: flutter
20+
flutter_lints: ^5.0.0
21+
22+
flutter:
23+
uses-material-design: true

0 commit comments

Comments
 (0)