mirror of
https://github.com/GopeedLab/gopeed.git
synced 2026-11-05 16:15:43 +00:00
108 lines
3.1 KiB
Dart
108 lines
3.1 KiB
Dart
import 'package:flutter/widgets.dart';
|
|
|
|
import '../../core/utils/breakpoints.dart';
|
|
import '../theme/app_design_tokens.dart';
|
|
import '../theme/app_palette.dart';
|
|
|
|
class AppChoiceOption<T extends Object> {
|
|
const AppChoiceOption({required this.value, required this.label, required this.icon, this.key});
|
|
|
|
final T value;
|
|
final String label;
|
|
final IconData icon;
|
|
final String? key;
|
|
}
|
|
|
|
class AppChoiceSegmentedControl<T extends Object> extends StatelessWidget {
|
|
const AppChoiceSegmentedControl({
|
|
super.key,
|
|
required this.value,
|
|
required this.options,
|
|
required this.onChanged,
|
|
this.buttonKeyPrefix = 'choice',
|
|
this.alignment,
|
|
this.showIcons = true,
|
|
});
|
|
|
|
final T value;
|
|
final List<AppChoiceOption<T>> options;
|
|
final ValueChanged<T> onChanged;
|
|
final String buttonKeyPrefix;
|
|
final WrapAlignment? alignment;
|
|
final bool showIcons;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final palette = AppPalette.of(context);
|
|
final desktop = MediaQuery.sizeOf(context).width >= Breakpoints.mobile;
|
|
return Wrap(
|
|
spacing: 8,
|
|
runSpacing: 8,
|
|
alignment: alignment ?? (desktop ? WrapAlignment.end : WrapAlignment.start),
|
|
children: [
|
|
for (final option in options)
|
|
_ChoiceButton<T>(
|
|
key: ValueKey('$buttonKeyPrefix-${option.key ?? option.value}'),
|
|
option: option,
|
|
selected: option.value == value,
|
|
palette: palette,
|
|
showIcon: showIcons,
|
|
onPressed: () => onChanged(option.value),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
}
|
|
|
|
class _ChoiceButton<T extends Object> extends StatelessWidget {
|
|
const _ChoiceButton({
|
|
super.key,
|
|
required this.option,
|
|
required this.selected,
|
|
required this.palette,
|
|
required this.onPressed,
|
|
required this.showIcon,
|
|
});
|
|
|
|
final AppChoiceOption<T> option;
|
|
final bool selected;
|
|
final AppPalette palette;
|
|
final VoidCallback onPressed;
|
|
final bool showIcon;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return GestureDetector(
|
|
behavior: HitTestBehavior.opaque,
|
|
onTap: onPressed,
|
|
child: AnimatedContainer(
|
|
duration: const Duration(milliseconds: 140),
|
|
height: 34,
|
|
padding: const EdgeInsets.symmetric(horizontal: 10),
|
|
decoration: BoxDecoration(
|
|
color: selected ? palette.itemActiveBg : palette.cardBg,
|
|
borderRadius: BorderRadius.circular(AppDesignTokens.controlRadius),
|
|
border: Border.all(color: selected ? palette.textPrimary : palette.border),
|
|
),
|
|
child: Row(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
if (showIcon) ...[
|
|
Icon(option.icon, size: 15, color: selected ? palette.textPrimary : palette.textSecondary),
|
|
const SizedBox(width: 7),
|
|
],
|
|
Text(
|
|
option.label,
|
|
style: TextStyle(
|
|
color: selected ? palette.textPrimary : palette.textSecondary,
|
|
fontSize: 12,
|
|
fontWeight: FontWeight.w700,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|