90 lines
2.8 KiB
Dart
90 lines
2.8 KiB
Dart
import 'package:flutter/material.dart';
|
|
|
|
class ChatInput extends StatefulWidget {
|
|
final void Function(String text) onSend;
|
|
final double borderRadius;
|
|
final double horizontalPadding;
|
|
final double innerHorizontalPadding;
|
|
final double maxHeight;
|
|
|
|
const ChatInput({
|
|
super.key,
|
|
required this.onSend,
|
|
this.borderRadius = 12,
|
|
this.horizontalPadding = 10,
|
|
this.innerHorizontalPadding = 12,
|
|
this.maxHeight = 150,
|
|
});
|
|
|
|
@override
|
|
State<ChatInput> createState() => _ChatInputState();
|
|
}
|
|
|
|
class _ChatInputState extends State<ChatInput> {
|
|
final TextEditingController _controller = TextEditingController();
|
|
|
|
void _handleSend() {
|
|
final text = _controller.text.trim();
|
|
if (text.isEmpty) return;
|
|
widget.onSend(text);
|
|
_controller.clear();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final theme = Theme.of(context);
|
|
final activeColor = theme.colorScheme.primary;
|
|
return
|
|
Container(
|
|
color: Colors.grey[200],
|
|
padding: EdgeInsets.symmetric(horizontal: 8, vertical: 6),
|
|
child: Row(
|
|
crossAxisAlignment: CrossAxisAlignment.end,
|
|
children: [
|
|
SizedBox(width: 5),
|
|
Expanded(
|
|
child: Container(
|
|
padding: EdgeInsets.symmetric(horizontal: 12),
|
|
decoration: BoxDecoration(
|
|
color: Colors.white,
|
|
borderRadius: BorderRadius.circular(10),
|
|
),
|
|
constraints: BoxConstraints(maxWidth: 300), // 限制最大宽度(可选)
|
|
child: TextField(
|
|
controller: _controller,
|
|
onSubmitted: (_) => _handleSend,
|
|
maxLines: 10,
|
|
// 允许自动换行(关键)
|
|
minLines: 1,
|
|
// 最小1行
|
|
keyboardType: TextInputType.multiline,
|
|
// 启用多行输入
|
|
decoration: InputDecoration(
|
|
border: InputBorder.none,
|
|
contentPadding: EdgeInsets.symmetric(vertical: 10),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
SizedBox(width: 5),
|
|
Container(
|
|
decoration: BoxDecoration(
|
|
color: activeColor,
|
|
borderRadius: BorderRadius.circular(10)),
|
|
child: GestureDetector(
|
|
onTap:_handleSend,
|
|
child: Container(
|
|
width: 60,
|
|
height: 46,
|
|
decoration: BoxDecoration(
|
|
color:activeColor,
|
|
borderRadius: BorderRadius.circular(5)),
|
|
child: Icon(Icons.send,
|
|
color: Colors.white, size: 24)))),
|
|
SizedBox(width: 5)
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|