frosty/lib/widgets/chat.dart

113 lines
4 KiB
Dart
Raw Normal View History

2021-08-20 21:18:51 -04:00
import 'package:flutter/material.dart';
2021-10-01 00:10:50 -04:00
import 'package:flutter_mobx/flutter_mobx.dart';
import 'package:frosty/stores/auth_store.dart';
2021-10-01 00:10:50 -04:00
import 'package:frosty/stores/chat_store.dart';
import 'package:frosty/widgets/chat_stats.dart';
import 'package:provider/provider.dart';
2021-10-01 00:10:50 -04:00
class Chat extends StatefulWidget {
final ChatStore chatStore;
const Chat({Key? key, required this.chatStore}) : super(key: key);
2021-08-20 21:18:51 -04:00
2021-10-01 00:10:50 -04:00
@override
_ChatState createState() => _ChatState();
}
class _ChatState extends State<Chat> {
2021-08-20 21:18:51 -04:00
@override
Widget build(BuildContext context) {
2021-09-23 21:23:09 -04:00
return FutureBuilder(
future: widget.chatStore.getAssets(),
2021-10-01 00:10:50 -04:00
builder: (_, snapshot) {
2021-09-23 21:23:09 -04:00
if (snapshot.connectionState == ConnectionState.done) {
return Column(
children: [
Expanded(
child: Stack(
alignment: AlignmentDirectional.bottomCenter,
children: [
Observer(
builder: (_) {
return ListView.builder(
addAutomaticKeepAlives: false,
addRepaintBoundaries: false,
itemCount: widget.chatStore.messages.length,
controller: widget.chatStore.scrollController,
padding: const EdgeInsets.all(5.0),
itemBuilder: (context, index) {
return widget.chatStore.renderChatMessage(widget.chatStore.messages[index]);
},
);
},
),
Observer(
builder: (_) {
return Visibility(
visible: !widget.chatStore.autoScroll,
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 4.0),
width: double.infinity,
child: ElevatedButton(
onPressed: widget.chatStore.resumeScroll,
child: const Text('Resume Scroll'),
),
),
);
},
),
],
),
),
Row(
children: [
IconButton(
icon: const Icon(Icons.stacked_bar_chart),
onPressed: () {
showModalBottomSheet(
context: context,
builder: (context) {
return ChatStats(
chatStore: widget.chatStore,
);
},
);
},
),
if (context.read<AuthStore>().isLoggedIn)
Expanded(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: TextField(
decoration: const InputDecoration(
isDense: true,
contentPadding: EdgeInsets.all(8.0),
border: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(5.0)),
),
hintText: 'Send a message',
),
controller: widget.chatStore.textController,
onSubmitted: (string) => widget.chatStore.sendMessage(string),
),
),
),
],
)
],
2021-09-11 02:17:09 -04:00
);
2021-09-23 21:23:09 -04:00
}
return const Center(
child: CircularProgressIndicator(),
);
},
2021-08-20 21:18:51 -04:00
);
}
2021-10-01 00:10:50 -04:00
@override
void dispose() {
widget.chatStore.dispose();
2021-10-01 00:10:50 -04:00
super.dispose();
}
2021-08-26 16:37:37 -04:00
}