Created
July 19, 2022 04:16
-
-
Save pfernandom/d862b9e5ccf804805d72af06905feed2 to your computer and use it in GitHub Desktop.
I created this gist using Octokit!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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), | |
| ); | |
| }, | |
| ), | |
| ) | |
| ], | |
| ); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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, | |
| ), | |
| ), | |
| ) | |
| ], | |
| ); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| flutter pub add rxdart |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| flutter pub add get_it |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| void main() { | |
| GetIt getIt = GetIt.I; | |
| getIt.registerSingleton<NotificationsService>(NotificationsService()); | |
| runApp(const MyApp()); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| GetIt getIt = GetIt.I; | |
| var service = getIt.get<NotificationsService>(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fetchUpdates().then((newUpdates) { | |
| GetIt getIt = GetIt.I; | |
| var service = getIt.get<NotificationsService>(); | |
| service.updateNotifications(newUpdates); | |
| ... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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, | |
| ), | |
| ), | |
| ) | |
| ], | |
| ); | |
| }, | |
| ); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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