add unread indicator in backbutton

This commit is contained in:
Salvatore Giordano
2020-11-11 15:21:00 +01:00
parent bc64691be3
commit e32902d5aa
5 changed files with 123 additions and 60 deletions
+20 -7
View File
@@ -1,21 +1,29 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:stream_chat_flutter/src/stream_icons.dart';
import 'package:stream_chat_flutter/src/unread_indicator.dart';
import 'package:stream_chat_flutter/stream_chat_flutter.dart';
class StreamBackButton extends StatelessWidget { class StreamBackButton extends StatelessWidget {
const StreamBackButton({ const StreamBackButton({
Key key, Key key,
this.onPressed, this.onPressed,
this.icon = Icons.arrow_back_ios_outlined, this.icon = Icons.arrow_back_ios_outlined,
this.showUnreads = false,
}) : super(key: key); }) : super(key: key);
final VoidCallback onPressed; final VoidCallback onPressed;
final IconData icon; final IconData icon;
final bool showUnreads;
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Padding( return Stack(
children: [
Padding(
padding: const EdgeInsets.all(14.0), padding: const EdgeInsets.all(14.0),
child: RawMaterialButton( child: RawMaterialButton(
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(4)), shape:
RoundedRectangleBorder(borderRadius: BorderRadius.circular(4)),
elevation: 0, elevation: 0,
highlightElevation: 0, highlightElevation: 0,
focusElevation: 0, focusElevation: 0,
@@ -28,17 +36,22 @@ class StreamBackButton extends StatelessWidget {
Navigator.of(context).pop(); Navigator.of(context).pop();
} }
}, },
fillColor: Theme.of(context).brightness == Brightness.dark
? Colors.white.withOpacity(.1)
: Colors.black.withOpacity(.1),
child: Icon( child: Icon(
icon ?? Icons.arrow_back_ios_outlined, icon ?? StreamIcons.left,
size: 15, size: 24,
color: Theme.of(context).brightness == Brightness.dark color: Theme.of(context).brightness == Brightness.dark
? Colors.white ? Colors.white
: Colors.black, : Colors.black,
), ),
), ),
),
if (showUnreads)
Positioned(
top: 7,
right: 7,
child: UnreadIndicator(),
),
],
); );
} }
} }
+4 -1
View File
@@ -81,7 +81,10 @@ class ChannelHeader extends StatelessWidget implements PreferredSizeWidget {
brightness: Theme.of(context).brightness, brightness: Theme.of(context).brightness,
elevation: 1, elevation: 1,
leading: showBackButton leading: showBackButton
? StreamBackButton(onPressed: onBackPressed) ? StreamBackButton(
onPressed: onBackPressed,
showUnreads: true,
)
: SizedBox(), : SizedBox(),
backgroundColor: backgroundColor:
StreamChatTheme.of(context).channelTheme.channelHeaderTheme.color, StreamChatTheme.of(context).channelTheme.channelHeaderTheme.color,
+2 -2
View File
@@ -2,10 +2,10 @@ import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart'; import 'package:flutter/widgets.dart';
import 'package:jiffy/jiffy.dart'; import 'package:jiffy/jiffy.dart';
import 'package:stream_chat/stream_chat.dart'; import 'package:stream_chat/stream_chat.dart';
import 'package:stream_chat_flutter/src/unread_indicator.dart';
import '../stream_chat_flutter.dart'; import '../stream_chat_flutter.dart';
import 'channel_name.dart'; import 'channel_name.dart';
import 'channel_unread_indicator.dart';
/// ![screenshot](https://raw.githubusercontent.com/GetStream/stream-chat-flutter/master/screenshots/channel_preview.png) /// ![screenshot](https://raw.githubusercontent.com/GetStream/stream-chat-flutter/master/screenshots/channel_preview.png)
/// ![screenshot](https://raw.githubusercontent.com/GetStream/stream-chat-flutter/master/screenshots/channel_preview_paint.png) /// ![screenshot](https://raw.githubusercontent.com/GetStream/stream-chat-flutter/master/screenshots/channel_preview_paint.png)
@@ -80,7 +80,7 @@ class ChannelPreview extends StatelessWidget {
e.user.id == channel.client.state.user.id)) { e.user.id == channel.client.state.user.id)) {
return SizedBox(); return SizedBox();
} }
return UnreadIndicator( return ChannelUnreadIndicator(
channel: channel, channel: channel,
); );
}), }),
+48
View File
@@ -0,0 +1,48 @@
import 'package:flutter/material.dart';
import 'package:stream_chat/stream_chat.dart';
import 'package:stream_chat_flutter/src/stream_chat_theme.dart';
class ChannelUnreadIndicator extends StatelessWidget {
const ChannelUnreadIndicator({
Key key,
@required this.channel,
}) : super(key: key);
final Channel channel;
@override
Widget build(BuildContext context) {
return StreamBuilder<int>(
stream: channel.state.unreadCountStream,
initialData: channel.state.unreadCount,
builder: (context, snapshot) {
if (!snapshot.hasData || snapshot.data == 0) {
return SizedBox();
}
return Material(
borderRadius: BorderRadius.circular(8),
color: StreamChatTheme.of(context)
.channelPreviewTheme
.unreadCounterColor,
child: Padding(
padding: const EdgeInsets.only(
left: 5.0,
right: 5.0,
top: 2,
bottom: 1,
),
child: Center(
child: Text(
'${snapshot.data}',
style: TextStyle(
fontSize: 11,
color: Colors.white,
),
),
),
),
);
},
);
}
}
+6 -7
View File
@@ -1,20 +1,18 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:stream_chat/stream_chat.dart';
import 'package:stream_chat_flutter/src/stream_chat_theme.dart'; import 'package:stream_chat_flutter/src/stream_chat_theme.dart';
import 'package:stream_chat_flutter/stream_chat_flutter.dart';
class UnreadIndicator extends StatelessWidget { class UnreadIndicator extends StatelessWidget {
const UnreadIndicator({ const UnreadIndicator({
Key key, Key key,
@required this.channel,
}) : super(key: key); }) : super(key: key);
final Channel channel;
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final client = StreamChat.of(context).client;
return StreamBuilder<int>( return StreamBuilder<int>(
stream: channel.state.unreadCountStream, stream: client.state.totalUnreadCountStream,
initialData: channel.state.unreadCount, initialData: client.state.totalUnreadCount,
builder: (context, snapshot) { builder: (context, snapshot) {
if (!snapshot.hasData || snapshot.data == 0) { if (!snapshot.hasData || snapshot.data == 0) {
return SizedBox(); return SizedBox();
@@ -42,6 +40,7 @@ class UnreadIndicator extends StatelessWidget {
), ),
), ),
); );
}); },
);
} }
} }