fix: ssh tab multi page FocusNode

This commit is contained in:
lollipopkit
2024-05-22 19:17:46 +08:00
parent 2c5886de09
commit 9304a0377b
2 changed files with 11 additions and 4 deletions

View File

@@ -5,6 +5,7 @@ import 'package:dartssh2/dartssh2.dart';
import 'package:fl_lib/fl_lib.dart'; import 'package:fl_lib/fl_lib.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_background_service/flutter_background_service.dart'; import 'package:flutter_background_service/flutter_background_service.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
import 'package:toolbox/core/extension/context/locale.dart'; import 'package:toolbox/core/extension/context/locale.dart';
@@ -30,6 +31,7 @@ class SSHPage extends StatefulWidget {
final String? initCmd; final String? initCmd;
final bool notFromTab; final bool notFromTab;
final Function()? onSessionEnd; final Function()? onSessionEnd;
final FocusNode? focus;
const SSHPage({ const SSHPage({
super.key, super.key,
@@ -37,6 +39,7 @@ class SSHPage extends StatefulWidget {
this.initCmd, this.initCmd,
this.notFromTab = true, this.notFromTab = true,
this.onSessionEnd, this.onSessionEnd,
this.focus,
}); });
@override @override
@@ -50,7 +53,7 @@ class _SSHPageState extends State<SSHPage> with AutomaticKeepAliveClientMixin {
late final _terminal = Terminal(inputHandler: _keyboard); late final _terminal = Terminal(inputHandler: _keyboard);
final TerminalController _terminalController = TerminalController(); final TerminalController _terminalController = TerminalController();
final List<List<VirtKey>> _virtKeysList = []; final List<List<VirtKey>> _virtKeysList = [];
final _focus = FocusNode(); late final _focus = widget.focus ?? FocusNode();
late MediaQueryData _media; late MediaQueryData _media;
late TerminalStyle _terminalStyle; late TerminalStyle _terminalStyle;

View File

@@ -25,6 +25,7 @@ class _SSHTabPageState extends State<SSHTabPage>
vsync: this, vsync: this,
); );
final _fabRN = ValueNotifier(0); final _fabRN = ValueNotifier(0);
final _focusMap = <String, FocusNode>{};
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
@@ -36,7 +37,10 @@ class _SSHTabPageState extends State<SSHTabPage>
isScrollable: true, isScrollable: true,
tabAlignment: TabAlignment.start, tabAlignment: TabAlignment.start,
dividerColor: Colors.transparent, dividerColor: Colors.transparent,
onTap: (value) => _fabRN.value = value, onTap: (value) {
_fabRN.value = value;
_focusMap[_tabIds.keys.elementAt(value)]?.requestFocus();
},
), ),
body: _buildBody(), body: _buildBody(),
floatingActionButton: ListenableBuilder( floatingActionButton: ListenableBuilder(
@@ -138,10 +142,10 @@ class _SSHTabPageState extends State<SSHTabPage>
} }
return spi.name; return spi.name;
}(); }();
final key = GlobalKey<State<SSHPage>>(debugLabel: 'sshTabPage_$name'); final focus = _focusMap.putIfAbsent(name, () => FocusNode());
_tabIds[name] = SSHPage( _tabIds[name] = SSHPage(
key: key,
spi: spi, spi: spi,
focus: focus,
notFromTab: false, notFromTab: false,
onSessionEnd: () { onSessionEnd: () {
// debugPrint("Session done received on page whose tabId = $name"); // debugPrint("Session done received on page whose tabId = $name");