flutter_integrated_whb/lib/customWidget/ItemWidgetFactory.dart

510 lines
14 KiB
Dart
Raw Normal View History

2025-07-11 11:03:21 +08:00
import 'package:flutter/material.dart';
2025-07-29 08:53:00 +08:00
import 'package:qhd_prevention/customWidget/toast_util.dart';
2025-07-11 11:03:21 +08:00
import '../http/ApiService.dart';
2025-07-11 11:03:21 +08:00
import '../tools/tools.dart';
/// 自定义组件
class ListItemFactory {
/// 类型1横向spaceBetween布局两个文本加按钮
static Widget createRowSpaceBetweenItem({
required String leftText,
required String rightText,
double verticalPadding = 10,
double horizontalPadding = 0,
Color textColor = Colors.black,
bool isRight = false,
2025-08-07 17:33:16 +08:00
bool isRequired = false,
2025-07-11 11:03:21 +08:00
}) {
return Padding(
padding: EdgeInsets.symmetric(
vertical: verticalPadding,
horizontal: horizontalPadding,
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
2025-08-07 17:33:16 +08:00
Row(
children: [
if (isRequired) Text('* ', style: TextStyle(color: Colors.red)),
Text(
leftText,
style: TextStyle(
fontSize: 15,
fontWeight: FontWeight.bold,
color: textColor,
),
),
],
2025-07-11 11:03:21 +08:00
),
2025-08-07 17:33:16 +08:00
2025-07-11 11:03:21 +08:00
if (isRight)
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
2025-08-07 17:33:16 +08:00
_truncateText(rightText, 100),
// rightText,
2025-07-11 11:03:21 +08:00
style: TextStyle(fontSize: 15, color: Colors.grey),
),
2025-08-07 17:33:16 +08:00
SizedBox(width: 2),
Icon(
Icons.arrow_forward_ios_rounded,
color: Colors.black45,
size: 15,
),
2025-07-11 11:03:21 +08:00
],
)
else
Text(rightText, style: TextStyle(fontSize: 15, color: Colors.grey)),
],
),
);
}
2025-08-07 17:33:16 +08:00
// 辅助函数:截断文本
static String _truncateText(String text, int maxLength) {
if (text.length <= maxLength) return text;
return text.substring(0, maxLength) + '...';
2025-07-11 11:03:21 +08:00
}
///类型2上下布局两个文本自适应高度
static Widget createColumnTextItem({
required String topText,
required String bottomText,
double verticalPadding = 15,
double horizontalPadding = 0,
}) {
return Padding(
padding: EdgeInsets.symmetric(
vertical: verticalPadding,
horizontal: horizontalPadding,
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
Text(
topText,
style: TextStyle(
fontSize: 15,
fontWeight: FontWeight.bold,
color: Colors.black,
),
),
const SizedBox(height: 5),
Text(
bottomText,
style: TextStyle(fontSize: 15, color: Colors.grey),
softWrap: true,
maxLines: null, // 允许无限行数
),
],
),
);
}
/// 类型3文本和图片上下布局
static Widget createTextImageItem({
required String text,
required List<String> imageUrls,
double imageHeight = 90,
double verticalPadding = 10,
double horizontalPadding = 0,
// 点击图片时回调index 为被点击图片的下标
void Function(int index)? onImageTapped,
}) {
return Padding(
padding: EdgeInsets.symmetric(
vertical: verticalPadding,
horizontal: horizontalPadding,
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
text,
style: const TextStyle(
fontSize: 15,
fontWeight: FontWeight.bold,
color: Colors.black,
),
),
const SizedBox(height: 10),
Wrap(
spacing: 8, // 水平间距
runSpacing: 8, // 垂直间距
children: List.generate(imageUrls.length, (i) {
2025-08-07 17:33:16 +08:00
final url = ApiService.baseImgPath + imageUrls[i];
2025-07-11 11:03:21 +08:00
Widget img;
if (url.startsWith('http')) {
img = Image.network(
url,
height: imageHeight,
width: imageHeight * 3 / 2,
fit: BoxFit.cover,
);
} else {
img = Image.asset(
url,
height: imageHeight,
width: imageHeight * 3 / 2,
fit: BoxFit.cover,
);
}
return GestureDetector(
onTap: () {
if (onImageTapped != null) onImageTapped(i);
},
child: ClipRRect(
borderRadius: BorderRadius.circular(4),
child: img,
),
);
}),
),
],
),
);
}
2025-08-07 17:33:16 +08:00
2025-07-11 11:03:21 +08:00
/// 类型6文本和视频上下布局
static Widget createTextVideoItem({
required String text,
required String videoUrl,
double videoHeight = 90,
double verticalPadding = 10,
double horizontalPadding = 0,
VoidCallback? onVideoTapped,
}) {
return Padding(
padding: EdgeInsets.symmetric(
vertical: verticalPadding,
horizontal: horizontalPadding,
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
text,
style: const TextStyle(
fontSize: 15,
fontWeight: FontWeight.bold,
color: Colors.black,
),
),
const SizedBox(height: 10),
2025-08-14 15:05:48 +08:00
videoUrl.isNotEmpty ?
2025-07-11 11:03:21 +08:00
GestureDetector(
onTap: onVideoTapped,
child: Container(
height: videoHeight,
width: videoHeight * 3 / 2,
decoration: BoxDecoration(
color: Colors.grey[300],
borderRadius: BorderRadius.circular(4),
),
child: const Center(
child: Icon(
Icons.play_circle_outline,
size: 40,
color: Colors.white,
),
),
),
2025-08-14 15:05:48 +08:00
) : SizedBox(height: 10,)
2025-07-11 11:03:21 +08:00
],
),
);
}
///类型4一个文本自适应高度
static Widget createAloneTextItem({
required String text,
double verticalPadding = 10,
double horizontalPadding = 0,
}) {
return Padding(
padding: EdgeInsets.symmetric(
vertical: verticalPadding,
horizontal: horizontalPadding,
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
Text(
text,
style: TextStyle(fontSize: 15, color: Colors.grey),
softWrap: true,
maxLines: null, // 允许无限行数
),
],
),
);
}
2025-07-31 17:33:26 +08:00
/// YesNo
2025-07-11 11:03:21 +08:00
static Widget createYesNoSection({
required String title,
2025-08-11 17:40:03 +08:00
String yesLabel = '',
String noLabel = '',
2025-08-12 10:57:07 +08:00
required bool? groupValue,
2025-07-11 11:03:21 +08:00
required ValueChanged<bool> onChanged,
double verticalPadding = 15,
double horizontalPadding = 10,
2025-08-12 10:57:07 +08:00
bool isEdit = true,
String text = '',
2025-08-14 15:05:48 +08:00
bool isRequired = false,
2025-08-12 10:57:07 +08:00
2025-07-11 11:03:21 +08:00
}) {
return Padding(
2025-08-07 17:33:16 +08:00
padding: EdgeInsets.only(
top: 0,
right: horizontalPadding,
left: horizontalPadding,
bottom: verticalPadding,
),
2025-07-11 11:03:21 +08:00
child: Container(
padding: EdgeInsets.symmetric(horizontal: 10),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(5),
),
child: Row(
children: [
Expanded(
2025-08-14 15:05:48 +08:00
child: Row(
children: [
if (isRequired && isEdit) Text('* ', style: TextStyle(color: Colors.red)),
Text(
title,
style: TextStyle(
fontSize: 15,
fontWeight: FontWeight.bold,
color: Colors.black,
),
)
],
2025-07-11 11:03:21 +08:00
),
),
2025-08-12 10:57:07 +08:00
if (isEdit)
Row(
children: [
Row(
children: [
Radio<bool>(
activeColor: Colors.blue,
value: true,
groupValue: groupValue,
onChanged: (val) => onChanged(val!),
),
Text(yesLabel),
],
),
const SizedBox(width: 16),
Row(
children: [
Radio<bool>(
activeColor: Colors.blue,
value: false,
groupValue: groupValue,
onChanged: (val) => onChanged(val!),
),
Text(noLabel),
],
),
],
),
if (!isEdit)
Text(text, style: TextStyle(),)
2025-07-11 11:03:21 +08:00
],
),
),
);
}
2025-08-07 17:33:16 +08:00
2025-07-11 11:03:21 +08:00
/// 列表标题头(蓝色标识+文字)
2025-08-12 10:57:07 +08:00
static Widget createBuildSimpleSection(
String title, {
double horPadding = 10,
}) {
2025-07-11 11:03:21 +08:00
return Container(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(8),
),
child: Padding(
2025-08-11 17:40:03 +08:00
padding: EdgeInsets.symmetric(horizontal: horPadding, vertical: 10),
2025-07-11 11:03:21 +08:00
child: Row(
children: [
Container(width: 3, height: 15, color: Colors.blue),
const SizedBox(width: 8),
Text(
title,
style: const TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
),
],
),
),
);
}
2025-08-07 17:33:16 +08:00
2025-07-31 17:33:26 +08:00
/// 单纯标题
2025-08-07 17:33:16 +08:00
static Widget headerTitle(String title, {bool isRequired = false}) {
2025-07-31 17:33:26 +08:00
return Container(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(8),
),
child: Row(
children: [
2025-08-07 17:33:16 +08:00
if (isRequired) Text('* ', style: TextStyle(color: Colors.red)),
2025-07-31 17:33:26 +08:00
Text(
title,
style: const TextStyle(fontSize: 15, fontWeight: FontWeight.bold),
),
],
),
);
}
2025-08-07 17:33:16 +08:00
2025-07-11 11:03:21 +08:00
/// 扩展项(根据需求自定义)
static Widget createCustomItem({
required Widget child,
double verticalPadding = 15,
double horizontalPadding = 0,
}) {
return Padding(
padding: EdgeInsets.symmetric(
vertical: verticalPadding,
horizontal: horizontalPadding,
),
child: child,
);
}
2025-08-07 17:33:16 +08:00
2025-07-11 11:03:21 +08:00
/// 标题加输入框上下排列
static Widget createBuildMultilineInput(
2025-08-07 17:33:16 +08:00
String label,
String hint,
TextEditingController controller, {
bool isRequired = false,
}) {
2025-07-11 11:03:21 +08:00
return Container(
height: 130,
padding: const EdgeInsets.only(top: 8),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
2025-08-07 17:33:16 +08:00
Row(
children: [
if (isRequired) Text('* ', style: TextStyle(color: Colors.red)),
// 标题
HhTextStyleUtils.mainTitle(label, fontSize: 15),
],
),
2025-07-11 11:03:21 +08:00
const SizedBox(height: 8),
2025-07-31 17:33:26 +08:00
// 文本输入框,清除默认内边距以与标题左对齐
2025-07-11 11:03:21 +08:00
Expanded(
child: TextField(
2025-07-28 14:22:07 +08:00
autofocus: false,
2025-07-11 11:03:21 +08:00
controller: controller,
keyboardType: TextInputType.multiline,
maxLines: null,
expands: true,
style: const TextStyle(fontSize: 15),
decoration: InputDecoration(
hintText: hint,
border: InputBorder.none,
2025-08-07 17:33:16 +08:00
contentPadding:
isRequired
? EdgeInsets.symmetric(horizontal: 10)
: EdgeInsets.zero, // 去除默认内边距
2025-07-11 11:03:21 +08:00
),
),
),
],
),
);
}
2025-07-24 09:38:06 +08:00
2025-07-29 08:53:00 +08:00
/// 分类头部
static Widget createYesNoSectionTwo({
required String title,
required String yesLabel,
required String noLabel,
required bool groupValue,
required bool canClick,
required BuildContext context,
required ValueChanged<bool> onChanged,
double verticalPadding = 15,
double horizontalPadding = 10,
}) {
return Padding(
2025-08-07 17:33:16 +08:00
padding: EdgeInsets.only(
top: 0,
right: horizontalPadding,
left: horizontalPadding,
bottom: verticalPadding,
),
2025-07-29 08:53:00 +08:00
child: Container(
padding: EdgeInsets.symmetric(horizontal: 10),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(5),
),
child: Row(
children: [
Expanded(
child: Text(
title,
style: TextStyle(
fontSize: 15,
fontWeight: FontWeight.bold,
color: Colors.black,
),
),
),
Row(
children: [
Row(
children: [
Radio<bool>(
activeColor: Colors.blue,
value: true,
groupValue: groupValue,
2025-08-07 17:33:16 +08:00
onChanged: (val) {
if (canClick) {
2025-07-29 08:53:00 +08:00
onChanged(val!);
2025-08-07 17:33:16 +08:00
} else {
2025-07-29 08:53:00 +08:00
ToastUtil.showNormal(context, "重大隐患不允许选此项");
}
2025-08-07 17:33:16 +08:00
},
2025-07-29 08:53:00 +08:00
// (val) => onChanged(val!),
),
Text(yesLabel),
],
),
const SizedBox(width: 16),
Row(
children: [
Radio<bool>(
activeColor: Colors.blue,
value: false,
groupValue: groupValue,
onChanged: (val) => onChanged(val!),
),
Text(noLabel),
],
),
],
),
],
),
),
);
}
2025-07-11 11:03:21 +08:00
}