Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save pfernandom/d862b9e5ccf804805d72af06905feed2 to your computer and use it in GitHub Desktop.
Save pfernandom/d862b9e5ccf804805d72af06905feed2 to your computer and use it in GitHub Desktop.
I created this gist using Octokit!
import 'package:flutter/material.dart';
class Update {
Update({required this.description});
String description;
}
class MyStatefulWidget extends StatefulWidget {
@override
State<StatefulWidget> createState() => MyStatefulWidgetState();
}
class MyStatefulWidgetState extends State<MyStatefulWidget> {
List<Update> updates = [];
@override
Widget build(BuildContext context) {
print("Updates: $updates");
return Column(
children: [
ElevatedButton(
onPressed: () {
fetchUpdates().then((newUpdates) {
setState(() {
updates = newUpdates;
});
});
},
child: const Text("Fetch notifications"),
),
Flexible(
child: ListView.builder(
itemCount: updates.length,
itemBuilder: (BuildContext context, int index) {
return ListTile(
title: Text(updates[index].description),
);
},
),
)
],
);
}
}
import 'package:flutter/material.dart';
class NotificationIcon extends StatelessWidget {
@override
Widget build(BuildContext context) {
var notificationsCount = 0;
return Stack(
children: <Widget>[
const Icon(Icons.notifications),
Positioned(
right: 0,
child: Container(
padding: EdgeInsets.all(1),
decoration: BoxDecoration(
color: Colors.red,
borderRadius: BorderRadius.circular(6),
),
constraints: const BoxConstraints(
minWidth: 12,
minHeight: 12,
),
child: Text(
'$notificationsCount',
style: const TextStyle(
color: Colors.white,
fontSize: 8,
),
textAlign: TextAlign.center,
),
),
)
],
);
}
}
import 'package:rxdart/rxdart.dart';
import 'MyStatefulWidget.dart';
class NotificationsService {
final BehaviorSubject<List<Update>> notificationSubject = BehaviorSubject.seeded([]);
ValueStream get stream$ => notificationSubject.stream;
updateNotifications(List<Update> updates) {
notificationSubject.add(updates);
}
}
void main() {
GetIt getIt = GetIt.I;
getIt.registerSingleton<NotificationsService>(NotificationsService());
runApp(const MyApp());
}
GetIt getIt = GetIt.I;
var service = getIt.get<NotificationsService>();
fetchUpdates().then((newUpdates) {
GetIt getIt = GetIt.I;
var service = getIt.get<NotificationsService>();
service.updateNotifications(newUpdates);
...
import 'package:flutter/material.dart';
import 'package:get_it/get_it.dart';
import 'MyStatefulWidget.dart';
import 'NotificationsService.dart';
class NotificationIcon extends StatelessWidget {
late NotificationsService service;
NotificationIcon() {
GetIt getIt = GetIt.I;
service = getIt.get<NotificationsService>();
}
@override
Widget build(BuildContext context) {
return StreamBuilder(
stream: service.stream$,
builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) {
if (!snapshot.hasData || snapshot.data == null) {
return const CircularProgressIndicator();
}
List<Update> updates = snapshot.data;
if (updates.isEmpty) {
return Container();
}
var notificationsCount = updates.length;
return Stack(
children: <Widget>[
const Icon(Icons.notifications),
Positioned(
right: 0,
child: Container(
padding: EdgeInsets.all(1),
decoration: BoxDecoration(
color: Colors.red,
borderRadius: BorderRadius.circular(6),
),
constraints: const BoxConstraints(
minWidth: 12,
minHeight: 12,
),
child: Text(
'$notificationsCount',
style: const TextStyle(
color: Colors.white,
fontSize: 8,
),
textAlign: TextAlign.center,
),
),
)
],
);
},
);
}
}
return StreamBuilder(
stream: service.stream$,
builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) {
if (!snapshot.hasData || snapshot.data == null) {
return const CircularProgressIndicator();
}
List<Update> updates = snapshot.data;
if (updates.isEmpty) {
return Container();
}
var notificationsCount = updates.length;
// ... render the notification icon
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment