feat: Added sharing and added actions for buttons

This commit is contained in:
Deven Joshi
2020-11-19 21:01:17 +05:30
parent dd829e18bc
commit 412c721ab8
7 changed files with 397 additions and 284 deletions
+295 -238
View File
@@ -11,6 +11,7 @@ import 'package:stream_chat_flutter/src/stream_chat.dart';
import 'package:stream_chat_flutter/src/stream_chat_theme.dart';
import 'package:stream_chat_flutter/src/stream_icons.dart';
import 'package:stream_chat_flutter/src/user_avatar.dart';
import 'package:stream_chat_flutter/stream_chat_flutter.dart';
import './channel_name.dart';
import 'channel_image.dart';
@@ -31,6 +32,7 @@ class ImageFooter extends StatefulWidget {
final int totalPages;
final List<String> urls;
final Message message;
/// Creates a channel header
ImageFooter({
@@ -41,6 +43,7 @@ class ImageFooter extends StatefulWidget {
this.currentPage = 0,
this.totalPages = 0,
this.urls,
this.message,
}) : preferredSize = Size.fromHeight(kToolbarHeight),
super(key: key);
@@ -57,7 +60,7 @@ class _ImageFooterState extends State<ImageFooter> {
TextEditingController _searchController = TextEditingController();
TextEditingController _messageController = TextEditingController();
List<User> selectedUsers = [];
List<User> _selectedUsers = [];
@override
void initState() {
@@ -134,8 +137,8 @@ class _ImageFooterState extends State<ImageFooter> {
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.only(
topRight: Radius.circular(8.0),
topLeft: Radius.circular(8.0),
topRight: Radius.circular(16.0),
topLeft: Radius.circular(16.0),
)),
child: Stack(
children: [
@@ -196,184 +199,197 @@ class _ImageFooterState extends State<ImageFooter> {
}
Widget _buildShareModal(context) {
final channel = StreamChannel.of(context).channel;
showDialog(
context: context,
builder: (context) {
print('KEY ${MediaQuery.of(context).viewInsets.bottom}');
print(MediaQuery.of(context).viewInsets.bottom == 0.0);
return Scaffold(
backgroundColor: Colors.transparent,
body: StatefulBuilder(builder: (modalContext, modalSetState) {
return Align(
alignment:
_userSearchMode ? Alignment.topCenter : Alignment.bottomCenter,
child: Material(
color: Colors.transparent,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16.0),
),
child: Stack(
children: [
ListView(
children: [
if (!_userSearchMode && MediaQuery.of(context).viewInsets.bottom == 0.0)
GestureDetector(
child: Container(
height: MediaQuery.of(modalContext).size.height / 2,
color: Colors.transparent,
return StreamChannel(
channel: channel,
child: Scaffold(
backgroundColor: Colors.transparent,
body: StatefulBuilder(builder: (modalContext, modalSetState) {
return Align(
alignment: _userSearchMode
? Alignment.topCenter
: Alignment.bottomCenter,
child: Material(
color: Colors.transparent,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16.0),
),
child: Stack(
children: [
ListView(
children: [
if (!_userSearchMode &&
MediaQuery.of(context).viewInsets.bottom == 0.0)
GestureDetector(
child: Container(
height:
MediaQuery.of(modalContext).size.height / 2,
color: Colors.transparent,
),
onTapUp: (val) {
Navigator.pop(modalContext);
},
),
onTapUp: (val) {
Navigator.pop(modalContext);
},
Container(
margin: EdgeInsets.symmetric(
horizontal: 8.0,
),
padding: EdgeInsets.symmetric(
vertical: 4.0,
),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.only(
topRight: Radius.circular(16.0),
topLeft: Radius.circular(16.0),
)),
child: _buildTextInputSection(modalSetState),
),
Container(
margin: EdgeInsets.symmetric(
horizontal: 8.0,
),
padding: EdgeInsets.symmetric(
vertical: 4.0,
),
decoration: BoxDecoration(
Container(
margin: EdgeInsets.symmetric(
horizontal: 8.0,
),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.only(
topRight: Radius.circular(16.0),
topLeft: Radius.circular(16.0),
)),
child: _buildTextInputSection(modalSetState),
),
Container(
margin: EdgeInsets.symmetric(
horizontal: 8.0,
),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.only(
bottomRight:
Radius.circular(_userSearchMode ? 16.0 : 0.0),
bottomLeft:
Radius.circular(_userSearchMode ? 16.0 : 0.0),
bottomRight: Radius.circular(
_userSearchMode ? 16.0 : 0.0),
bottomLeft: Radius.circular(
_userSearchMode ? 16.0 : 0.0),
),
),
),
child: FutureBuilder<QueryUsersResponse>(
future: _userQuery,
builder: (context, snapshot) {
if (!snapshot.hasData) {
return Center(
child: CircularProgressIndicator(),
);
}
var filteredData = snapshot.data.users.toList();
if (_userSearchMode &&
_searchController.text.length > 0) {
filteredData = snapshot.data.users
.where((element) => element.name
.toLowerCase()
.contains(_searchController.text
.toLowerCase()))
.toList();
}
return GridView.builder(
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
itemBuilder: (context, position) {
var user = filteredData[position];
var isUserSelected =
selectedUsers.contains(user);
return Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment:
MainAxisAlignment.start,
crossAxisAlignment:
CrossAxisAlignment.center,
children: [
Stack(
children: [
CircleAvatar(
maxRadius: 32.0,
child: UserAvatar(
onTap: (user) {
if (isUserSelected) {
selectedUsers.remove(user);
} else {
selectedUsers.add(user);
}
modalSetState(() {});
},
user: user,
constraints:
BoxConstraints.tightFor(
height:
isUserSelected ? 56 : 64,
width:
isUserSelected ? 56 : 64,
),
borderRadius:
BorderRadius.circular(32),
),
),
],
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
user.name,
style: Theme.of(context)
.textTheme
.subtitle2,
textAlign: TextAlign.center,
maxLines: 2,
),
),
],
),
child: FutureBuilder<QueryUsersResponse>(
future: _userQuery,
builder: (context, snapshot) {
if (!snapshot.hasData) {
return Center(
child: CircularProgressIndicator(),
);
},
itemCount: filteredData.length,
gridDelegate:
SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 4,
childAspectRatio: 0.75,
),
);
}),
),
],
),
if (!_userSearchMode && selectedUsers.isEmpty)
Align(
alignment: Alignment.bottomCenter,
child: Container(
color: Colors.white,
height: 48.0,
margin: EdgeInsets.symmetric(
horizontal: 8.0,
}
var filteredData =
snapshot.data.users.toList();
if (_userSearchMode &&
_searchController.text.length > 0) {
filteredData = snapshot.data.users
.where((element) => element.name
.toLowerCase()
.contains(_searchController.text
.toLowerCase()))
.toList();
}
return GridView.builder(
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
itemBuilder: (context, position) {
var user = filteredData[position];
var isUserSelected =
_selectedUsers.contains(user);
return Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment:
MainAxisAlignment.start,
crossAxisAlignment:
CrossAxisAlignment.center,
children: [
Stack(
children: [
CircleAvatar(
maxRadius: 32.0,
child: UserAvatar(
onTap: (user) {
if (isUserSelected) {
_selectedUsers
.remove(user);
} else {
_selectedUsers.add(user);
}
modalSetState(() {});
},
user: user,
constraints:
BoxConstraints.tightFor(
height: isUserSelected
? 56
: 64,
width: isUserSelected
? 56
: 64,
),
borderRadius:
BorderRadius.circular(
32),
),
),
],
),
Padding(
padding:
const EdgeInsets.all(8.0),
child: Text(
user.name,
style: Theme.of(context)
.textTheme
.subtitle2,
textAlign: TextAlign.center,
maxLines: 2,
),
),
],
),
);
},
itemCount: filteredData.length,
gridDelegate:
SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 4,
childAspectRatio: 0.75,
),
);
}),
),
child: SizedBox.expand(
child: Center(
child: Text(
'Save to Photos',
style: TextStyle(
color: StreamChatTheme.of(modalContext).accentColor,
fontWeight: FontWeight.bold,
],
),
if (!_userSearchMode && _selectedUsers.isEmpty)
Align(
alignment: Alignment.bottomCenter,
child: Container(
color: Colors.white,
height: 48.0,
margin: EdgeInsets.symmetric(
horizontal: 8.0,
),
child: SizedBox.expand(
child: Center(
child: Text(
'Save to Photos',
style: TextStyle(
color: StreamChatTheme.of(modalContext)
.accentColor,
fontWeight: FontWeight.bold,
),
),
),
),
),
),
),
if (!_userSearchMode && selectedUsers.isNotEmpty)
_buildShareTextInputSection(modalSetState),
],
if (!_userSearchMode && _selectedUsers.isNotEmpty)
_buildShareTextInputSection(modalSetState),
],
),
),
),
);
}),
);
}),
),
);
},
);
@@ -487,91 +503,93 @@ class _ImageFooterState extends State<ImageFooter> {
Widget _buildShareTextInputSection(modalSetState) {
return Align(
alignment: Alignment.bottomCenter,
child: Container(
color: Colors.white,
height: 56.0,
child: Row(
children: [
Expanded(
child: Padding(
padding: const EdgeInsets.only(left: 8.0),
child: TextField(
controller: _messageController,
onChanged: (val) {
modalSetState(() {});
},
onTap: () {
modalSetState(() {});
setState(() {});
},
decoration: InputDecoration(
isDense: true,
prefixText: ' ',
hintText: 'Add a comment',
border: OutlineInputBorder(
alignment: Alignment.bottomCenter,
child: Container(
color: Colors.white,
height: 56.0,
child: Row(
children: [
Expanded(
child: Padding(
padding: const EdgeInsets.only(left: 8.0),
child: TextField(
controller: _messageController,
onChanged: (val) {
modalSetState(() {});
},
onTap: () {
modalSetState(() {});
setState(() {});
},
decoration: InputDecoration(
isDense: true,
prefixText: ' ',
hintText: 'Add a comment',
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(32.0),
borderSide: BorderSide(
color: Colors.black.withOpacity(0.16),
),
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(32.0),
borderSide: BorderSide(
color: Colors.black.withOpacity(0.16),
),
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(32.0),
borderSide: BorderSide(
color: Colors.black.withOpacity(0.16),
)
),
contentPadding: EdgeInsets.symmetric(vertical: 12.0),
),
)),
contentPadding: EdgeInsets.symmetric(vertical: 12.0),
),
),
),
AnimatedCrossFade(
crossFadeState: _messageController.text.isNotEmpty
? CrossFadeState.showFirst
: CrossFadeState.showSecond,
firstChild: IconTheme(
data:
StreamChatTheme.of(context).channelTheme.messageInputButtonIconTheme,
child: Center(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: InkWell(
onTap: () {
// sendMessage();
},
child: Transform.rotate(
angle: -pi / 2,
child: Icon(
StreamIcons.send_message,
color: StreamChatTheme.of(context).accentColor,
),),
),
),
),
),
secondChild: IconTheme(
data:
StreamChatTheme.of(context).channelTheme.messageInputButtonIconTheme,
),
AnimatedCrossFade(
crossFadeState: _messageController.text.isNotEmpty
? CrossFadeState.showFirst
: CrossFadeState.showSecond,
firstChild: IconTheme(
data: StreamChatTheme.of(context)
.channelTheme
.messageInputButtonIconTheme,
child: Center(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Center(
child: InkWell(
onTap: () {},
child: Icon(
StreamIcons.send_message,
color: Colors.grey,
),
)),
child: InkWell(
onTap: () {
sendMessage();
},
child: Transform.rotate(
angle: -pi / 2,
child: Icon(
StreamIcons.send_message,
color: StreamChatTheme.of(context).accentColor,
),
),
),
),
),
duration: Duration(milliseconds: 300),
alignment: Alignment.center,
),
],
),
secondChild: IconTheme(
data: StreamChatTheme.of(context)
.channelTheme
.messageInputButtonIconTheme,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Center(
child: InkWell(
onTap: () {},
child: Icon(
StreamIcons.send_message,
color: Colors.grey,
),
)),
),
),
duration: Duration(milliseconds: 300),
alignment: Alignment.center,
),
],
),
);
),
);
}
Future<QueryUsersResponse> _queryUsers(context) {
@@ -587,4 +605,43 @@ class _ImageFooterState extends State<ImageFooter> {
],
);
}
/// Sends the current message
void sendMessage() async {
var text = _messageController.text.trim();
final attachments = widget.message.attachments;
_messageController.clear();
final client = StreamChat.of(context).client;
for(var user in _selectedUsers) {
var c = client.channel('messaging', extraData: {
'members': [
user.id,
StreamChat.of(context).user.id,
],
});
await c.create();
Future sendingFuture;
Message message;
message = (Message()).copyWith(
text: text,
attachments: attachments,
mentionedUsers: [],
showInChannel: false,
);
sendingFuture = c.sendMessage(message);
await sendingFuture;
}
Navigator.pop(context);
Navigator.pop(context);
}
}