arblant
Railynx / lib / ui / widgets.dart
widgets.dart1213 lines · 36.7 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213
import 'dart:async';
import 'dart:math' as math;

import 'package:flutter/material.dart';

import '../app/latency.dart';
import '../app/look.dart';
import '../app/tokens.dart';

/// Glass panel, the HTML's .glass rule: a 160deg sheen gradient
/// (rgba(148,175,220,.10) -> .03), the .14 edge border, radius 26, an inset
/// top highlight and the deep soft shadow.
///
/// No BackdropFilter on purpose: the backdrop behind the cards is itself a
/// soft gradient field with no sharp detail, so the browser's blur(22px)
/// over it is visually identical to the unblurred backdrop — while stacking
/// a live backdrop filter under every card is the classic scroll-jank
/// source on mid-range phones. The look stays, the cost goes.
class GlassCard extends StatelessWidget {
  const GlassCard({super.key, required this.child, this.padding = const EdgeInsets.all(18)});

  final Widget child;
  final EdgeInsetsGeometry padding;

  @override
  Widget build(BuildContext context) {
    final radius = BorderRadius.circular(26);
    return Container(
      decoration: BoxDecoration(
        borderRadius: radius,
        // Rich shadows (the default, original look): the deep soft
        // shadow. The light set is the Settings trade-off — a gaussian
        // shadow this size is real raster work on weak GPUs.
        boxShadow: <BoxShadow>[
          if (Look.richShadows)
            BoxShadow(
              color: Colors.black.withValues(alpha: 0.55),
              blurRadius: 44,
              offset: const Offset(0, 20),
            )
          else
            BoxShadow(
              color: Colors.black.withValues(alpha: 0.45),
              blurRadius: 20,
              offset: const Offset(0, 12),
            ),
        ],
      ),
      child: ClipRRect(
        borderRadius: radius,
        child: Stack(
          children: <Widget>[
            Positioned.fill(
              child: DecoratedBox(
                decoration: BoxDecoration(
                  gradient: LinearGradient(
                    begin: const Alignment(-0.94, -0.34),
                    end: const Alignment(0.94, 0.34),
                    colors: <Color>[
                      Tokens.glassSheen.withValues(alpha: 0.10),
                      Tokens.glassSheen.withValues(alpha: 0.03),
                    ],
                  ),
                ),
              ),
            ),
            Container(
              padding: padding,
              decoration: BoxDecoration(
                borderRadius: radius,
                border: Border.all(color: Tokens.edge.withValues(alpha: 0.14)),
              ),
              child: child,
            ),
            // inset 0 1px 0 rgba(255,255,255,.07)
            Positioned(
              top: 0,
              left: 26,
              right: 26,
              child: Container(height: 1, color: Colors.white.withValues(alpha: 0.07)),
            ),
          ],
        ),
      ),
    );
  }
}

/// Small uppercase label used across the page (.label).
class SectionLabel extends StatelessWidget {
  const SectionLabel(this.text, {super.key});

  final String text;

  @override
  Widget build(BuildContext context) {
    return Text(
      text.toUpperCase(),
      style: const TextStyle(
        fontSize: 11,
        fontWeight: FontWeight.w800,
        letterSpacing: 1.4,
        color: Tokens.ink3,
      ),
    );
  }
}

/// Coloured pill with monospace text: latency chips and badges.
class Pill extends StatelessWidget {
  const Pill({super.key, required this.text, required this.color, this.mono = true});

  final String text;
  final Color color;
  final bool mono;

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 9, vertical: 4),
      decoration: BoxDecoration(
        color: color.withValues(alpha: 0.14),
        borderRadius: BorderRadius.circular(999),
        border: Border.all(color: color.withValues(alpha: 0.4)),
      ),
      child: Text(
        text,
        style: TextStyle(
          fontFamily: mono ? Tokens.monoFont : null,
          fontSize: 12,
          fontWeight: FontWeight.w700,
          color: color,
          fontFeatures: const <FontFeature>[FontFeature.tabularFigures()],
        ),
      ),
    );
  }
}

/// Latency chip, coloured by tier (fast/mid/slow/poor).
class LatencyChip extends StatelessWidget {
  const LatencyChip({super.key, required this.ms});

  final int ms;

  @override
  Widget build(BuildContext context) {
    return Pill(text: '$ms ms', color: tierColor(tierFor(ms)));
  }
}

/// IP address with dimmed dots, as in ui.html.
class IpText extends StatelessWidget {
  const IpText(this.ip, {super.key, this.size = 14, this.color = Tokens.ink});

  final String ip;
  final double size;
  final Color color;

  @override
  Widget build(BuildContext context) {
    final octets = ip.split('.');
    final spans = <InlineSpan>[];
    for (var i = 0; i < octets.length; i++) {
      if (i > 0) {
        spans.add(TextSpan(
          text: '.',
          style: TextStyle(color: Tokens.ink3.withValues(alpha: 0.7)),
        ));
      }
      spans.add(TextSpan(text: octets[i]));
    }
    return Text.rich(
      TextSpan(
        style: TextStyle(
          fontFamily: Tokens.monoFont,
          fontSize: size,
          fontWeight: FontWeight.w600,
          color: color,
        ),
        children: spans,
      ),
    );
  }
}

/// Gradient call-to-action button (.btn-grad): h58, radius 20, the aurora
/// gradient, dark ink text, the pink/teal lift shadow. In its [live] state
/// (scanning) it turns coral with the gradient flowing across it, exactly
/// like the mockup's .btn-grad.live.
class GradientButton extends StatefulWidget {
  const GradientButton({
    super.key,
    required this.label,
    required this.onPressed,
    this.icon,
    this.height = 58,
    this.radius = 20,
    this.fontSize = 16,
    this.live = false,
  });

  final String label;
  final VoidCallback? onPressed;
  final IconData? icon;
  final double height;
  final double radius;
  final double fontSize;
  final bool live;

  @override
  State<GradientButton> createState() => _GradientButtonState();
}

class _GradientButtonState extends State<GradientButton> with SingleTickerProviderStateMixin {
  late final AnimationController _flow = AnimationController(
    vsync: this,
    duration: const Duration(milliseconds: 4200),
  );

  @override
  void initState() {
    super.initState();
    if (widget.live) {
      // Ping-pong, never a hard restart: a plain repeat() snapped the
      // gradient back to its first frame every cycle (the user's "the
      // colours suddenly reset" report). Reversing makes the flow
      // continuous — there is no wrap point at all.
      _flow.repeat(reverse: true);
    }
  }

  @override
  void didUpdateWidget(GradientButton oldWidget) {
    super.didUpdateWidget(oldWidget);
    if (widget.live && !_flow.isAnimating) {
      _flow.repeat(reverse: true);
    } else if (!widget.live && _flow.isAnimating) {
      _flow.stop();
    }
  }

  @override
  void dispose() {
    _flow.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    final enabled = widget.onPressed != null;
    final radius = BorderRadius.circular(widget.radius);
    // No Opacity layer at full opacity (invisible plumbing, always
    // on). The shadows follow the Rich shadows switch: original =
    // the two coloured glows; light = one modest shadow — a gaussian
    // shadow this size re-rasterises on every repaint of the button,
    // which while scanning is every animation tick.
    final button = Container(
        decoration: BoxDecoration(
          borderRadius: radius,
          boxShadow: <BoxShadow>[
            if (Look.richShadows) ...<BoxShadow>[
              BoxShadow(
                color: (widget.live ? Tokens.poor : Tokens.pink).withValues(alpha: 0.45),
                blurRadius: 28,
                offset: const Offset(0, 12),
                spreadRadius: -8,
              ),
              BoxShadow(
                color: Tokens.teal.withValues(alpha: 0.35),
                blurRadius: 14,
                offset: const Offset(0, 4),
                spreadRadius: -4,
              ),
            ] else
              BoxShadow(
                color: (widget.live ? Tokens.poor : Tokens.pink).withValues(alpha: 0.38),
                blurRadius: 16,
                offset: const Offset(0, 8),
                spreadRadius: -6,
              ),
          ],
        ),
        child: Material(
          color: Colors.transparent,
          child: InkWell(
            borderRadius: radius,
            onTap: widget.onPressed,
            child: AnimatedBuilder(
            animation: _flow,
            builder: (context, _) {
              // .btn-grad.live: the same 200%-wide gradient, flowing.
              final shift = widget.live ? -1 + 2 * _flow.value : 0.0;
              final gradient = LinearGradient(
                begin: Alignment(shift - 1, -0.35),
                end: Alignment(shift + 1, 0.35),
                colors: Tokens.ringColors,
                stops: Tokens.ringStops,
              );
              return Ink(
                height: widget.height,
                decoration: BoxDecoration(
                  gradient: gradient,
                  borderRadius: radius,
                ),
                child: Center(
                  child: Row(
                    mainAxisSize: MainAxisSize.min,
                    children: <Widget>[
                      if (widget.icon != null) ...<Widget>[
                        Icon(widget.icon, size: 18, color: Tokens.onGradient),
                        const SizedBox(width: 10),
                      ],
                      Flexible(
                        child: Text(
                          widget.label,
                          softWrap: false,
                          overflow: TextOverflow.ellipsis,
                          style: TextStyle(
                            fontSize: widget.fontSize,
                            fontWeight: FontWeight.w800,
                            letterSpacing: 0.2,
                            color: Tokens.onGradient,
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
              );
            },
            ),
          ),
        ),
      );
    if (!enabled) {
      return Opacity(opacity: 0.4, child: button);
    }
    return button;
  }
}

/// Ghost or soft action button, matching .btn-ghost (h54, radius 19).
class TintButton extends StatelessWidget {
  const TintButton({
    super.key,
    required this.label,
    required this.onPressed,
    this.tint = Tokens.ink2,
    this.strong = false,
    this.height = 54,
    this.radius = 19,
    this.fontSize = 14,
  });

  final String label;
  final VoidCallback? onPressed;
  final Color tint;
  final bool strong;
  final double height;
  final double radius;
  final double fontSize;

  @override
  Widget build(BuildContext context) {
    final enabled = onPressed != null;
    final ghost = tint == Tokens.ink2 && !strong;
    final body = Material(
        color: Colors.transparent,
        child: InkWell(
          borderRadius: BorderRadius.circular(radius),
          onTap: onPressed,
          child: Container(
            height: height,
            padding: const EdgeInsets.symmetric(horizontal: 18),
            decoration: BoxDecoration(
              color: ghost
                  ? Tokens.glassSheen.withValues(alpha: 0.09)
                  : tint.withValues(alpha: strong ? 0.16 : 0.09),
              borderRadius: BorderRadius.circular(radius),
              border: Border.all(
                color: ghost
                    ? Tokens.edge.withValues(alpha: 0.18)
                    : tint.withValues(alpha: strong ? 0.35 : 0.18),
              ),
            ),
            child: Center(
              child: Text(
                label,
                style: TextStyle(
                  fontSize: fontSize,
                  fontWeight: FontWeight.w700,
                  color: Tokens.ink,
                ),
              ),
            ),
          ),
        ),
      );
    if (!enabled) {
      return Opacity(opacity: 0.4, child: body);
    }
    return body;
  }
}

/// The .chip button from the config card (h46, radius 14): Paste.
class ChipButton extends StatelessWidget {
  const ChipButton({super.key, required this.label, required this.onPressed});

  final String label;
  final VoidCallback? onPressed;

  @override
  Widget build(BuildContext context) {
    return Material(
      color: Colors.transparent,
      child: InkWell(
        borderRadius: BorderRadius.circular(14),
        onTap: onPressed,
        child: Container(
          height: 46,
          padding: const EdgeInsets.symmetric(horizontal: 16),
          decoration: BoxDecoration(
            color: Tokens.glassSheen.withValues(alpha: 0.09),
            borderRadius: BorderRadius.circular(14),
            border: Border.all(color: Tokens.edge.withValues(alpha: 0.14)),
          ),
          child: Center(
            child: Text(
              label,
              style: const TextStyle(fontSize: 13, fontWeight: FontWeight.w700, color: Tokens.ink),
            ),
          ),
        ),
      ),
    );
  }
}

/// Icon-only rounded button (.copy / .close / .chev family, 36px default).
class IconTap extends StatelessWidget {
  const IconTap({
    super.key,
    required this.icon,
    required this.onPressed,
    this.tooltip,
    this.size = 36,
    this.iconSize = 16,
  });

  final IconData icon;
  final VoidCallback onPressed;
  final String? tooltip;
  final double size;
  final double iconSize;

  @override
  Widget build(BuildContext context) {
    final button = Material(
      color: Colors.transparent,
      child: InkWell(
        borderRadius: BorderRadius.circular(12),
        onTap: onPressed,
        child: Container(
          width: size,
          height: size,
          decoration: BoxDecoration(
            color: Tokens.glassSheen.withValues(alpha: 0.08),
            borderRadius: BorderRadius.circular(12),
            border: Border.all(color: Tokens.edge.withValues(alpha: 0.14)),
          ),
          child: Icon(icon, size: iconSize, color: Tokens.ink2),
        ),
      ),
    );
    return tooltip == null ? button : Tooltip(message: tooltip!, child: button);
  }
}

/// Switch with the ui.html look (teal track when on).
class AppSwitch extends StatelessWidget {
  const AppSwitch({super.key, required this.value, required this.onChanged});

  final bool value;
  final ValueChanged<bool> onChanged;

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () => onChanged(!value),
      child: AnimatedContainer(
        duration: const Duration(milliseconds: 200),
        width: 44,
        height: 24,
        padding: const EdgeInsets.all(3),
        decoration: BoxDecoration(
          color: value ? Tokens.teal.withValues(alpha: 0.7) : Tokens.edge.withValues(alpha: 0.18),
          borderRadius: BorderRadius.circular(999),
        ),
        child: AnimatedAlign(
          duration: const Duration(milliseconds: 200),
          alignment: value ? Alignment.centerRight : Alignment.centerLeft,
          child: Container(
            width: 18,
            height: 18,
            decoration: const BoxDecoration(color: Tokens.ink, shape: BoxShape.circle),
          ),
        ),
      ),
    );
  }
}

/// Button that fires once on press and repeats while held (the stepper).
class HoldRepeatButton extends StatefulWidget {
  const HoldRepeatButton({super.key, required this.child, required this.onStep, this.enabled = true});

  final Widget child;
  final VoidCallback onStep;
  final bool enabled;

  @override
  State<HoldRepeatButton> createState() => _HoldRepeatButtonState();
}

class _HoldRepeatButtonState extends State<HoldRepeatButton> {
  Timer? _delay;
  Timer? _repeat;

  void _stop() {
    _delay?.cancel();
    _repeat?.cancel();
    _delay = null;
    _repeat = null;
  }

  @override
  void dispose() {
    _stop();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Listener(
      onPointerDown: (_) {
        if (!widget.enabled) {
          return;
        }
        widget.onStep();
        _delay = Timer(const Duration(milliseconds: 350), () {
          _repeat = Timer.periodic(const Duration(milliseconds: 70), (_) => widget.onStep());
        });
      },
      onPointerUp: (_) => _stop(),
      onPointerCancel: (_) => _stop(),
      child: widget.enabled ? widget.child : Opacity(opacity: 0.35, child: widget.child),
    );
  }
}

/// Segmented control with a sliding gradient thumb (.seg): Top N /
/// Best for all / Custom.
class Segmented extends StatelessWidget {
  const Segmented({
    super.key,
    required this.labels,
    required this.index,
    required this.onChanged,
  });

  final List<String> labels;
  final int index;
  final ValueChanged<int> onChanged;

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 48,
      padding: const EdgeInsets.all(4),
      decoration: BoxDecoration(
        color: Tokens.inputBg,
        borderRadius: BorderRadius.circular(18),
        border: Border.all(color: Tokens.edge.withValues(alpha: 0.14)),
      ),
      child: LayoutBuilder(
        builder: (context, constraints) {
          final segWidth = constraints.maxWidth / labels.length;
          return Stack(
            children: <Widget>[
              AnimatedPositioned(
                duration: const Duration(milliseconds: 450),
                curve: const Cubic(0.22, 1, 0.36, 1),
                left: index * segWidth,
                top: 0,
                bottom: 0,
                width: segWidth,
                child: Container(
                  decoration: BoxDecoration(
                    gradient: Tokens.grad,
                    borderRadius: BorderRadius.circular(14),
                    boxShadow: <BoxShadow>[
                      BoxShadow(
                        color: Tokens.indigo.withValues(alpha: 0.55),
                        blurRadius: Look.richShadows ? 18 : 10,
                        offset: const Offset(0, 6),
                        spreadRadius: -6,
                      ),
                    ],
                  ),
                ),
              ),
              Row(
                children: <Widget>[
                  for (var i = 0; i < labels.length; i++)
                    Expanded(
                      child: InkWell(
                        borderRadius: BorderRadius.circular(14),
                        onTap: () => onChanged(i),
                        child: Center(
                          child: Text(
                            labels[i],
                            maxLines: 1,
                            overflow: TextOverflow.ellipsis,
                            style: TextStyle(
                              fontSize: 13,
                              fontWeight: FontWeight.w800,
                              color: i == index ? Tokens.onGradient : Tokens.ink2,
                            ),
                          ),
                        ),
                      ),
                    ),
                ],
              ),
            ],
          );
        },
      ),
    );
  }
}

/// Circular progress ring with the aurora sweep stroke (.ring): r52 of a
/// 120 viewBox, stroke 7, the #gradStroke ramp, round caps, and the indigo
/// drop-shadow under the arc. [fraction] is animated, so the arc glides.
class ScanRing extends StatelessWidget {
  const ScanRing({
    super.key,
    required this.fraction,
    required this.child,
    this.size = 176,
    this.glow = true,
    this.glowBlur = true,
  });

  final double fraction;
  final Widget child;
  final double size;

  /// Settings switch: false drops the halo underlay and draws the
  /// arc as one flat colour (the ramp colour of the head position).
  final bool glow;

  /// Blurred glows switch, forwarded to the painter.
  final bool glowBlur;

  @override
  Widget build(BuildContext context) {
    final target = fraction.clamp(0.0, 1.0).toDouble();
    return SizedBox(
      width: size,
      height: size,
      child: TweenAnimationBuilder<double>(
        tween: Tween<double>(begin: 0, end: target),
        duration: const Duration(milliseconds: 600),
        curve: Curves.easeOutCubic,
        builder: (context, value, _) {
          return Stack(
            alignment: Alignment.center,
            children: <Widget>[
              CustomPaint(
                size: Size.square(size),
                painter: _RingPainter(value, glow: glow, glowBlur: glowBlur),
              ),
              child,
            ],
          );
        },
      ),
    );
  }
}

/// The ramp colour at sweep position [t] (0 = arc start, 1 = full circle),
/// from the mockup's #gradStroke stops.
Color ringRampColor(double t) {
  final colors = Tokens.ringColors;
  final stops = Tokens.ringStops;
  if (t <= stops.first) {
    return colors.first;
  }
  for (var i = 1; i < stops.length; i++) {
    if (t <= stops[i]) {
      final span = stops[i] - stops[i - 1];
      final local = span == 0 ? 0.0 : (t - stops[i - 1]) / span;
      return Color.lerp(colors[i - 1], colors[i], local)!;
    }
  }
  return colors.last;
}

class _RingPainter extends CustomPainter {
  _RingPainter(this.fraction, {this.glow = true, this.glowBlur = true});

  final double fraction;
  final bool glow;

  /// Blurred glows switch: true paints the original MaskFilter.blur
  /// halo; false paints the stepped halo (no filter pass).
  final bool glowBlur;

  @override
  void paint(Canvas canvas, Size size) {
    final scale = size.width / 120;
    final stroke = 7 * scale;
    final radius = 52 * scale;
    final center = size.center(Offset.zero);
    final rect = Rect.fromCircle(center: center, radius: radius);

    canvas.drawCircle(
      center,
      radius,
      Paint()
        ..style = PaintingStyle.stroke
        ..strokeWidth = stroke
        ..color = Tokens.edge.withValues(alpha: 0.09),
    );
    if (fraction <= 0) {
      return;
    }

    final sweep = 2 * math.pi * fraction;
    Shader shader() => const SweepGradient(
          colors: Tokens.ringColors,
          stops: Tokens.ringStops,
          transform: GradientRotation(-math.pi / 2),
        ).createShader(rect);

    // Butt caps on purpose: a round cap at the arc's start reaches BACK
    // past the top into the sweep's final degrees and picks up the amber
    // end of the ramp there — on the phone the tiny finished arc showed
    // an orange tip at its start (the user's report). The round head is
    // drawn separately as a dot in the ramp colour of the head position,
    // so amber can only ever appear as the circle nears completion.
    //
    // drop-shadow(0 0 8px rgba(129,140,248,.45)) in the mockup. Two
    // forms, chosen by the Blurred glows switch: the original is a
    // MaskFilter.blur pass over the ring canvas on every repaint
    // (rich, and the pricier one — the overlay readings put the
    // raster thread at 25-28ms); the light form is two wider, fainter
    // unblurred strokes, a stepped halo that reads the same at phone
    // scale. Skipped entirely when the ring glow setting is off; the
    // arc then paints flat.
    if (glow && glowBlur) {
      canvas.drawArc(
        rect,
        -math.pi / 2,
        sweep,
        false,
        Paint()
          ..style = PaintingStyle.stroke
          ..strokeCap = StrokeCap.butt
          ..strokeWidth = stroke + 3 * scale
          ..color = Tokens.indigo.withValues(alpha: 0.45)
          ..maskFilter = MaskFilter.blur(BlurStyle.normal, 8 * scale),
      );
    } else if (glow) {
      canvas.drawArc(
        rect,
        -math.pi / 2,
        sweep,
        false,
        Paint()
          ..style = PaintingStyle.stroke
          ..strokeCap = StrokeCap.butt
          ..strokeWidth = stroke + 9 * scale
          ..color = Tokens.indigo.withValues(alpha: 0.10),
      );
      canvas.drawArc(
        rect,
        -math.pi / 2,
        sweep,
        false,
        Paint()
          ..style = PaintingStyle.stroke
          ..strokeCap = StrokeCap.butt
          ..strokeWidth = stroke + 4 * scale
          ..color = Tokens.indigo.withValues(alpha: 0.20),
      );
    }
    canvas.drawArc(
      rect,
      -math.pi / 2,
      sweep,
      false,
      glow
          ? (Paint()
            ..style = PaintingStyle.stroke
            ..strokeCap = StrokeCap.butt
            ..strokeWidth = stroke
            ..shader = shader())
          : (Paint()
            ..style = PaintingStyle.stroke
            ..strokeCap = StrokeCap.butt
            ..strokeWidth = stroke
            ..color = ringRampColor(fraction)),
    );
    // The round head, in the exact ramp colour of its position.
    final headAngle = -math.pi / 2 + sweep;
    final head = center + Offset(math.cos(headAngle), math.sin(headAngle)) * radius;
    canvas.drawCircle(head, stroke / 2, Paint()..color = ringRampColor(fraction));
  }

  @override
  bool shouldRepaint(_RingPainter old) =>
      old.fraction != fraction || old.glow != glow || old.glowBlur != glowBlur;
}

/// The empty-state orbit from the mockup: a dashed outer circle, a plain
/// inner circle, the gradient core, and two dots circling at 14s and -9s.
///
/// The spin follows [animated] (the aurora-animation switch): this used
/// to repeat unconditionally, which meant the app scheduled frames
/// forever in its most common state — the idle empty card — even with
/// every effect switched off. Off now means a static single frame.
class OrbitArt extends StatefulWidget {
  const OrbitArt({super.key, this.size = 112, this.animated = true});

  final double size;
  final bool animated;

  @override
  State<OrbitArt> createState() => _OrbitArtState();
}

class _OrbitArtState extends State<OrbitArt> with SingleTickerProviderStateMixin {
  late final AnimationController _spin = AnimationController(
    vsync: this,
    duration: const Duration(seconds: 14),
  );

  @override
  void initState() {
    super.initState();
    _sync();
  }

  @override
  void didUpdateWidget(OrbitArt oldWidget) {
    super.didUpdateWidget(oldWidget);
    _sync();
  }

  void _sync() {
    if (widget.animated) {
      if (!_spin.isAnimating) {
        _spin.repeat();
      }
    } else {
      _spin.stop();
    }
  }

  @override
  void dispose() {
    _spin.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return RepaintBoundary(
      child: AnimatedBuilder(
        animation: _spin,
        builder: (context, _) => CustomPaint(
          size: Size.square(widget.size),
          painter: _OrbitPainter(_spin.value),
        ),
      ),
    );
  }
}

class _OrbitPainter extends CustomPainter {
  _OrbitPainter(this.t);

  final double t;

  @override
  void paint(Canvas canvas, Size size) {
    final s = size.width / 112;
    final center = Offset(56 * s, 56 * s);

    // Dashed outer circle: dash 3, gap 6 (in 112 units).
    final dashPaint = Paint()
      ..style = PaintingStyle.stroke
      ..strokeWidth = 1 * s
      ..color = Tokens.edge.withValues(alpha: 0.12);
    const circumference = 2 * math.pi * 50;
    const dashLen = 3.0;
    const gapLen = 6.0;
    var travelled = 0.0;
    while (travelled < circumference) {
      final startAngle = (travelled / 50) - math.pi / 2;
      final sweepAngle = dashLen / 50;
      canvas.drawArc(
        Rect.fromCircle(center: center, radius: 50 * s),
        startAngle,
        sweepAngle,
        false,
        dashPaint,
      );
      travelled += dashLen + gapLen;
    }

    canvas.drawCircle(
      center,
      34 * s,
      Paint()
        ..style = PaintingStyle.stroke
        ..strokeWidth = 1 * s
        ..color = Tokens.edge.withValues(alpha: 0.16),
    );

    // Gradient core, r12, opacity .9.
    final coreRect = Rect.fromCircle(center: center, radius: 12 * s);
    canvas.drawCircle(
      center,
      12 * s,
      Paint()
        ..shader = const LinearGradient(
          begin: Alignment.topLeft,
          end: Alignment.bottomRight,
          colors: <Color>[Tokens.teal, Tokens.indigo, Tokens.pink],
          stops: <double>[0, 0.6, 1],
        ).createShader(coreRect)
        ..color = Colors.white.withValues(alpha: 0.9),
    );

    // Orbiting dots: teal r4 on r50 (14s), pink r3 on r34 (reverse, 9s).
    final a1 = -math.pi / 2 + 2 * math.pi * t;
    canvas.drawCircle(
      center + Offset(math.cos(a1), math.sin(a1)) * 50 * s,
      4 * s,
      Paint()..color = Tokens.teal,
    );
    final a2 = -2 * math.pi * ((t * 14 / 9) % 1);
    canvas.drawCircle(
      center + Offset(math.cos(a2), math.sin(a2)) * 34 * s,
      3 * s,
      Paint()..color = Tokens.pink,
    );
  }

  @override
  bool shouldRepaint(_OrbitPainter old) => old.t != t;
}

/// The Shafaq backdrop: the mockup's four layered radial washes plus the
/// three drifting aurora blobs. Two painters on purpose:
///  * [_WashPainter] is fully static (base + washes), so its
///    RepaintBoundary rasterises it once and it is never repainted —
///    before, one combined painter redrew all seven gradients on every
///    animation tick, a real cost on mid-range phones.
///  * [_BlobPainter] carries only the three drifting blobs.
/// Blob repaint stays throttled to ~30fps (the drift runs 20-29s; nothing
/// on screen can tell the difference).
class AuroraBackdrop extends StatefulWidget {
  const AuroraBackdrop({
    super.key,
    required this.animated,
    this.showBlobs = true,
    this.showWashes = true,
    this.halfRes = false,
  });

  final bool animated;

  /// Settings switches: the drifting aurora discs and the static light
  /// washes can each be turned off (Performance preset turns off both,
  /// leaving the plain background colour).
  final bool showBlobs;
  final bool showWashes;

  /// Settings switch: rasterise the blob layer at half resolution and
  /// upscale it (a quarter of the pixels, the same soft picture).
  final bool halfRes;

  @override
  State<AuroraBackdrop> createState() => _AuroraBackdropState();
}

class _AuroraBackdropState extends State<AuroraBackdrop> with SingleTickerProviderStateMixin {
  late final AnimationController _clock = AnimationController(
    vsync: this,
    duration: const Duration(seconds: 290),
  );

  @override
  void initState() {
    super.initState();
    _sync();
  }

  @override
  void didUpdateWidget(AuroraBackdrop oldWidget) {
    super.didUpdateWidget(oldWidget);
    _sync();
  }

  void _sync() {
    if (widget.animated) {
      if (!_clock.isAnimating) {
        _clock.repeat();
      }
    } else {
      _clock.stop();
    }
  }

  @override
  void dispose() {
    _clock.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return IgnorePointer(
      child: Stack(
        fit: StackFit.expand,
        children: <Widget>[
          RepaintBoundary(
            child: CustomPaint(painter: _WashPainter(washes: widget.showWashes), size: Size.infinite),
          ),
          if (widget.showBlobs)
            AnimatedBuilder(
              animation: _clock,
              builder: (context, _) {
                final seconds = widget.animated ? _clock.value * 290 : 0.0;
                final step = (seconds * 30).floor() / 30;
                if (!widget.halfRes) {
                  // The original: full-resolution rasterisation.
                  return RepaintBoundary(
                    child: CustomPaint(
                      painter: _BlobPainter(step),
                      size: Size.infinite,
                    ),
                  );
                }
                // Half-resolution rasterisation: the blobs are soft
                // gradients, so a quarter of the pixels carries the
                // same picture. The boundary rasterises at half size;
                // the FittedBox upscales the cached texture.
                return LayoutBuilder(
                  builder: (context, constraints) {
                    final full = constraints.biggest;
                    return FittedBox(
                      fit: BoxFit.fill,
                      alignment: Alignment.topLeft,
                      child: SizedBox(
                        width: full.width / 2,
                        height: full.height / 2,
                        child: RepaintBoundary(
                          child: CustomPaint(
                            painter: _HalfResPainter(_BlobPainter(step), full),
                            size: Size(full.width / 2, full.height / 2),
                          ),
                        ),
                      ),
                    );
                  },
                );
              },
            ),
        ],
      ),
    );
  }
}

/// Paints [inner] (which works in full-size coordinates) into a
/// half-size canvas, for the backdrop's half-resolution blob layer.
class _HalfResPainter extends CustomPainter {
  _HalfResPainter(this.inner, this.fullSize);

  final _BlobPainter inner;
  final Size fullSize;

  @override
  void paint(Canvas canvas, Size size) {
    canvas.scale(0.5);
    inner.paint(canvas, fullSize);
  }

  @override
  bool shouldRepaint(_HalfResPainter old) =>
      old.inner.seconds != inner.seconds || old.fullSize != fullSize;
}

/// ease-in-out triangle wave for one blob's drift cycle.
double _driftPhase(double seconds, double period) {
  final x = (seconds % period) / period;
  final tri = x < 0.5 ? x * 2 : 2 - x * 2;
  return tri * tri * (3 - 2 * tri);
}

/// Base colour + the four .screen washes. Static — painted once.
class _WashPainter extends CustomPainter {
  _WashPainter({this.washes = true});

  /// False = background colour only (Performance preset).
  final bool washes;
  void _wash(Canvas canvas, Offset center, double rx, double ry, Color color, double fadeEnd) {
    final rect = Rect.fromCenter(center: center, width: rx * 2, height: ry * 2);
    final paint = Paint()
      ..shader = RadialGradient(
        colors: <Color>[color, color.withValues(alpha: 0)],
        stops: <double>[0, fadeEnd],
      ).createShader(rect);
    canvas.save();
    canvas.translate(center.dx, center.dy);
    canvas.scale(1, ry / rx);
    canvas.translate(-center.dx, -center.dy);
    canvas.drawCircle(center, rx, paint);
    canvas.restore();
  }

  @override
  void paint(Canvas canvas, Size size) {
    final w = size.width;
    final h = size.height;
    canvas.drawRect(Offset.zero & size, Paint()..color = Tokens.bg);
    if (!washes) {
      return;
    }

    // .screen background washes (values straight from the CSS).
    _wash(canvas, Offset(w * 0.5, -h * 0.12), w * 1.2, h * 0.55, Tokens.indigo.withValues(alpha: 0.38), 0.62);
    _wash(canvas, Offset(-w * 0.10, h * 0.28), w * 0.9, h * 0.45, Tokens.teal.withValues(alpha: 0.22), 0.62);
    _wash(canvas, Offset(w * 1.10, h * 0.48), w * 0.9, h * 0.55, Tokens.pink.withValues(alpha: 0.20), 0.62);
    _wash(canvas, Offset(w * 0.5, h * 1.12), w * 1.3, h * 0.42, Tokens.amber.withValues(alpha: 0.30), 0.70);
  }

  @override
  bool shouldRepaint(_WashPainter old) => old.washes != washes;
}

/// The three .aurora blobs: solid discs under CSS blur(72px) at opacity
/// .42. A blurred solid disc is NOT a centre-out gradient disc — the blur
/// keeps a broad bright plateau and spreads light ~two sigmas past the
/// disc's edge. The old gradient-disc approximation (.42 -> .20 -> 0 over
/// the bare disc radius) threw away most of that light, which is why the
/// app looked darker than the HTML (the user's report). The falloff below
/// follows the blurred-disc shape instead: plateau, then a soft tail,
/// over a radius widened by the blur spread.
class _BlobPainter extends CustomPainter {
  _BlobPainter(this.seconds);

  final double seconds;

  void _blob(Canvas canvas, Offset center, double r, Color color, double phase) {
    final dx = 42 * phase;
    final dy = -34 * phase;
    final scaled = r * (1 + 0.14 * phase);
    final c = center + Offset(dx, dy);
    const spread = 64.0; // ~2 sigma of the 72px CSS blur
    final drawRadius = scaled + spread;
    final plateau = (scaled - 30) / drawRadius;
    final fall = (scaled + 25) / drawRadius;
    final rect = Rect.fromCircle(center: c, radius: drawRadius);
    canvas.drawCircle(
      c,
      drawRadius,
      Paint()
        ..shader = RadialGradient(
          colors: <Color>[
            color.withValues(alpha: 0.42),
            color.withValues(alpha: 0.40),
            color.withValues(alpha: 0.15),
            color.withValues(alpha: 0),
          ],
          stops: <double>[0, plateau, fall, 1],
        ).createShader(rect),
    );
  }

  @override
  void paint(Canvas canvas, Size size) {
    final w = size.width;
    final h = size.height;

    // .aurora blobs (blur(72px) discs, drift 20s / 25s / 29s).
    _blob(canvas, Offset(-90 + 130, -70 + 130), 130, Tokens.teal, _driftPhase(seconds, 20));
    _blob(canvas, Offset(w + 120 - 150, 230 + 150), 150, Tokens.indigo, _driftPhase(seconds + 7, 25));
    _blob(canvas, Offset(30 + 120, h + 130 - 120), 120, Tokens.pink, _driftPhase(seconds + 13, 29));
  }

  @override
  bool shouldRepaint(_BlobPainter old) => old.seconds != seconds;
}