flutter/lib/pages/video/components/popup_reply.dart

126 lines
4.0 KiB
Dart
Raw Normal View History

2025-07-21 15:46:30 +08:00
library;
import 'package:flutter/material.dart';
import 'package:get/get.dart';
class PopupReply extends StatefulWidget {
const PopupReply({
super.key,
2025-08-22 18:24:52 +08:00
this.onChanged,
this.onSubmitted, // 添加 onSubmitted 参数
this.hintText = '说点什么...',
2025-07-21 15:46:30 +08:00
});
// 输入框值改变
2025-08-22 18:24:52 +08:00
final ValueChanged<String>? onChanged;
final ValueChanged<String>? onSubmitted; // 定义 onSubmitted
final String hintText;
2025-07-21 15:46:30 +08:00
@override
State<PopupReply> createState() => _PopupReplyState();
}
class _PopupReplyState extends State<PopupReply> {
final TextEditingController controller = TextEditingController();
@override
void initState() {
super.initState();
}
@override
void dispose() {
super.dispose();
controller.dispose();
}
2025-08-22 18:24:52 +08:00
// 处理提交事件
void _handleSubmit() {
final text = controller.text.trim();
if (text.isNotEmpty) {
// 优先调用 onSubmitted
if (widget.onSubmitted != null) {
widget.onSubmitted!(text);
} else if (widget.onChanged != null) {
// 如果没有 onSubmitted则使用 onChanged
widget.onChanged!(text);
Get.back();
}
}
}
// 处理 onSubmitted 回调(接收 String 参数)
void _onSubmitted(String value) {
_handleSubmit();
}
2025-07-21 15:46:30 +08:00
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.transparent,
body: SafeArea(
child: Column(
children: [
Expanded(
child: GestureDetector(
child: Container(
color: Colors.transparent,
),
onTap: () {
Get.back();
},
),
),
Container(
color: Colors.white,
padding: EdgeInsets.all(10.0),
child: Row(
children: [
Expanded(
child: Container(
margin: EdgeInsets.only(right: 10.0),
decoration: BoxDecoration(
color: Colors.grey[200],
borderRadius: BorderRadius.circular(20.0),
),
child: TextField(
decoration: InputDecoration(
2025-08-22 18:24:52 +08:00
hintText: widget.hintText,
2025-07-21 15:46:30 +08:00
isDense: true,
hoverColor: Colors.transparent,
contentPadding: EdgeInsets.all(10.0),
border: OutlineInputBorder(borderSide: BorderSide.none),
),
style: TextStyle(fontSize: 14.0,),
textInputAction: TextInputAction.send,
autofocus: true,
maxLines: null,
controller: controller,
cursorColor: Color(0xFFFF5000),
2025-08-22 18:24:52 +08:00
onEditingComplete: _handleSubmit, // 使用统一的提交处理
onChanged: widget.onChanged, // 直接传递 onChanged
onSubmitted: _onSubmitted, // 传递正确的函数签名
2025-07-21 15:46:30 +08:00
),
),
),
FilledButton(
style: ButtonStyle(
backgroundColor: WidgetStateProperty.all(Color(0xFFFF5000)),
padding: WidgetStateProperty.all(EdgeInsets.zero),
minimumSize: WidgetStateProperty.all(Size(60.0, 40.0)),
shape: WidgetStatePropertyAll(
RoundedRectangleBorder(borderRadius: BorderRadius.circular(20.0))
)
),
2025-08-22 18:24:52 +08:00
onPressed: _handleSubmit, // 使用统一的提交处理
2025-07-21 15:46:30 +08:00
child: Text('发送',),
),
],
),
),
],
),
),
);
}
2025-08-22 18:24:52 +08:00
}