import 'package:flutter/material.dart'; import '../models/problem.dart'; class ProblemCard extends StatelessWidget { final Problem problem; final bool isSelected; final bool isInCart; final VoidCallback onTap; final ValueChanged onAddToCart; final ValueChanged? onRemoveFromCart; const ProblemCard({ super.key, required this.problem, this.isSelected = false, this.isInCart = false, required this.onTap, required this.onAddToCart, this.onRemoveFromCart, }); @override Widget build(BuildContext context) { final preview = problem.bodyPreview.isNotEmpty ? problem.bodyPreview : '${problem.questionType} · ${problem.difficultyLabel}(${problem.difficulty})'; return Card( margin: const EdgeInsets.symmetric(horizontal: 8, vertical: 2), color: isSelected ? Theme.of(context).colorScheme.primaryContainer : null, child: ListTile( dense: true, leading: _typeIcon(), title: Text( preview, maxLines: 3, overflow: TextOverflow.ellipsis, style: const TextStyle(fontSize: 13, height: 1.4), ), subtitle: Row( children: [ _difficultyChip(context), const SizedBox(width: 4), if (!problem.isAnnotated) const Icon(Icons.warning_amber, size: 14, color: Colors.orange), ], ), trailing: isInCart ? IconButton( icon: const Icon(Icons.remove_shopping_cart, size: 18, color: Colors.red), onPressed: () => onRemoveFromCart?.call(problem.id), tooltip: '从试题篮移除', ) : IconButton( icon: const Icon(Icons.add_shopping_cart, size: 18), onPressed: () => onAddToCart(problem.id), tooltip: '加入试题篮', ), onTap: onTap, ), ); } Widget _difficultyChip(BuildContext context) { Color color; switch (problem.difficultyLabel) { case '易': color = Colors.green; break; case '中': color = Colors.orange; break; default: color = Colors.red; } return Container( padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 1), decoration: BoxDecoration( color: color.withValues(alpha: 0.15), borderRadius: BorderRadius.circular(8), ), child: Text( problem.difficultyLabel, style: TextStyle(fontSize: 11, color: color, fontWeight: FontWeight.w500), ), ); } Widget _typeIcon() { IconData icon; Color color; switch (problem.questionType) { case '单选题': icon = Icons.radio_button_checked; color = Colors.blue; break; case '多选题': icon = Icons.check_box; color = Colors.purple; break; case '填空题': icon = Icons.edit_note; color = Colors.orange; break; case '解答题': icon = Icons.assignment; color = Colors.green; break; default: icon = Icons.question_mark; color = Colors.grey; } return Icon(icon, color: color, size: 20); } }