Remove redundant provider

This commit is contained in:
Nutcake 2023-05-16 16:11:03 +02:00
parent c65868fa80
commit 41d51780bc

View file

@ -80,229 +80,230 @@ class _MessagesListState extends State<MessagesList> with SingleTickerProviderSt
.colorScheme .colorScheme
.surfaceVariant; .surfaceVariant;
return Consumer<MessagingClient>( return Consumer<MessagingClient>(
builder: (context, mClient, _) { builder: (context, mClient, _) {
final cache = mClient.getUserMessageCache(widget.friend.id); final cache = mClient.getUserMessageCache(widget.friend.id);
return Scaffold( return Scaffold(
appBar: AppBar( appBar: AppBar(
title: Row( title: Row(
crossAxisAlignment: CrossAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.center,
children: [ children: [
FriendOnlineStatusIndicator(userStatus: widget.friend.userStatus), FriendOnlineStatusIndicator(userStatus: widget.friend.userStatus),
const SizedBox(width: 8,), const SizedBox(width: 8,),
Text(widget.friend.username), Text(widget.friend.username),
if (widget.friend.isHeadless) Padding( if (widget.friend.isHeadless) Padding(
padding: const EdgeInsets.only(left: 12), padding: const EdgeInsets.only(left: 12),
child: Icon(Icons.dns, size: 18, color: Theme child: Icon(Icons.dns, size: 18, color: Theme
.of(context) .of(context)
.colorScheme .colorScheme
.onSecondaryContainer .onSecondaryContainer
.withAlpha(150),), .withAlpha(150),),
), ),
], ],
),
scrolledUnderElevation: 0.0,
backgroundColor: appBarColor,
),
body: Column(
children: [
if (sessions.isNotEmpty) Container(
constraints: const BoxConstraints(maxHeight: 64),
decoration: BoxDecoration(
color: appBarColor,
border: const Border(top: BorderSide(width: 1, color: Colors.black26),)
),
child: Stack(
children: [
ListView.builder(
controller: _sessionListScrollController,
scrollDirection: Axis.horizontal,
itemCount: sessions.length,
itemBuilder: (context, index) => SessionTile(session: sessions[index]),
),
AnimatedOpacity(
opacity: _shevronOpacity,
curve: Curves.easeOut,
duration: const Duration(milliseconds: 200),
child: Align(
alignment: Alignment.centerRight,
child: Container(
padding: const EdgeInsets.only(left: 16, right: 4, top: 1, bottom: 1),
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.centerLeft,
end: Alignment.centerRight,
colors: [
appBarColor.withOpacity(0),
appBarColor,
appBarColor,
],
),
),
height: double.infinity,
child: const Icon(Icons.chevron_right),
),
),
)
],
),
), ),
Expanded( scrolledUnderElevation: 0.0,
child: Builder( backgroundColor: appBarColor,
builder: (context) { ),
if (cache == null) { body: Column(
children: [
if (sessions.isNotEmpty) Container(
constraints: const BoxConstraints(maxHeight: 64),
decoration: BoxDecoration(
color: appBarColor,
border: const Border(top: BorderSide(width: 1, color: Colors.black26),)
),
child: Stack(
children: [
ListView.builder(
controller: _sessionListScrollController,
scrollDirection: Axis.horizontal,
itemCount: sessions.length,
itemBuilder: (context, index) => SessionTile(session: sessions[index]),
),
AnimatedOpacity(
opacity: _shevronOpacity,
curve: Curves.easeOut,
duration: const Duration(milliseconds: 200),
child: Align(
alignment: Alignment.centerRight,
child: Container(
padding: const EdgeInsets.only(left: 16, right: 4, top: 1, bottom: 1),
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.centerLeft,
end: Alignment.centerRight,
colors: [
appBarColor.withOpacity(0),
appBarColor,
appBarColor,
],
),
),
height: double.infinity,
child: const Icon(Icons.chevron_right),
),
),
)
],
),
),
Expanded(
child: Builder(
builder: (context) {
if (cache == null) {
return const Column( return const Column(
mainAxisAlignment: MainAxisAlignment.start, mainAxisAlignment: MainAxisAlignment.start,
children: [ children: [
LinearProgressIndicator() LinearProgressIndicator()
], ],
); );
} }
if (cache.error != null) { if (cache.error != null) {
return DefaultErrorWidget( return DefaultErrorWidget(
message: cache.error.toString(), message: cache.error.toString(),
onRetry: () { onRetry: () {
setState(() { setState(() {
mClient.deleteUserMessageCache(widget.friend.id); mClient.deleteUserMessageCache(widget.friend.id);
}); });
mClient.loadUserMessageCache(widget.friend.id); mClient.loadUserMessageCache(widget.friend.id);
},
);
}
if (cache.messages.isEmpty) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Icon(Icons.message_outlined),
Padding(
padding: const EdgeInsets.symmetric(vertical: 24),
child: Text(
"There are no messages here\nWhy not say hello?",
textAlign: TextAlign.center,
style: Theme
.of(context)
.textTheme
.titleMedium,
),
)
],
),
);
}
return ListView.builder(
controller: _messageScrollController,
reverse: true,
itemCount: cache.messages.length,
itemBuilder: (context, index) {
final entry = cache.messages[index];
if (index == cache.messages.length - 1) {
return Padding(
padding: const EdgeInsets.only(top: 12),
child: MessageBubble(message: entry,),
);
}
return MessageBubble(message: entry,);
}, },
); );
} },
if (cache.messages.isEmpty) { ),
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Icon(Icons.message_outlined),
Padding(
padding: const EdgeInsets.symmetric(vertical: 24),
child: Text(
"There are no messages here\nWhy not say hello?",
textAlign: TextAlign.center,
style: Theme
.of(context)
.textTheme
.titleMedium,
),
)
],
),
);
}
return ListView.builder(
controller: _messageScrollController,
reverse: true,
itemCount: cache.messages.length,
itemBuilder: (context, index) {
final entry = cache.messages[index];
if (index == cache.messages.length - 1) {
return Padding(
padding: const EdgeInsets.only(top: 12),
child: MessageBubble(message: entry,),
);
}
return MessageBubble(message: entry,);
},
);
},
), ),
), AnimatedContainer(
AnimatedContainer( decoration: BoxDecoration(
decoration: BoxDecoration( boxShadow: [
boxShadow: [ BoxShadow(
BoxShadow( blurRadius: _showBottomBarShadow ? 8 : 0,
blurRadius: _showBottomBarShadow ? 8 : 0, color: Theme
color: Theme.of(context).shadowColor, .of(context)
offset: const Offset(0, 4), .shadowColor,
), offset: const Offset(0, 4),
],
color: Theme.of(context).colorScheme.background,
),
padding: const EdgeInsets.symmetric(horizontal: 4),
duration: const Duration(milliseconds: 250),
child: Row(
children: [
Expanded(
child: Padding(
padding: const EdgeInsets.all(8),
child: TextField(
enabled: cache != null && cache.error == null,
autocorrect: true,
controller: _messageTextController,
maxLines: 4,
minLines: 1,
onChanged: (text) {
if (text.isNotEmpty && !_isSendable) {
setState(() {
_isSendable = true;
});
} else if (text.isEmpty && _isSendable) {
setState(() {
_isSendable = false;
});
}
},
decoration: InputDecoration(
isDense: true,
hintText: "Message ${widget.friend
.username}...",
hintMaxLines: 1,
contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(24)
)
),
),
), ),
), ],
Padding( color: Theme
padding: const EdgeInsets.only(left: 8, right: 4.0), .of(context)
child: Consumer<MessagingClient>( .colorScheme
builder: (context, mClient, _) { .background,
return IconButton( ),
splashRadius: 24, padding: const EdgeInsets.symmetric(horizontal: 4),
onPressed: _isSendable ? () async { duration: const Duration(milliseconds: 250),
setState(() { child: Row(
_isSendable = false; children: [
}); Expanded(
final message = Message( child: Padding(
id: Message.generateId(), padding: const EdgeInsets.all(8),
recipientId: widget.friend.id, child: TextField(
senderId: apiClient.userId, enabled: cache != null && cache.error == null,
type: MessageType.text, autocorrect: true,
content: _messageTextController.text, controller: _messageTextController,
sendTime: DateTime.now().toUtc(), maxLines: 4,
); minLines: 1,
try { onChanged: (text) {
mClient.sendMessage(message); if (text.isNotEmpty && !_isSendable) {
_messageTextController.clear();
setState(() {});
} catch (e) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text("Failed to send message\n$e",
maxLines: null,
),
),
);
setState(() { setState(() {
_isSendable = true; _isSendable = true;
}); });
} else if (text.isEmpty && _isSendable) {
setState(() {
_isSendable = false;
});
} }
} : null, },
iconSize: 28, decoration: InputDecoration(
icon: const Icon(Icons.send), isDense: true,
); hintText: "Message ${widget.friend
}, .username}...",
hintMaxLines: 1,
contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(24)
)
),
),
),
), ),
) Padding(
], padding: const EdgeInsets.only(left: 8, right: 4.0),
child: IconButton(
splashRadius: 24,
onPressed: _isSendable ? () async {
setState(() {
_isSendable = false;
});
final message = Message(
id: Message.generateId(),
recipientId: widget.friend.id,
senderId: apiClient.userId,
type: MessageType.text,
content: _messageTextController.text,
sendTime: DateTime.now().toUtc(),
);
try {
mClient.sendMessage(message);
_messageTextController.clear();
setState(() {});
} catch (e) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text("Failed to send message\n$e",
maxLines: null,
),
),
);
setState(() {
_isSendable = true;
});
}
} : null,
iconSize: 28,
icon: const Icon(Icons.send),
),
),
],
),
), ),
), ],
], ),
), );
); }
}
); );
} }
} }