arblant
firefox-vpn-client / app / lib / ui / zed_portal_home.dart
zed_portal_home.dart199 lines · 5.9 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
import 'package:flutter/material.dart';

import '../app_settings.dart';
import '../core/core_controller.dart';
import '../l10n/app_localizations.dart';
import 'widgets.dart';
import 'zed_home_common.dart';

/// The Portal design, ported from FoxyVPN's Android app: an immersive
/// single column — the state word, the connect core glowing inside two
/// rings at the center, and every fact in one unified card below; then
/// the server, accounts and speed rows.
class ZedPortalHome extends StatelessWidget {
  const ZedPortalHome({
    super.key,
    required this.core,
    required this.settings,
    this.onOpenServers,
    this.onOpenAccounts,
  });

  final CoreController core;
  final AppSettings settings;
  final VoidCallback? onOpenServers;
  final VoidCallback? onOpenAccounts;

  @override
  Widget build(BuildContext context) {
    final l = AppLocalizations.of(context);
    final theme = Theme.of(context);
    final scheme = theme.colorScheme;
    final status = core.status!;
    final d = ZedHomeData(core, status, l);
    final title = d.live
        ? l.stateConnected
        : d.busy
        ? l.stateConnecting
        : l.stateOff;
    final ring = d.live
        ? scheme.primary
        : d.busy
        ? scheme.tertiary
        : scheme.surfaceContainerHighest;
    return ZedHomeScaffold(
      children: [
        const SizedBox(height: 12),
        AnimatedSwitcher(
          duration: const Duration(milliseconds: 250),
          reverseDuration: const Duration(milliseconds: 150),
          child: Text(
            title,
            key: ValueKey(title),
            textAlign: TextAlign.center,
            style: theme.textTheme.headlineSmall?.copyWith(
              fontWeight: FontWeight.w800,
              color: d.live ? scheme.primary : scheme.onSurface,
            ),
          ),
        ),
        if (d.live) ...[
          const SizedBox(height: 4),
          Center(
            child: ZedElapsedText(
              since: d.since,
              style: theme.textTheme.titleMedium,
              color: scheme.onSurfaceVariant,
            ),
          ),
        ],
        const SizedBox(height: 20),
        Center(
          child: SizedBox.square(
            dimension: 236,
            child: Stack(
              alignment: Alignment.center,
              children: [
                AnimatedContainer(
                  duration: const Duration(milliseconds: 500),
                  width: 236,
                  height: 236,
                  decoration: BoxDecoration(
                    shape: BoxShape.circle,
                    color: ring.withValues(alpha: 0.14),
                  ),
                ),
                AnimatedContainer(
                  duration: const Duration(milliseconds: 500),
                  width: 208,
                  height: 208,
                  decoration: BoxDecoration(
                    shape: BoxShape.circle,
                    color: ring.withValues(alpha: 0.20),
                  ),
                ),
                ZedConnectCore(
                  live: d.live,
                  busy: d.busy,
                  since: d.since,
                  size: 200,
                  onToggle: () => d.toggle(context, onOpenAccounts),
                ),
              ],
            ),
          ),
        ),
        const SizedBox(height: 20),
        Container(
          decoration: BoxDecoration(
            color: scheme.surfaceContainer,
            borderRadius: BorderRadius.circular(24),
          ),
          padding: const EdgeInsets.symmetric(vertical: 6),
          child: Column(
            children: [
              _InfoRow(
                label: l.homeLocation,
                value: d.place.name,
                flagCode: d.place.flag,
              ),
              Divider(
                indent: 20,
                endIndent: 20,
                color: scheme.outlineVariant.withValues(alpha: 0.5),
              ),
              _InfoRow(
                label: l.zedPing,
                value: d.pingMs == null ? '—' : '${d.pingMs} ms',
              ),
              Divider(
                indent: 20,
                endIndent: 20,
                color: scheme.outlineVariant.withValues(alpha: 0.5),
              ),
              _InfoRow(
                label: l.navAccounts,
                value: l.zedAccountsActive(d.live ? d.ready : 0, d.total),
              ),
            ],
          ),
        ),
        if (d.hasError) ...[
          const SizedBox(height: 12),
          ZedErrorCard(text: d.errorText),
        ],
        const SizedBox(height: 12),
        ZedServerRow(data: d, onTap: () => onOpenServers?.call()),
        const SizedBox(height: 12),
        ZedAccountsRow(
          data: d,
          onTap: () => d.openAccounts(context, onOpenAccounts),
        ),
        const SizedBox(height: 12),
        ZedSpeedRow(data: d),
        const SizedBox(height: 16),
      ],
    );
  }
}

/// One fact of the Portal card: the label at the start, the flag and
/// the value at the end.
class _InfoRow extends StatelessWidget {
  const _InfoRow({required this.label, required this.value, this.flagCode});

  final String label;
  final String value;
  final String? flagCode;

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);
    final scheme = theme.colorScheme;
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 12),
      child: Row(
        children: [
          Expanded(
            child: Text(
              label,
              style: theme.textTheme.labelLarge?.copyWith(
                color: scheme.onSurfaceVariant,
              ),
            ),
          ),
          if (flagCode != null && flagCode!.length == 2) ...[
            CountryFlag(code: flagCode!, width: 22),
            const SizedBox(width: 6),
          ],
          Text(
            value,
            style: theme.textTheme.titleSmall?.copyWith(
              fontWeight: FontWeight.w700,
            ),
          ),
        ],
      ),
    );
  }
}