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
+39 -26
View File
@@ -1,44 +1,57 @@
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(
padding: const EdgeInsets.all(14.0), children: [
child: RawMaterialButton( Padding(
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(4)), padding: const EdgeInsets.all(14.0),
elevation: 0, child: RawMaterialButton(
highlightElevation: 0, shape:
focusElevation: 0, RoundedRectangleBorder(borderRadius: BorderRadius.circular(4)),
disabledElevation: 0, elevation: 0,
hoverElevation: 0, highlightElevation: 0,
onPressed: () { focusElevation: 0,
if (onPressed != null) { disabledElevation: 0,
onPressed(); hoverElevation: 0,
} else { onPressed: () {
Navigator.of(context).pop(); if (onPressed != null) {
} onPressed();
}, } else {
fillColor: Theme.of(context).brightness == Brightness.dark Navigator.of(context).pop();
? 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,
),
),
),
),
);
},
);
}
}
+30 -31
View File
@@ -1,47 +1,46 @@
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();
} }
return Material( return Material(
borderRadius: BorderRadius.circular(8), borderRadius: BorderRadius.circular(8),
color: StreamChatTheme.of(context) color: StreamChatTheme.of(context)
.channelPreviewTheme .channelPreviewTheme
.unreadCounterColor, .unreadCounterColor,
child: Padding( child: Padding(
padding: const EdgeInsets.only( padding: const EdgeInsets.only(
left: 5.0, left: 5.0,
right: 5.0, right: 5.0,
top: 2, top: 2,
bottom: 1, bottom: 1,
), ),
child: Center( child: Center(
child: Text( child: Text(
'${snapshot.data}', '${snapshot.data}',
style: TextStyle( style: TextStyle(
fontSize: 11, fontSize: 11,
color: Colors.white, color: Colors.white,
),
), ),
), ),
), ),
); ),
}); );
},
);
} }
} }