Improve Android connection controls and status display

This commit is contained in:
Misaka
2026-05-17 16:42:40 +08:00
parent 1286a63507
commit 31d1681947
10 changed files with 778 additions and 115 deletions

View File

@@ -28,12 +28,15 @@ class WirelessTextSyncerApp extends StatelessWidget {
enum SendButtonState { idle, sending, success }
const connectionInfoFontFamily = 'WDXL Lubrifont SC';
class ConnectionSnapshot {
const ConnectionSnapshot({
this.connected = false,
this.connecting = false,
this.host = '',
this.port = 8181,
this.name = '',
this.lastError,
});
@@ -47,6 +50,7 @@ class ConnectionSnapshot {
connecting: map['connecting'] == true,
host: (map['host'] as String?) ?? '',
port: (map['port'] as num?)?.toInt() ?? 8181,
name: (map['name'] as String?) ?? '',
lastError: map['lastError'] as String?,
);
}
@@ -55,6 +59,7 @@ class ConnectionSnapshot {
final bool connecting;
final String host;
final int port;
final String name;
final String? lastError;
}
@@ -105,10 +110,11 @@ class NativeConnectionApi {
}
}
Future<void> connect(String host, int port) async {
Future<void> connect(String host, int port, {String? name}) async {
await _methodChannel.invokeMethod<void>('connect', {
'host': host,
'port': port,
if (name != null) 'name': name,
});
}
@@ -288,9 +294,15 @@ class _InputSyncPageState extends State<InputSyncPage>
scanning = false;
discoveredDevices = devices;
});
if (devices.length == 1 &&
!connection.connected &&
!connection.connecting) {
await _connectDevice(devices.single);
}
}
Future<void> _connect() async {
Future<void> _connect({String? name}) async {
final host = hostController.text.trim();
final portText = portController.text.trim();
final port = int.tryParse(portText);
@@ -307,15 +319,24 @@ class _InputSyncPageState extends State<InputSyncPage>
await prefs.setString(_portKey, port.toString());
setState(() {
connection = ConnectionSnapshot(connecting: true, host: host, port: port);
connection = ConnectionSnapshot(
connecting: true,
host: host,
port: port,
name: name ?? '',
);
});
try {
await api.connect(host, port);
await api.connect(host, port, name: name);
} on PlatformException catch (exception) {
_showToast(exception.message ?? '连接失败,请检查地址');
setState(() {
connection = ConnectionSnapshot(host: host, port: port);
connection = ConnectionSnapshot(
host: host,
port: port,
name: name ?? '',
);
headerExpanded = true;
});
}
@@ -324,7 +345,7 @@ class _InputSyncPageState extends State<InputSyncPage>
Future<void> _connectDevice(DiscoveredDevice device) async {
hostController.text = device.host;
portController.text = device.port.toString();
await _connect();
await _connect(name: device.name);
}
Future<void> _disconnect() async {
@@ -333,6 +354,7 @@ class _InputSyncPageState extends State<InputSyncPage>
connection = ConnectionSnapshot(
host: hostController.text.trim(),
port: int.tryParse(portController.text.trim()) ?? 8181,
name: connection.name,
);
headerExpanded = true;
sendButtonState = SendButtonState.idle;
@@ -542,11 +564,6 @@ class _InputSyncPageState extends State<InputSyncPage>
Widget _buildHeader(BuildContext context) {
final colorScheme = Theme.of(context).colorScheme;
final statusText = connection.connected
? '已连接: ${connection.host}:${connection.port}'
: connection.connecting
? '正在连接: ${connection.host}:${connection.port}'
: '未连接 (点击配置)';
return Material(
color: colorScheme.surface,
@@ -566,29 +583,23 @@ class _InputSyncPageState extends State<InputSyncPage>
});
},
child: SizedBox(
height: 44,
height: 52,
child: Row(
children: [
_ConnectionIndicator(
connected: connection.connected,
connecting: connection.connecting,
animation: statusPulseController,
),
const SizedBox(width: 10),
Expanded(
child: Text(
statusText,
overflow: TextOverflow.ellipsis,
style: TextStyle(
color: connection.connected
? const Color(0xFF166534)
: connection.connecting
? colorScheme.primary
: colorScheme.error,
fontWeight: FontWeight.w600,
),
child: _ConnectionSummary(
connected: connection.connected,
connecting: connection.connecting,
endpoint: connection.host.isEmpty
? '未设置地址'
: '${connection.host}:${connection.port}',
deviceName: connection.name.isEmpty
? 'Windows 桌面端'
: connection.name,
animation: statusPulseController,
),
),
const SizedBox(width: 8),
Icon(
headerExpanded
? Icons.keyboard_arrow_up
@@ -979,6 +990,105 @@ class _InputSyncPageState extends State<InputSyncPage>
}
}
class _ConnectionSummary extends StatelessWidget {
const _ConnectionSummary({
required this.connected,
required this.connecting,
required this.endpoint,
required this.deviceName,
required this.animation,
});
final bool connected;
final bool connecting;
final String endpoint;
final String deviceName;
final Animation<double> animation;
@override
Widget build(BuildContext context) {
final colorScheme = Theme.of(context).colorScheme;
final iconColor = connected
? const Color(0xFF16A34A)
: connecting
? colorScheme.primary
: colorScheme.error;
final primaryTextColor = connected
? const Color(0xFF166534)
: const Color(0xFF0F172A);
final secondaryTextColor = connected
? const Color(0xFF15803D)
: colorScheme.onSurfaceVariant;
return Row(
children: [
AnimatedBuilder(
animation: animation,
builder: (context, child) {
return Transform.scale(
scale: connecting ? animation.value : 1,
child: Container(
width: 42,
height: 42,
alignment: Alignment.center,
decoration: BoxDecoration(
color: iconColor.withValues(alpha: 0.12),
borderRadius: BorderRadius.circular(8),
),
child: Icon(
connected
? Icons.sync
: connecting
? Icons.sync
: Icons.sync_disabled,
color: iconColor,
size: 24,
),
),
);
},
),
const SizedBox(width: 10),
Expanded(
child: SizedBox(
height: 64,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
endpoint,
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: TextStyle(
color: primaryTextColor,
fontFamily: connectionInfoFontFamily,
fontSize: 21,
fontWeight: FontWeight.w700,
height: 1.15,
),
),
const SizedBox(height: 3),
Text(
deviceName,
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: TextStyle(
color: secondaryTextColor,
fontFamily: connectionInfoFontFamily,
fontSize: 18,
height: 1.15,
),
),
],
),
),
),
],
);
}
}
class _ConnectionIndicator extends StatelessWidget {
const _ConnectionIndicator({
required this.connected,