arblant
Railynx / lib / ui / ip_picker_dialog.dart
ip_picker_dialog.dart168 lines · 6.4 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
import 'package:flutter/material.dart';

import '../app/controller.dart';
import '../app/tokens.dart';
import '../engine/vless_scanner.dart';
import 'widgets.dart';

/// Custom-mode picker: choose one of the scan results for a config row,
/// or type an IP by hand. A typed IP is only accepted when the scan
/// actually found it — anything else gets an inline error (the user's
/// requirement: a custom IP outside the found set must error, not pass).
/// Ships as a dialog rather than the floating popup of ui.html (see
/// PORT_NOTES). [results] overrides the candidate list (multi-config
/// rows pick from their own server's results).
class IpPickerDialog extends StatefulWidget {
  const IpPickerDialog({super.key, required this.controller, required this.rowIndex, this.results});

  final AppController controller;
  final int rowIndex;
  final List<ScanResult>? results;

  @override
  State<IpPickerDialog> createState() => _IpPickerDialogState();
}

class _IpPickerDialogState extends State<IpPickerDialog> {
  final TextEditingController _typed = TextEditingController();
  String? _error;

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

  List<ScanResult> get _results => widget.results ?? widget.controller.results;

  static final RegExp _ipv4 = RegExp(r'^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$');

  void _applyTyped() {
    final ip = _typed.text.trim();
    final match = _ipv4.firstMatch(ip);
    final wellFormed =
        match != null && match.groups(<int>[1, 2, 3, 4]).every((g) => int.parse(g!) <= 255);
    if (!wellFormed) {
      setState(() => _error = 'Enter a valid IPv4 address.');
      return;
    }
    if (!_results.any((r) => r.ip == ip)) {
      setState(() => _error = 'That IP is not among the clean IPs found.');
      return;
    }
    widget.controller.pickIp(widget.rowIndex, ip);
    Navigator.of(context).pop();
  }

  @override
  Widget build(BuildContext context) {
    final c = widget.controller;
    final current = widget.rowIndex < c.rows.length ? c.rows[widget.rowIndex].ip : null;
    return Dialog(
      backgroundColor: Tokens.surface,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(18),
        side: BorderSide(color: Tokens.edge.withValues(alpha: 0.14)),
      ),
      child: ConstrainedBox(
        constraints: const BoxConstraints(maxWidth: 320, maxHeight: 430),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Padding(
              padding: const EdgeInsets.fromLTRB(16, 14, 16, 8),
              child: SectionLabel('Choose an IP for config #${widget.rowIndex + 1}'),
            ),
            Padding(
              padding: const EdgeInsets.symmetric(horizontal: 16),
              child: Row(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Expanded(
                    child: TextField(
                      controller: _typed,
                      keyboardType: TextInputType.number,
                      onSubmitted: (_) => _applyTyped(),
                      decoration: InputDecoration(
                        hintText: 'Type an IP — 69.46.46.7',
                        hintStyle: const TextStyle(
                          fontFamily: Tokens.monoFont,
                          fontSize: 12,
                          color: Tokens.placeholder,
                        ),
                        isDense: true,
                        filled: true,
                        fillColor: Tokens.inputBg,
                        contentPadding: const EdgeInsets.symmetric(horizontal: 12, vertical: 11),
                        border: OutlineInputBorder(
                          borderRadius: BorderRadius.circular(12),
                          borderSide: const BorderSide(color: Tokens.inputEdge),
                        ),
                        enabledBorder: OutlineInputBorder(
                          borderRadius: BorderRadius.circular(12),
                          borderSide: const BorderSide(color: Tokens.inputEdge),
                        ),
                        focusedBorder: OutlineInputBorder(
                          borderRadius: BorderRadius.circular(12),
                          borderSide: BorderSide(color: Tokens.teal.withValues(alpha: 0.7)),
                        ),
                      ),
                      style: const TextStyle(
                        fontFamily: Tokens.monoFont,
                        fontSize: 13,
                        color: Tokens.ink,
                      ),
                    ),
                  ),
                  const SizedBox(width: 8),
                  ChipButton(label: 'Use', onPressed: _applyTyped),
                ],
              ),
            ),
            if (_error != null)
              Padding(
                padding: const EdgeInsets.fromLTRB(16, 8, 16, 0),
                child: Text(
                  _error!,
                  style: const TextStyle(fontSize: 12, fontWeight: FontWeight.w600, color: Tokens.poor),
                ),
              ),
            const SizedBox(height: 6),
            Flexible(
              child: ListView(
                shrinkWrap: true,
                children: <Widget>[
                  for (final r in _results)
                    InkWell(
                      onTap: () {
                        c.pickIp(widget.rowIndex, r.ip);
                        Navigator.of(context).pop();
                      },
                      child: Padding(
                        padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 9),
                        child: Row(
                          children: <Widget>[
                            SizedBox(
                              width: 18,
                              child: r.ip == current
                                  ? const Icon(Icons.check_rounded, size: 16, color: Tokens.fast)
                                  : null,
                            ),
                            IpText(r.ip, size: 14),
                            const Spacer(),
                            LatencyChip(ms: r.delayMs),
                          ],
                        ),
                      ),
                    ),
                ],
              ),
            ),
            const SizedBox(height: 8),
          ],
        ),
      ),
    );
  }
}