import 'package:flutter/material.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:get/get.dart'; import 'package:intl/intl.dart'; import 'package:problem_check_system/data/models/problem_model.dart'; import 'package:problem_check_system/modules/problem/views/widgets/custom_button.dart'; import 'package:problem_check_system/modules/problem/views/problem_form_page.dart'; import 'package:tdesign_flutter/tdesign_flutter.dart'; // 定义枚举类型 enum ProblemCardViewType { buttons, checkbox } class ProblemCard extends StatelessWidget { final Problem problem; final ProblemCardViewType viewType; const ProblemCard( this.problem, { super.key, this.viewType = ProblemCardViewType.buttons, }); @override Widget build(BuildContext context) { return Card( // margin: EdgeInsets.symmetric(vertical: 5.h, horizontal: 9.w), child: Column( mainAxisSize: MainAxisSize.min, children: [ ListTile( leading: Image.asset( 'assets/images/problem_preview.png', fit: BoxFit.contain, ), title: Text('问题描述', style: TextStyle(fontSize: 16.sp)), subtitle: LayoutBuilder( builder: (context, constraints) { return Text( problem.description, maxLines: 2, overflow: TextOverflow.ellipsis, style: TextStyle(fontSize: 14.sp), ); }, ), ), SizedBox(height: 8.h), Row( children: [ SizedBox(width: 16.w), Row( children: [ Icon(Icons.location_on, color: Colors.grey, size: 16.h), SizedBox(width: 8.w), Text(problem.location, style: TextStyle(fontSize: 12.sp)), ], ), SizedBox(width: 16.w), Row( children: [ Icon(Icons.access_time, color: Colors.grey, size: 16.h), SizedBox(width: 8.w), Text( DateFormat('yyyy-MM-dd HH:mm').format(problem.createdAt), style: TextStyle(fontSize: 12.sp), ), ], ), ], ), SizedBox(height: 8.h), Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ SizedBox(width: 16.w), Wrap( spacing: 8, children: [ problem.isUploaded ? TDTag('已上传', isLight: true, theme: TDTagTheme.success) : TDTag('未上传', isLight: true, theme: TDTagTheme.danger), problem.bindData != null && problem.bindData!.isNotEmpty ? TDTag('已绑定', isLight: true, theme: TDTagTheme.primary) : TDTag('未绑定', isLight: true, theme: TDTagTheme.warning), ], ), const Spacer(), // 使用 Spacer 替代固定的 SizedBox _buildBottomActions(), ], ), SizedBox(height: 8.h), ], ), ); } // 渲染底部 UI 的私有方法 Widget _buildBottomActions() { switch (viewType) { case ProblemCardViewType.buttons: return Row( children: [ CustomButton( text: '修改', onTap: () { Get.to(ProblemFormPage(problem: problem)); }, ), SizedBox(width: 8.w), CustomButton( text: '查看', onTap: () { Get.to(ProblemFormPage(problem: problem, isReadOnly: true)); }, ), SizedBox(width: 16.w), ], ); case ProblemCardViewType.checkbox: return Padding( padding: EdgeInsets.only(right: 16.w), child: Obx( () => Checkbox( // 将 Checkbox 的 value 绑定到 controller.isChecked.value value: problem.isChecked.value, // 当 Checkbox 状态改变时,调用 controller 中的方法来更新状态 onChanged: (bool? value) { problem.isChecked.value = value ?? false; }, ), ), ); } } }