qhd-prevention-flutter/lib/customWidget/ItemWidgetFactory.dart

272 lines
7.4 KiB
Dart
Raw Normal View History

2025-07-03 09:45:15 +08:00
import 'package:flutter/material.dart';
/// 自定义组件
class ListItemFactory {
/// 类型1横向spaceBetween布局两个文本
static Widget createRowSpaceBetweenItem({
required String leftText,
required String rightText,
double verticalPadding = 15,
double horizontalPadding = 0,
bool isRight = false,
}) {
return Padding(
padding: EdgeInsets.symmetric(
vertical: verticalPadding,
horizontal: horizontalPadding,
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
leftText,
style: TextStyle(
fontSize: 15,
fontWeight: FontWeight.bold,
color: Colors.black,
),
),
if (isRight)
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
rightText,
style: TextStyle(fontSize: 15, color: Colors.grey),
),
SizedBox(width: 2,),
Icon(Icons.arrow_forward_ios_rounded, size: 15),
],
)
else
Text(rightText, style: TextStyle(fontSize: 15, color: Colors.grey)),
],
),
);
}
///类型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 String imageUrl,
double imageHeight = 90,
double verticalPadding = 15,
double horizontalPadding = 0,
// 添加点击事件回调函数
VoidCallback? onImageTapped, // 新增的回调函数参数
}) {
return Padding(
padding: EdgeInsets.symmetric(
vertical: verticalPadding,
horizontal: horizontalPadding,
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
text,
style: TextStyle(
fontSize: 15,
fontWeight: FontWeight.bold,
color: Colors.black,
),
),
const SizedBox(height: 10),
// 使用GestureDetector包裹图片区域
GestureDetector(
onTap: onImageTapped, // 将外部传入的回调绑定到点击事件
child: Builder(
builder: (context) {
// 网络图片
if (imageUrl.startsWith('http')) {
return Image.network(
imageUrl,
height: imageHeight,
width: imageHeight * 3 / 2,
fit: BoxFit.cover,
alignment: Alignment.centerLeft,
);
}
// 本地图片
else {
return Image.asset(
imageUrl,
height: imageHeight,
width: double.infinity,
fit: BoxFit.cover,
alignment: Alignment.centerLeft,
);
}
},
),
),
],
),
);
}
///类型4一个文本自适应高度
static Widget createAloneTextItem({
required String text,
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(
text,
style: TextStyle(fontSize: 15, color: Colors.grey),
softWrap: true,
maxLines: null, // 允许无限行数
),
],
),
);
}
/// 分类头部
static Widget createYesNoSection({
required String title,
required String yesLabel,
required String noLabel,
required bool groupValue,
required ValueChanged<bool> onChanged,
double verticalPadding = 15,
double horizontalPadding = 0,
}) {
return Padding(
padding: EdgeInsets.symmetric(
vertical: verticalPadding,
horizontal: horizontalPadding,
),
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,
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),
],
),
],
),
],
),
),
);
}
static Widget createBuildSimpleSection(String title) {
return Container(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(8),
),
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 10),
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),
),
],
),
),
);
}
/// 扩展项(根据需求自定义)
static Widget createCustomItem({
required Widget child,
double verticalPadding = 15,
double horizontalPadding = 0,
}) {
return Padding(
padding: EdgeInsets.symmetric(
vertical: verticalPadding,
horizontal: horizontalPadding,
),
child: child,
);
}
}