Skip to content

Offline gym log for Android. No account, no ads, no internet.

29.8 KB
Raw Blame
import 'package:flutter/material.dart';

import '../l10n/l10n.dart';
import '../state/fit_state.dart';
import '../theme/app_colors.dart';
import '../theme/app_theme.dart';
import '../widgets/dialogs.dart';
import '../widgets/glass.dart';
import '../widgets/ui_kit.dart';

class ToolDetailScreen extends StatelessWidget {
  const ToolDetailScreen({super.key, this.embedded = false});

  final bool embedded;

  @override
  Widget build(BuildContext context) {
    final gc = context.gc;
    final id = fit.activeToolId;
    final meta = _meta(id);
    final body = Column(
      crossAxisAlignment: CrossAxisAlignment.stretch,
      children: [
        if (embedded)
          Padding(
            padding: const EdgeInsetsDirectional.only(start: 4),
            child: Text(meta.$1, style: AppTheme.f(19, weight: FontWeight.w800, color: gc.text)),
          )
        else
          ScreenHeader(title: meta.$1, onBack: fit.closeTool, titleSize: 21),
        SizedBox(height: embedded ? 14 : 20),
        Container(
          padding: const EdgeInsets.fromLTRB(22, 24, 22, 24),
          decoration: BoxDecoration(
            color: gc.bgRaised,
            borderRadius: BorderRadius.circular(24),
          ),
          child: Column(children: [
            Text(t.result.toUpperCase(),
                style: AppTheme.f(10.5,
                    weight: FontWeight.w700, color: gc.textTertiary, letterSpacing: 1.6)),
            const SizedBox(height: 8),
            Text(meta.$2,
                textAlign: TextAlign.center,
                style: AppTheme.f(40, weight: FontWeight.w800, color: gc.text)),
            const SizedBox(height: 8),
            Text(meta.$3, style: AppTheme.f(13, weight: FontWeight.w500, color: gc.textSecondary), textAlign: TextAlign.center),
          ]),
        ),
        const SizedBox(height: 18),
        ..._grouped(_inputs(context, gc, id)),
      ],
    );
    if (embedded) return body;
    return SafeArea(
      bottom: false,
      child: SingleChildScrollView(
        clipBehavior: Clip.none,
        padding: const EdgeInsets.fromLTRB(20, 12, 20, 32),
        child: body,
      ),
    );
  }

  List<Widget> _grouped(List<Widget> items) {
    final out = <Widget>[];
    final run = <ToolRow>[];
    void add(Widget w) {
      if (out.isNotEmpty) out.add(const SizedBox(height: 10));
      out.add(w);
    }

    void flush() {
      if (run.isEmpty) return;
      add(ToolGroup([...run]));
      run.clear();
    }

    for (final w in items) {
      if (w is SizedBox) continue;
      if (w is ToolRow) {
        run.add(w);
        continue;
      }
      flush();
      add(w);
    }
    flush();
    return out;
  }

  (String, String, String) _meta(String? id) {
    switch (id) {
      case 'rm':
        return (t.toolTitle('rm'), ltr('${fit.weightValue(fit.rmResult)} ${fit.units}'), t.toolResultHint('rm'));
      case 'bmi':

        return (t.toolTitle('bmi'), fmt(fit.bmiVal), t.bmiCategory(fit.bmiCat));
      case 'cal':
        return (t.toolTitle('cal'), '${fit.tdee} kcal', t.toolResultHint('cal'));
      case 'bf':
        return (t.toolTitle('bf'), '${fmt(fit.bfVal)}%', t.toolResultHint('bf'));
      case 'plate':
        return (t.toolTitle('plate'), ltr('${fit.weightValue(fit.plateTarget)} ${fit.units}'), t.toolResultHint('plate'));
      case 'warmup':
        return (t.toolTitle('warmup'), ltr('${fit.weightValue(fit.warmupTarget)} ${fit.units}'), t.toolResultHint('warmup'));
      case 'rpe':
        return (t.toolTitle('rpe'), ltr('${fit.weightValue(fit.rpeResult)} ${fit.units}'),
            t.rpeResultHint(fit.rpeTargetReps, fmt(fit.rpeTargetRpe)));
      case 'dots':
        return (t.toolTitle('dots'), fmt(fit.dotsScore), t.dotsLevelName(fit.dotsLevel));
      case 'wilks':
        return (t.toolTitle('wilks'), fmt(fit.wilksScore), t.dotsLevelName(fit.wilksLevel));
      case 'ffmi':
        return (t.toolTitle('ffmi'), fmt(fit.ffmiScore), t.ffmiLevelName(fit.ffmiLevel));
      case 'protein':
        return (t.toolTitle('protein'), ltr('${fit.protLow}-${fit.protHigh} g'), t.toolResultHint('protein'));
      case 'water':
        return (t.toolTitle('water'), ltr('${fmt(fit.waterLiters)} L'), t.toolResultHint('water'));
      default:
        return ('', '', '');
    }
  }

  List<Widget> _inputs(BuildContext context, GymColors gc, String? id) {
    switch (id) {
      case 'rm':
        return [
          ToolRow(label: t.weightLifted, control: StepperControl(value: ltr('${fit.weightValue(fit.rmWeight)} ${fit.units}'), onDec: () => fit.bumpRmWeight(-fit.fromDisplayWeight(fit.weightStep)), onInc: () => fit.bumpRmWeight(fit.fromDisplayWeight(fit.weightStep)), onEdit: () => _editNumber(context, title: t.weightLifted, current: fit.toDisplayWeight(fit.rmWeight), decimal: true, apply: (v) => fit.bumpRmWeight(fit.fromDisplayWeight(v) - fit.rmWeight)))),
          const SizedBox(height: 10),
          ToolRow(label: t.repsPerformed, control: StepperControl(value: '${fit.rmReps}', minWidth: 40, onDec: () => fit.bumpRmReps(-1), onInc: () => fit.bumpRmReps(1), onEdit: () => _editNumber(context, title: t.repsPerformed, current: fit.rmReps.toDouble(), decimal: false, apply: (v) => fit.bumpRmReps(v.round() - fit.rmReps)))),
          const SizedBox(height: 10),
          ToolRow(label: t.rmPercent, control: StepperControl(value: '${fit.rmPct}%', minWidth: 52, onDec: () => fit.bumpRmPct(-1), onInc: () => fit.bumpRmPct(1), onEdit: () => _editNumber(context, title: t.rmPercent, current: fit.rmPct.toDouble(), decimal: false, apply: (v) => fit.bumpRmPct(v.round() - fit.rmPct)))),
          const SizedBox(height: 10),
          _rmPctCard(context, gc),
        ];
      case 'bmi':
        return [
          ToolRow(label: t.heightLabel, control: StepperControl(value: fit.heightLabel(fit.bmiHeight), onDec: () => fit.bumpBmiHeight(-fit.heightStep), onInc: () => fit.bumpBmiHeight(fit.heightStep), onEdit: () => _editNumber(context, title: t.heightLabel, current: fit.toDisplayCm(fit.bmiHeight), decimal: true, apply: (v) => fit.bumpBmiHeight(fit.fromDisplayCm(v) - fit.bmiHeight)))),
          const SizedBox(height: 10),
          ToolRow(label: t.weightLabel, control: StepperControl(value: ltr('${fit.weightValue(fit.bmiWeight)} ${fit.units}'), onDec: () => fit.bumpBmiWeight(-fit.fromDisplayWeight(fit.isLb ? 1 : 0.5)), onInc: () => fit.bumpBmiWeight(fit.fromDisplayWeight(fit.isLb ? 1 : 0.5)), onEdit: () => _editNumber(context, title: t.weightLabel, current: fit.toDisplayWeight(fit.bmiWeight), decimal: true, apply: (v) => fit.bumpBmiWeight(fit.fromDisplayWeight(v) - fit.bmiWeight)))),
        ];
      case 'cal':
        return [
          ToolRow(
            label: t.sexLabel,
            control: SegToggle([
              SegOption(t.male, fit.calSex == 'male', () => fit.setCalSex('male')),
              SegOption(t.female, fit.calSex == 'female', () => fit.setCalSex('female')),
            ]),
          ),
          const SizedBox(height: 10),
          ToolRow(label: t.ageLabel, control: StepperControl(value: '${fit.calAge}', minWidth: 40, onDec: () => fit.bumpCalAge(-1), onInc: () => fit.bumpCalAge(1), onEdit: () => _editNumber(context, title: t.ageLabel, current: fit.calAge.toDouble(), decimal: false, apply: (v) => fit.bumpCalAge(v.round() - fit.calAge)))),
          const SizedBox(height: 10),
          ToolRow(label: t.heightLabel, control: StepperControl(value: fit.heightLabel(fit.calHeight), onDec: () => fit.bumpCalHeight(-fit.heightStep), onInc: () => fit.bumpCalHeight(fit.heightStep), onEdit: () => _editNumber(context, title: t.heightLabel, current: fit.toDisplayCm(fit.calHeight), decimal: true, apply: (v) => fit.bumpCalHeight(fit.fromDisplayCm(v) - fit.calHeight)))),
          const SizedBox(height: 10),
          ToolRow(label: t.weightLabel, control: StepperControl(value: ltr('${fit.weightValue(fit.calWeight)} ${fit.units}'), onDec: () => fit.bumpCalWeight(-fit.fromDisplayWeight(fit.isLb ? 1 : 0.5)), onInc: () => fit.bumpCalWeight(fit.fromDisplayWeight(fit.isLb ? 1 : 0.5)), onEdit: () => _editNumber(context, title: t.weightLabel, current: fit.toDisplayWeight(fit.calWeight), decimal: true, apply: (v) => fit.bumpCalWeight(fit.fromDisplayWeight(v) - fit.calWeight)))),
          const SizedBox(height: 10),
          _activityCard(gc),
          const SizedBox(height: 10),
          _macros(gc),
        ];
      case 'bf':
        final male = fit.bfSex == 'male';
        final girth = male ? t.bfAbdomen : t.bfWaistNarrow;
        return [
          ToolRow(
            label: t.sexLabel,
            control: SegToggle([
              SegOption(t.male, male, () => fit.setBfSex('male')),
              SegOption(t.female, !male, () => fit.setBfSex('female')),
            ]),
          ),
          const SizedBox(height: 10),
          ToolRow(label: t.heightLabel, control: StepperControl(value: fit.heightLabel(fit.bfHeight), onDec: () => fit.bumpBfHeight(-fit.heightStep), onInc: () => fit.bumpBfHeight(fit.heightStep), onEdit: () => _editNumber(context, title: t.heightLabel, current: fit.toDisplayCm(fit.bfHeight), decimal: true, apply: (v) => fit.bumpBfHeight(fit.fromDisplayCm(v) - fit.bfHeight)))),
          const SizedBox(height: 10),
          ToolRow(label: t.bfNeckBelow, control: StepperControl(value: fit.cmLabel(fit.bfNeck), onDec: () => fit.bumpBfNeck(-fit.girthStep), onInc: () => fit.bumpBfNeck(fit.girthStep), onEdit: () => _editNumber(context, title: t.bfNeckBelow, current: fit.toDisplayCm(fit.bfNeck), decimal: true, apply: (v) => fit.bumpBfNeck(fit.fromDisplayCm(v) - fit.bfNeck)))),
          const SizedBox(height: 10),
          ToolRow(label: girth, control: StepperControl(value: fit.cmLabel(fit.bfWaist), onDec: () => fit.bumpBfWaist(-fit.girthStep), onInc: () => fit.bumpBfWaist(fit.girthStep), onEdit: () => _editNumber(context, title: girth, current: fit.toDisplayCm(fit.bfWaist), decimal: true, apply: (v) => fit.bumpBfWaist(fit.fromDisplayCm(v) - fit.bfWaist)))),
          if (!male) ...[
            const SizedBox(height: 10),
            ToolRow(label: t.bfHipWide, control: StepperControl(value: fit.cmLabel(fit.bfHip), onDec: () => fit.bumpBfHip(-fit.girthStep), onInc: () => fit.bumpBfHip(fit.girthStep), onEdit: () => _editNumber(context, title: t.bfHipWide, current: fit.toDisplayCm(fit.bfHip), decimal: true, apply: (v) => fit.bumpBfHip(fit.fromDisplayCm(v) - fit.bfHip)))),
          ],
        ];
      case 'plate':
        return [
          ToolRow(label: t.targetWeight, control: StepperControl(value: ltr('${fit.weightValue(fit.plateTarget)} ${fit.units}'), onDec: () => fit.bumpPlateTarget(-fit.fromDisplayWeight(fit.weightStep)), onInc: () => fit.bumpPlateTarget(fit.fromDisplayWeight(fit.weightStep)), onEdit: () => _editNumber(context, title: t.targetWeight, current: fit.toDisplayWeight(fit.plateTarget), decimal: true, apply: (v) => fit.bumpPlateTarget(fit.fromDisplayWeight(v) - fit.plateTarget)))),
          const SizedBox(height: 10),
          _barWeightCard(context, gc),
          const SizedBox(height: 10),
          _perSideCard(gc),
        ];
      case 'warmup':
        return [
          ToolRow(label: t.workingWeight, control: StepperControl(value: ltr('${fit.weightValue(fit.warmupTarget)} ${fit.units}'), onDec: () => fit.bumpWarmupTarget(-fit.fromDisplayWeight(fit.weightStep)), onInc: () => fit.bumpWarmupTarget(fit.fromDisplayWeight(fit.weightStep)), onEdit: () => _editNumber(context, title: t.workingWeight, current: fit.toDisplayWeight(fit.warmupTarget), decimal: true, apply: (v) => fit.bumpWarmupTarget(fit.fromDisplayWeight(v) - fit.warmupTarget)))),
          const SizedBox(height: 10),
          _warmupCard(gc),
        ];
      case 'rpe':
        final step = fit.fromDisplayWeight(fit.weightStep);
        return [
          _sectionLabel(gc, t.rpeYourSet),
          ToolRow(label: t.weightLifted, control: StepperControl(value: ltr('${fit.weightValue(fit.rpeWeight)} ${fit.units}'), onDec: () => fit.bumpRpeWeight(-step), onInc: () => fit.bumpRpeWeight(step), onEdit: () => _editNumber(context, title: t.weightLifted, current: fit.toDisplayWeight(fit.rpeWeight), decimal: true, apply: (v) => fit.bumpRpeWeight(fit.fromDisplayWeight(v) - fit.rpeWeight)))),
          ToolRow(label: t.repsPerformed, control: StepperControl(value: '${fit.rpeReps}', minWidth: 40, onDec: () => fit.bumpRpeReps(-1), onInc: () => fit.bumpRpeReps(1))),
          ToolRow(label: t.rpeEffortLabel, control: StepperControl(value: fmt(fit.rpeRpe), minWidth: 40, onDec: () => fit.bumpRpe(-0.5), onInc: () => fit.bumpRpe(0.5))),
          _sectionLabel(gc, t.rpeGoal),
          ToolRow(label: t.repsCol, control: StepperControl(value: '${fit.rpeTargetReps}', minWidth: 40, onDec: () => fit.bumpRpeTargetReps(-1), onInc: () => fit.bumpRpeTargetReps(1))),
          ToolRow(label: t.rpeEffortLabel, control: StepperControl(value: fmt(fit.rpeTargetRpe), minWidth: 40, onDec: () => fit.bumpRpeTarget(-0.5), onInc: () => fit.bumpRpeTarget(0.5))),
          _note(gc, '${t.rpeOneRmLine(fit.weightLabel(fit.rpeOneRm))}\n${t.rpeExplain}'),
        ];
      case 'dots':
      case 'wilks':
        final step = fit.fromDisplayWeight(fit.weightStep);
        Widget lift(String label, double kg, void Function(double) bump) => ToolRow(
              label: label,
              control: StepperControl(
                value: ltr('${fit.weightValue(kg)} ${fit.units}'),
                onDec: () => bump(-step),
                onInc: () => bump(step),
                onEdit: () => _editNumber(context, title: label, current: fit.toDisplayWeight(kg), decimal: true, apply: (v) => bump(fit.fromDisplayWeight(v) - kg)),
              ),
            );
        return [
          ToolRow(
            label: t.sexLabel,
            control: SegToggle([
              SegOption(t.male, fit.dotsSex == 'male', () => fit.setDotsSex('male')),
              SegOption(t.female, fit.dotsSex == 'female', () => fit.setDotsSex('female')),
            ]),
          ),
          lift(t.bodyweight, fit.dotsBody, fit.bumpDotsBody),
          _sectionLabel(gc, t.dotsBestLifts),
          lift(t.dotsSquat, fit.dotsSquat, fit.bumpDotsSquat),
          lift(t.dotsBench, fit.dotsBench, fit.bumpDotsBench),
          lift(t.dotsDeadlift, fit.dotsDeadlift, fit.bumpDotsDeadlift),
          _note(gc, '${t.dotsTotalLine(fit.weightLabel(fit.dotsTotal))}\n${id == 'wilks' ? t.wilksExplain : t.dotsExplain}'),
        ];
      case 'ffmi':
        return [
          ToolRow(
            label: t.sexLabel,
            control: SegToggle([
              SegOption(t.male, fit.ffmiSex == 'male', () => fit.setFfmiSex('male')),
              SegOption(t.female, fit.ffmiSex == 'female', () => fit.setFfmiSex('female')),
            ]),
          ),
          ToolRow(label: t.heightLabel, control: StepperControl(value: fit.heightLabel(fit.ffmiHeight), onDec: () => fit.bumpFfmiHeight(-fit.heightStep), onInc: () => fit.bumpFfmiHeight(fit.heightStep), onEdit: () => _editNumber(context, title: t.heightLabel, current: fit.toDisplayCm(fit.ffmiHeight), decimal: true, apply: (v) => fit.bumpFfmiHeight(fit.fromDisplayCm(v) - fit.ffmiHeight)))),
          ToolRow(label: t.weightLabel, control: StepperControl(value: ltr('${fit.weightValue(fit.ffmiWeight)} ${fit.units}'), onDec: () => fit.bumpFfmiWeight(-fit.fromDisplayWeight(fit.isLb ? 1 : 0.5)), onInc: () => fit.bumpFfmiWeight(fit.fromDisplayWeight(fit.isLb ? 1 : 0.5)), onEdit: () => _editNumber(context, title: t.weightLabel, current: fit.toDisplayWeight(fit.ffmiWeight), decimal: true, apply: (v) => fit.bumpFfmiWeight(fit.fromDisplayWeight(v) - fit.ffmiWeight)))),
          ToolRow(label: t.ffmiBodyFat, control: StepperControl(value: '${fmt(fit.ffmiBf)}%', minWidth: 52, onDec: () => fit.bumpFfmiBf(-0.5), onInc: () => fit.bumpFfmiBf(0.5), onEdit: () => _editNumber(context, title: t.ffmiBodyFat, current: fit.ffmiBf, decimal: true, apply: (v) => fit.bumpFfmiBf(v - fit.ffmiBf)))),
          _note(gc, '${t.ffmiLeanLine(fit.weightLabel(fit.ffmiLean))}\n${t.ffmiExplain}'),
        ];
      case 'protein':
        return [
          ToolRow(
            label: t.proteinGoalLabel,
            control: SegToggle([
              SegOption(t.paceLose, fit.protGoal == 'lose', () => fit.setProtGoal('lose')),
              SegOption(t.paceHold, fit.protGoal == 'hold', () => fit.setProtGoal('hold')),
              SegOption(t.paceGain, fit.protGoal == 'gain', () => fit.setProtGoal('gain')),
            ]),
          ),
          ToolRow(label: t.weightLabel, control: StepperControl(value: ltr('${fit.weightValue(fit.protWeight)} ${fit.units}'), onDec: () => fit.bumpProtWeight(-fit.fromDisplayWeight(fit.isLb ? 1 : 0.5)), onInc: () => fit.bumpProtWeight(fit.fromDisplayWeight(fit.isLb ? 1 : 0.5)), onEdit: () => _editNumber(context, title: t.weightLabel, current: fit.toDisplayWeight(fit.protWeight), decimal: true, apply: (v) => fit.bumpProtWeight(fit.fromDisplayWeight(v) - fit.protWeight)))),
          _note(gc, t.proteinExplain),
        ];
      case 'water':
        return [
          ToolRow(label: t.weightLabel, control: StepperControl(value: ltr('${fit.weightValue(fit.waterWeight)} ${fit.units}'), onDec: () => fit.bumpWaterWeight(-fit.fromDisplayWeight(fit.isLb ? 1 : 0.5)), onInc: () => fit.bumpWaterWeight(fit.fromDisplayWeight(fit.isLb ? 1 : 0.5)), onEdit: () => _editNumber(context, title: t.weightLabel, current: fit.toDisplayWeight(fit.waterWeight), decimal: true, apply: (v) => fit.bumpWaterWeight(fit.fromDisplayWeight(v) - fit.waterWeight)))),
          ToolRow(label: t.waterTrainingLabel, control: StepperControl(value: ltr('${fit.waterMinutes} min'), minWidth: 64, onDec: () => fit.bumpWaterMinutes(-15), onInc: () => fit.bumpWaterMinutes(15), onEdit: () => _editNumber(context, title: t.waterTrainingLabel, current: fit.waterMinutes.toDouble(), decimal: false, apply: (v) => fit.bumpWaterMinutes(v.round() - fit.waterMinutes)))),
          _note(gc, t.waterExplain),
        ];
      default:
        return const [];
    }
  }

  Widget _sectionLabel(GymColors gc, String label) => Padding(
        padding: const EdgeInsets.fromLTRB(4, 6, 4, 0),
        child: Text(label.toUpperCase(),
            style: AppTheme.f(10.5, weight: FontWeight.w700, color: gc.textTertiary, letterSpacing: 1.4)),
      );

  Widget _note(GymColors gc, String text) => Padding(
        padding: const EdgeInsets.fromLTRB(4, 4, 4, 0),
        child: Text(text, style: AppTheme.f(12, weight: FontWeight.w500, color: gc.textSecondary, height: 1.5)),
      );

  Future<void> _editNumber(
    BuildContext context, {
    required String title,
    required double current,
    required bool decimal,
    required void Function(double) apply,
  }) async {
    final parsed = await askNumber(context, title: title, initial: fmt(current), decimal: decimal);
    if (parsed != null) apply(parsed);
  }

  Widget _activityCard(GymColors gc) {
    Widget pill(String label, double v) {
      final active = fit.calActivity == v;
      return Pill(
        label: label,
        bg: active ? gc.ember : gc.bgRaised2,
        fg: active ? gc.onEmber : gc.textSecondary,
        onTap: () => fit.setCalActivity(v),
        hPad: 12,
        vPad: 7,
        fontSize: 12,
      );
    }

    return SoftCard(
      radius: 22,
      borderColor: Colors.transparent,
      padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 14),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(t.activityLevel, style: AppTheme.f(13, weight: FontWeight.w600, color: gc.textSecondary)),
          const SizedBox(height: 10),
          Wrap(spacing: 6, runSpacing: 6, children: [
            pill(t.activityName('Sedentary'), 1.2),
            pill(t.activityName('Light'), 1.375),
            pill(t.activityName('Moderate'), 1.55),
            pill(t.activityName('Active'), 1.725),
          ]),
        ],
      ),
    );
  }

  Widget _macros(GymColors gc) {
    Widget card(String label, String value) => Expanded(
          child: SoftCard(
            radius: 22,
            borderColor: Colors.transparent,
            padding: const EdgeInsets.all(12),
            child: Column(children: [
              Text(label, style: AppTheme.f(10, weight: FontWeight.w600, color: gc.textSecondary)),
              const SizedBox(height: 4),
              Text(value, style: AppTheme.f(16, weight: FontWeight.w700, color: gc.text)),
            ]),
          ),
        );
    return Row(children: [
      card(t.macroProtein, '${fit.calProtein}g'),
      const SizedBox(width: 10),
      card(t.macroCarbs, '${fit.calCarbs}g'),
      const SizedBox(width: 10),
      card(t.macroFat, '${fit.calFat}g'),
    ]);
  }

  Widget _barWeightCard(BuildContext context, GymColors gc) {
    final current = fit.plateBarDisplay;
    final bars = fit.barOptions;
    final own = !bars.contains(current);
    Widget opt(String label, bool active, VoidCallback onTap) {
      return Expanded(
        child: GestureDetector(
          onTap: onTap,
          child: Container(
            padding: const EdgeInsets.all(8),
            alignment: Alignment.center,
            decoration: BoxDecoration(color: active ? gc.ember : gc.bgRaised2, borderRadius: BorderRadius.circular(10)),
            child: Text(label,
                maxLines: 1,
                overflow: TextOverflow.ellipsis,
                style: AppTheme.f(13, weight: FontWeight.w600, color: active ? gc.onEmber : gc.textSecondary)),
          ),
        ),
      );
    }

    return SoftCard(
      radius: 22,
      borderColor: Colors.transparent,
      padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 14),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(t.barWeight, style: AppTheme.f(13, weight: FontWeight.w600, color: gc.textSecondary)),
          const SizedBox(height: 10),
          Row(children: [
            for (final v in bars) ...[
              opt('${fmt(v)} ${fit.units}', current == v, () => fit.setPlateBar(v)),
              const SizedBox(width: 8),
            ],
            opt(own ? ltr('${fmt(current)} ${fit.units}') : t.custom, own,
                () => _editNumber(context, title: t.barWeight, current: current, decimal: true, apply: fit.setPlateBar)),
          ]),
        ],
      ),
    );
  }

  Widget _perSideCard(GymColors gc) {
    final rows = fit.plateBreakdown;
    final target = fit.toDisplayWeight(fit.plateTarget);
    final loadable = fit.loadableTotal(target, fit.plateBarDisplay);
    return SoftCard(
      radius: 22,
      borderColor: Colors.transparent,
      padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 14),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(t.perSide, style: AppTheme.f(13, weight: FontWeight.w600, color: gc.textSecondary)),
          const SizedBox(height: 10),
          if (rows.isEmpty)
            Text(t.justTheBar, style: AppTheme.f(13, weight: FontWeight.w500, color: gc.textSecondary))
          else
            for (final p in rows)
              Padding(
                padding: const EdgeInsets.only(bottom: 8),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text('${fmt(p.weight)} ${fit.units}', style: AppTheme.f(14, weight: FontWeight.w600, color: gc.text)),
                    Text(t.perSideCount(p.count), style: AppTheme.f(13, weight: FontWeight.w500, color: gc.textSecondary)),
                  ],
                ),
              ),
          if (loadable < target - 0.01)
            Text(t.plateAchievable('${fmt(loadable)} ${fit.units}'),
                style: AppTheme.f(12, weight: FontWeight.w500, color: gc.brass)),
        ],
      ),
    );
  }

  Widget _rmPctCard(BuildContext context, GymColors gc) {
    final weight = fit.rmAtPct;
    final bar = fit.defaultBar;
    final plates = weight > bar ? fit.platesPerSide(weight, bar) : const <({double weight, int count})>[];
    return GestureDetector(
      behavior: HitTestBehavior.opaque,
      onTap: weight > bar ? () => showPlateSheet(context, weight) : null,
      child: SoftCard(
        radius: 22,
        borderColor: Colors.transparent,
        padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 14),
        child: Row(
          children: [
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(t.rmAtPercent('${fit.rmPct}%'),
                      style: AppTheme.f(13, weight: FontWeight.w500, color: gc.textSecondary)),
                  if (plates.isNotEmpty) ...[
                    const SizedBox(height: 3),
                    Text(plates.map((p) => p.count == 1 ? fmt(p.weight) : '${fmt(p.weight)}×${p.count}').join(' · '),
                        style: AppTheme.f(11.5, weight: FontWeight.w500, color: gc.textTertiary)),
                  ],
                ],
              ),
            ),
            const SizedBox(width: 12),
            Text('${fmt(weight)} ${fit.units}', style: AppTheme.f(17, weight: FontWeight.w800, color: gc.text)),
          ],
        ),
      ),
    );
  }

  Widget _warmupCard(GymColors gc) {
    final sets = fit.warmupSets;
    return SoftCard(
      radius: 22,
      borderColor: Colors.transparent,
      padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
      child: Column(
        children: [
          for (int i = 0; i < sets.length; i++)
            Container(
              padding: const EdgeInsets.symmetric(vertical: 10),
              decoration: BoxDecoration(
                border: i < sets.length - 1 ? Border(bottom: BorderSide(color: gc.border)) : null,
              ),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Text('${sets[i].pct} · ${t.repCount(sets[i].reps)}', style: AppTheme.f(13, weight: FontWeight.w500, color: gc.textSecondary)),
                  Text('${fmt(sets[i].weight)} ${fit.units}', style: AppTheme.f(15, weight: FontWeight.w700, color: gc.text)),
                ],
              ),
            ),
        ],
      ),
    );
  }
}

void showPlateSheet(BuildContext context, double displayTarget, {double? startBar}) {
  final gc = context.gc;
  var bar = startBar ?? fit.defaultBar;
  showAppSheet<void>(
    context: context,
    backgroundColor: Colors.transparent,
    builder: (sheet) => StatefulBuilder(
      builder: (sheet, setSheet) {
        final rows = fit.platesPerSide(displayTarget, bar);
        final loadable = fit.loadableTotal(displayTarget, bar);
        return Container(
          padding: sheetPad(sheet),
          decoration: sheetDecoration(gc),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: [
              const SheetHandle(),
              const SizedBox(height: 16),
              SheetTitle(t.toolTitle('plate')),
              const SizedBox(height: 4),
              Text('${fmt(displayTarget)} ${fit.units}',
                  textAlign: TextAlign.center,
                  style: AppTheme.f(30, weight: FontWeight.w700, color: gc.ember)),
              const SizedBox(height: 18),
              Text(t.barWeight,
                  style: AppTheme.f(10, weight: FontWeight.w700, color: gc.textTertiary, letterSpacing: 1.5)),
              const SizedBox(height: 8),
              Row(children: [
                for (final option in fit.barOptions) ...[
                  _sheetBarChip(gc, ltr('${fmt(option)} ${fit.units}'), bar == option, () => setSheet(() => bar = option)),
                  const SizedBox(width: 8),
                ],
                _sheetBarChip(
                    gc,
                    fit.barOptions.contains(bar) ? t.custom : ltr('${fmt(bar)} ${fit.units}'),
                    !fit.barOptions.contains(bar), () async {
                  final v = await askNumber(sheet, title: t.barWeight, initial: fmt(bar), decimal: true);
                  if (v != null && v >= 0) setSheet(() => bar = v);
                }),
              ]),
              const SizedBox(height: 18),
              Text(t.perSide,
                  style: AppTheme.f(10, weight: FontWeight.w700, color: gc.textTertiary, letterSpacing: 1.5)),
              const SizedBox(height: 10),
              if (rows.isEmpty)
                Text(t.justTheBar, style: AppTheme.f(13, weight: FontWeight.w500, color: gc.textSecondary))
              else
                for (final p in rows)
                  Padding(
                    padding: const EdgeInsets.only(bottom: 8),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Text('${fmt(p.weight)} ${fit.units}',
                            style: AppTheme.f(14, weight: FontWeight.w600, color: gc.text)),
                        Text(t.perSideCount(p.count), style: AppTheme.f(13, weight: FontWeight.w500, color: gc.textSecondary)),
                      ],
                    ),
                  ),
              if (loadable < displayTarget - 0.01) ...[
                const SizedBox(height: 2),
                Text(t.plateAchievable('${fmt(loadable)} ${fit.units}'),
                    style: AppTheme.f(12, weight: FontWeight.w500, color: gc.brass)),
              ],
              const SizedBox(height: 18),
              PrimaryButton(label: t.done, onTap: () => Navigator.of(sheet).pop()),
            ],
          ),
        );
      },
    ),
  );
}

Widget _sheetBarChip(GymColors gc, String label, bool active, VoidCallback onTap) => Expanded(
      child: GestureDetector(
        behavior: HitTestBehavior.opaque,
        onTap: onTap,
        child: Container(
          padding: const EdgeInsets.all(10),
          alignment: Alignment.center,
          decoration: BoxDecoration(
            color: active ? gc.ember : gc.bgRaised2,
            borderRadius: BorderRadius.circular(10),
          ),
          child: Text(label,
              maxLines: 1,
              overflow: TextOverflow.ellipsis,
              style: AppTheme.f(13, weight: FontWeight.w600, color: active ? gc.onEmber : gc.textSecondary)),
        ),
      ),
    );