82 lines
2.2 KiB
Dart
82 lines
2.2 KiB
Dart
import "package:provider/provider.dart";
|
|
import "package:shadcn_flutter/shadcn_flutter.dart";
|
|
|
|
import "../providers/chat_provider.dart";
|
|
|
|
class InputBar extends StatefulWidget {
|
|
const InputBar({super.key});
|
|
|
|
@override
|
|
State<InputBar> createState() => _InputBarState();
|
|
}
|
|
|
|
class _InputBarState extends State<InputBar> {
|
|
late TextEditingController _controller;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_controller = TextEditingController();
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_controller.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
void _send(ChatProvider provider) {
|
|
final text = _controller.text.trim();
|
|
if (text.isEmpty) return;
|
|
provider.sendMessage(text);
|
|
_controller.clear();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Consumer<ChatProvider>(
|
|
builder: (context, chatProvider, _) {
|
|
return Container(
|
|
padding: const EdgeInsets.all(14),
|
|
decoration: const BoxDecoration(
|
|
border: Border(
|
|
top: BorderSide(color: Color(0xFFE2E8F0)),
|
|
),
|
|
),
|
|
child: Row(
|
|
crossAxisAlignment: CrossAxisAlignment.end,
|
|
children: [
|
|
|
|
Expanded(
|
|
child: TextField(
|
|
controller: _controller,
|
|
minLines: 1,
|
|
maxLines: 4,
|
|
placeholder: const Text("Type a message..."),
|
|
enabled: !chatProvider.isLoading,
|
|
onSubmitted: chatProvider.isLoading ? null : (_) => _send(chatProvider),
|
|
),
|
|
),
|
|
|
|
const SizedBox(width: 10),
|
|
|
|
chatProvider.isLoading
|
|
? const Padding(
|
|
padding: EdgeInsets.symmetric(horizontal: 8, vertical: 4),
|
|
child: SizedBox(
|
|
width: 22,
|
|
height: 22,
|
|
child: CircularProgressIndicator(value: null),
|
|
),
|
|
)
|
|
: PrimaryButton(
|
|
onPressed: () => _send(chatProvider),
|
|
child: const Text("Send"),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
},
|
|
);
|
|
}
|
|
}
|