flutter/lib/pages/chat/components/redpacket.dart

217 lines
9.0 KiB
Dart
Raw Normal View History

2025-07-21 15:46:30 +08:00
/// 发红包模板
library;
import 'package:flutter/material.dart';
2025-08-21 10:50:38 +08:00
import 'package:flutter/services.dart';
import 'package:shirne_dialog/shirne_dialog.dart';
2025-07-21 15:46:30 +08:00
class RedPacket extends StatefulWidget {
2025-08-21 10:50:38 +08:00
final bool flag; // true=群false=单
final void Function(Map<String, dynamic>)? onSend;
final int? maxNum; // 红包最大数量
const RedPacket({super.key, required this.flag, this.onSend, this.maxNum});
2025-07-21 15:46:30 +08:00
2025-08-21 10:50:38 +08:00
@override
State<RedPacket> createState() => _RedPacketState();
2025-07-21 15:46:30 +08:00
}
class _RedPacketState extends State<RedPacket> {
2025-08-21 10:50:38 +08:00
final TextEditingController _amountController = TextEditingController();
final TextEditingController _maxNumController = TextEditingController();
final TextEditingController _remarkController = TextEditingController();
2025-07-21 15:46:30 +08:00
String amount = '0.00';
2025-08-21 10:50:38 +08:00
String remark = '恭喜发财,大吉大利';
// 限制只能输入数字和小数点,且最多两位小数
final List<TextInputFormatter> _decimalInputFormatters = [
FilteringTextInputFormatter.allow(RegExp(r'^\d*\.?\d{0,2}')),
];
@override
void dispose() {
_amountController.dispose();
_maxNumController.dispose();
_remarkController.dispose();
super.dispose();
}
2025-07-21 15:46:30 +08:00
2025-08-21 10:50:38 +08:00
@override
Widget build(BuildContext context) {
return Material(
2025-07-21 15:46:30 +08:00
type: MaterialType.transparency,
child: Column(
children: [
ListView(
shrinkWrap: true,
padding: const EdgeInsets.only(bottom: 50.0),
children: [
2025-08-21 10:50:38 +08:00
if (widget.flag) const SizedBox(height: 10.0),
if (widget.flag)
Container(
margin: const EdgeInsets.symmetric(horizontal: 15.0),
padding: const EdgeInsets.symmetric(horizontal: 10.0),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(10.0),
),
child: Row(
children: <Widget>[
const Text('红包个数'),
Expanded(
child: TextField(
maxLength: widget.maxNum,
controller: _maxNumController,
textAlign: TextAlign.right,
buildCounter: (_, {required currentLength, maxLength, required isFocused}) => null, // 隐藏计数器
decoration: const InputDecoration(
hintText: "填写个数", isDense: true, hintStyle: TextStyle(fontSize: 14.0), border: OutlineInputBorder(borderSide: BorderSide.none)),
onChanged: (value) {
// 输入的红包个数
setState(() {
remark = value;
});
},
2025-07-21 15:46:30 +08:00
),
),
2025-08-21 10:50:38 +08:00
const Text(''),
],
),
2025-07-21 15:46:30 +08:00
),
const SizedBox(height: 10.0),
Container(
margin: const EdgeInsets.symmetric(horizontal: 15.0),
padding: const EdgeInsets.symmetric(horizontal: 10.0),
decoration: BoxDecoration(
2025-08-21 10:50:38 +08:00
color: Colors.white,
borderRadius: BorderRadius.circular(10.0),
2025-07-21 15:46:30 +08:00
),
child: Row(
children: <Widget>[
const Text('总金额'),
Expanded(
child: TextField(
2025-08-21 10:50:38 +08:00
controller: _amountController,
2025-07-21 15:46:30 +08:00
keyboardType: const TextInputType.numberWithOptions(decimal: true),
textAlign: TextAlign.right,
2025-08-21 10:50:38 +08:00
inputFormatters: _decimalInputFormatters,
maxLength: 6,
buildCounter: (_, {required currentLength, maxLength, required isFocused}) => null, // 隐藏计数器
2025-07-21 15:46:30 +08:00
decoration: const InputDecoration(
2025-08-21 10:50:38 +08:00
hintText: "¥0.00", isDense: true, hintStyle: TextStyle(fontSize: 14.0), border: OutlineInputBorder(borderSide: BorderSide.none)),
2025-07-21 15:46:30 +08:00
onChanged: (value) {
2025-08-21 10:50:38 +08:00
double val = double.tryParse(value) ?? 0.0;
if (val > 200) {
_amountController.text = '200';
val = 200;
}
2025-07-21 15:46:30 +08:00
setState(() {
2025-08-21 10:50:38 +08:00
amount = val.toStringAsFixed(2);
2025-07-21 15:46:30 +08:00
});
},
),
),
const Text(''),
],
),
),
const SizedBox(height: 10.0),
Container(
margin: const EdgeInsets.symmetric(horizontal: 15.0),
padding: const EdgeInsets.symmetric(horizontal: 10.0),
decoration: BoxDecoration(
2025-08-21 10:50:38 +08:00
color: Colors.white,
borderRadius: BorderRadius.circular(10.0),
2025-07-21 15:46:30 +08:00
),
child: Row(
children: <Widget>[
const Text('留言'),
Expanded(
child: TextField(
maxLines: null,
2025-08-21 10:50:38 +08:00
maxLength: 16,
controller: _remarkController,
2025-07-21 15:46:30 +08:00
keyboardType: TextInputType.multiline,
textAlign: TextAlign.right,
2025-08-21 10:50:38 +08:00
buildCounter: (_, {required currentLength, maxLength, required isFocused}) => null, // 隐藏计数器
2025-07-21 15:46:30 +08:00
decoration: const InputDecoration(
2025-08-21 10:50:38 +08:00
hintText: "恭喜发财,大吉大利",
isDense: true,
hintStyle: TextStyle(fontSize: 14.0),
border: OutlineInputBorder(borderSide: BorderSide.none)),
onChanged: (value) {
// 留言内容
setState(() {
if (value.isEmpty) {
remark = '恭喜发财,大吉大利';
} else {
remark = value;
}
});
},
2025-07-21 15:46:30 +08:00
),
),
],
),
),
const SizedBox(height: 30.0),
Row(
2025-08-21 10:50:38 +08:00
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[const Text('', style: TextStyle(fontSize: 24.0)), Text(amount, style: const TextStyle(fontSize: 36.0))]),
const SizedBox(
height: 20.0,
2025-07-21 15:46:30 +08:00
),
UnconstrainedBox(
constrainedAxis: Axis.vertical,
child: FilledButton(
style: ButtonStyle(
backgroundColor: WidgetStateProperty.all(Color(0xFFFF7F43)),
padding: WidgetStateProperty.all(EdgeInsets.zero),
minimumSize: WidgetStateProperty.all(const Size(180.0, 45.0)),
2025-08-21 10:50:38 +08:00
shape: WidgetStatePropertyAll(RoundedRectangleBorder(borderRadius: BorderRadius.circular(8.0))),
),
onPressed: () {
double amountDouble = double.tryParse(amount) ?? 0.0;
if (amountDouble > 0) {
//发送红包
widget.onSend!(
{
'maxNum': widget.maxNum ?? 1,
'amount': amount,
'remark': remark,
},
);
} else {
final baseStyle = MyDialog.theme.toastStyle?.top();
MyDialog.toast(
'未输入金额',
icon: const Icon(Icons.check_circle),
duration: Duration(milliseconds: 5000),
style: baseStyle?.copyWith(
backgroundColor: Colors.red.withAlpha(200),
),
);
}
},
child: const Text(
'塞钱进红包',
style: TextStyle(fontSize: 16.0),
2025-07-21 15:46:30 +08:00
),
),
),
2025-08-21 10:50:38 +08:00
const SizedBox(
height: 10.0,
),
2025-07-21 15:46:30 +08:00
const Align(
alignment: Alignment.center,
2025-08-21 10:50:38 +08:00
child: Text(
'未领取的红包将于24小时后发起退款',
style: TextStyle(color: Colors.grey, fontSize: 12.0),
),
2025-07-21 15:46:30 +08:00
),
],
),
],
),
);
2025-08-21 10:50:38 +08:00
}
2025-07-21 15:46:30 +08:00
}