Notifications Feed
Loading preview
Starting the Flutter app
Follow/like/comment notifications with unread dots
import 'package:flutter/material.dart';
const _notifications = [
{'user': 'Alice', 'type': 'follow', 'unread': true},
{'user': 'Bob', 'type': 'like', 'unread': true},
{'user': 'Charlie', 'type': 'comment', 'unread': false},
];
String _message(Map<String, Object> n) {
final user = n['user'];
return switch (n['type']) {
'follow' => '$user followed you',
'like' => '$user liked your post',
'comment' => '$user commented on your post',
_ => '$user sent you a notification',
};
}
class NotificationsFeed extends StatelessWidget {
const NotificationsFeed({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Notifications')),
body: ListView.builder(
itemCount: _notifications.length,
itemBuilder: (context, i) {
final n = _notifications[i];
return ListTile(
leading: Stack(
children: [
CircleAvatar(child: Text((n['user'] as String)[0])),
if (n['unread'] == true)
Positioned(
right: 0,
top: 0,
child: Container(
width: 8,
height: 8,
decoration: const BoxDecoration(
color: Colors.blue,
shape: BoxShape.circle,
),
),
),
],
),
title: Text(_message(n)),
subtitle: Text('2h ago'),
);
},
),
);
}
}
Add it to your project
Copies the Dart source into your app, so you own it. Flutter SDK only, no extra packages.
First time? Run dart pub global activate fluttertemplates_dev, or read the CLI guide.
How it works
_notifications holds a user, a type and an unread flag; Stack adds a blue dot on unread avatars. _message switches on type and returns a full sentence, such as Bob liked your post, with a generic fallback for unknown types. '2h ago' is hardcoded on every row. Rows aren’t tappable, so the unread dots never clear.
Adapting it
- Move the sentences in
_messageto your localization files. - Store timestamps and format relative time to replace
'2h ago'. - Add
onTapto theListTileto mark read and open the source post.