forked from eatvio/eatvio_app
116 lines
3.2 KiB
Dart
116 lines
3.2 KiB
Dart
import 'package:eatvio_app/features/insights/models/insights_period.dart';
|
||
import 'package:eatvio_app/features/insights/presentation/widgets/insights_colors.dart';
|
||
import 'package:eatvio_app/generated/l10n.dart';
|
||
import 'package:flutter/material.dart';
|
||
|
||
extension BodyPeriodLabel on BodyPeriod {
|
||
String pillLabel(S s) {
|
||
switch (this) {
|
||
case BodyPeriod.month:
|
||
return s.insights_period_1m;
|
||
case BodyPeriod.threeMonths:
|
||
return s.insights_period_3m;
|
||
case BodyPeriod.sixMonths:
|
||
return s.insights_period_6m;
|
||
case BodyPeriod.year:
|
||
return s.insights_period_1y;
|
||
case BodyPeriod.all:
|
||
return s.insights_period_all;
|
||
}
|
||
}
|
||
}
|
||
|
||
extension DailyPeriodLabel on DailyPeriod {
|
||
String pillLabel(S s) {
|
||
switch (this) {
|
||
case DailyPeriod.week:
|
||
return s.insights_period_1w;
|
||
case DailyPeriod.twoWeeks:
|
||
return s.insights_period_2w;
|
||
case DailyPeriod.month:
|
||
return s.insights_period_1m;
|
||
case DailyPeriod.threeMonths:
|
||
return s.insights_period_3m;
|
||
case DailyPeriod.all:
|
||
return s.insights_period_all;
|
||
}
|
||
}
|
||
}
|
||
|
||
/// Пилюли выбора периода (`1М 3М 6М 1Г Всё время`).
|
||
class PeriodPills<T> extends StatelessWidget {
|
||
const PeriodPills({
|
||
super.key,
|
||
required this.periods,
|
||
required this.selected,
|
||
required this.labelOf,
|
||
required this.keyOf,
|
||
this.onSelected,
|
||
});
|
||
|
||
final List<T> periods;
|
||
final T selected;
|
||
final String Function(T period) labelOf;
|
||
final String Function(T period) keyOf;
|
||
|
||
/// `null` — пилюли неинтерактивны (данные для периода ещё не подключены).
|
||
final ValueChanged<T>? onSelected;
|
||
|
||
@override
|
||
Widget build(BuildContext context) {
|
||
return SingleChildScrollView(
|
||
scrollDirection: Axis.horizontal,
|
||
child: Row(
|
||
children: [
|
||
for (final period in periods)
|
||
Padding(
|
||
padding: const EdgeInsets.only(right: 6),
|
||
child: _Pill(
|
||
key: Key('insights_period_${keyOf(period)}'),
|
||
label: labelOf(period),
|
||
isActive: period == selected,
|
||
onTap: onSelected == null ? null : () => onSelected!(period),
|
||
),
|
||
),
|
||
],
|
||
),
|
||
);
|
||
}
|
||
}
|
||
|
||
class _Pill extends StatelessWidget {
|
||
const _Pill({
|
||
super.key,
|
||
required this.label,
|
||
required this.isActive,
|
||
required this.onTap,
|
||
});
|
||
|
||
final String label;
|
||
final bool isActive;
|
||
final VoidCallback? onTap;
|
||
|
||
@override
|
||
Widget build(BuildContext context) {
|
||
return Material(
|
||
color: isActive ? InsightsColors.blue : InsightsColors.accentBackground,
|
||
borderRadius: BorderRadius.circular(999),
|
||
child: InkWell(
|
||
borderRadius: BorderRadius.circular(999),
|
||
onTap: onTap,
|
||
child: Padding(
|
||
padding: const EdgeInsets.symmetric(horizontal: 13, vertical: 7),
|
||
child: Text(
|
||
label,
|
||
style: TextStyle(
|
||
fontSize: 12.5,
|
||
fontWeight: FontWeight.w700,
|
||
color: isActive ? Colors.white : InsightsColors.subtitle,
|
||
),
|
||
),
|
||
),
|
||
),
|
||
);
|
||
}
|
||
}
|