mirror of
https://gitlab.com/veilid/veilid.git
synced 2024-11-21 16:37:39 -06:00
history controller fix for example code
This commit is contained in:
parent
0d95aa0859
commit
9b6a30acbf
@ -6,7 +6,7 @@ import 'package:flutter/material.dart';
|
||||
import 'package:loggy/loggy.dart';
|
||||
import 'package:veilid/veilid.dart';
|
||||
|
||||
import 'history_wrapper.dart';
|
||||
import 'history_text_editing_controller.dart';
|
||||
import 'log.dart';
|
||||
import 'log_terminal.dart';
|
||||
import 'veilid_theme.dart';
|
||||
@ -24,13 +24,13 @@ class _MyAppState extends State<MyApp> with UiLoggy {
|
||||
bool _startedUp = false;
|
||||
Stream<VeilidUpdate>? _updateStream;
|
||||
Future<void>? _updateProcessor;
|
||||
final _debugHistoryWrapper = HistoryWrapper();
|
||||
late final HistoryTextEditingController _historyController;
|
||||
String? _errorText;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
|
||||
_historyController = HistoryTextEditingController(setState: setState);
|
||||
unawaited(initPlatformState());
|
||||
}
|
||||
|
||||
@ -177,10 +177,10 @@ class _MyAppState extends State<MyApp> with UiLoggy {
|
||||
padding: const EdgeInsets.all(5),
|
||||
child: Row(children: [
|
||||
Expanded(
|
||||
child: pad(_debugHistoryWrapper.wrap(
|
||||
setState,
|
||||
child: pad(
|
||||
TextField(
|
||||
controller: _debugHistoryWrapper.controller,
|
||||
controller: _historyController.controller,
|
||||
focusNode: _historyController.focusNode,
|
||||
decoration: newInputDecoration(
|
||||
'Debug Command', _errorText, _startedUp),
|
||||
textInputAction: TextInputAction.unspecified,
|
||||
@ -197,16 +197,14 @@ class _MyAppState extends State<MyApp> with UiLoggy {
|
||||
}
|
||||
final res = await Veilid.instance.debug(v);
|
||||
loggy.info(res);
|
||||
setState(() {
|
||||
_debugHistoryWrapper.submit(v);
|
||||
});
|
||||
_historyController.submit(v);
|
||||
} on VeilidAPIException catch (e) {
|
||||
setState(() {
|
||||
_errorText = e.toDisplayError();
|
||||
});
|
||||
}
|
||||
}),
|
||||
))),
|
||||
)),
|
||||
pad(
|
||||
Column(children: [
|
||||
const Text('Startup'),
|
||||
|
@ -1,66 +0,0 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
|
||||
// TextField History Wrapper
|
||||
class HistoryWrapper {
|
||||
final List<String> _history = [];
|
||||
int _historyPosition = 0;
|
||||
final _historyTextEditingController = TextEditingController();
|
||||
String _historyCurrentEdit = '';
|
||||
|
||||
TextEditingController get controller => _historyTextEditingController;
|
||||
|
||||
void submit(String v) {
|
||||
// add to history
|
||||
if (_history.isEmpty || _history.last != v) {
|
||||
_history.add(v);
|
||||
if (_history.length > 100) {
|
||||
_history.removeAt(0);
|
||||
}
|
||||
}
|
||||
_historyPosition = _history.length;
|
||||
_historyTextEditingController.text = '';
|
||||
}
|
||||
|
||||
Widget wrap(
|
||||
void Function(void Function())? stateSetter, TextField textField) {
|
||||
final setState = stateSetter ?? (x) => x();
|
||||
return KeyboardListener(
|
||||
onKeyEvent: (event) {
|
||||
setState(() {
|
||||
if (event.runtimeType == KeyDownEvent &&
|
||||
event.logicalKey == LogicalKeyboardKey.arrowUp) {
|
||||
if (_historyPosition > 0) {
|
||||
if (_historyPosition == _history.length) {
|
||||
_historyCurrentEdit = _historyTextEditingController.text;
|
||||
}
|
||||
_historyPosition -= 1;
|
||||
_historyTextEditingController.text = _history[_historyPosition];
|
||||
}
|
||||
} else if (event.runtimeType == KeyDownEvent &&
|
||||
event.logicalKey == LogicalKeyboardKey.arrowDown) {
|
||||
if (_historyPosition < _history.length) {
|
||||
_historyPosition += 1;
|
||||
if (_historyPosition == _history.length) {
|
||||
_historyTextEditingController.text = _historyCurrentEdit;
|
||||
} else {
|
||||
_historyTextEditingController.text = _history[_historyPosition];
|
||||
}
|
||||
}
|
||||
} else if (event.runtimeType == KeyDownEvent) {
|
||||
_historyPosition = _history.length;
|
||||
_historyCurrentEdit = _historyTextEditingController.text;
|
||||
}
|
||||
});
|
||||
},
|
||||
focusNode: FocusNode(onKeyEvent: (node, event) {
|
||||
if (event.logicalKey == LogicalKeyboardKey.arrowDown ||
|
||||
event.logicalKey == LogicalKeyboardKey.arrowUp) {
|
||||
return KeyEventResult.handled;
|
||||
}
|
||||
return KeyEventResult.ignored;
|
||||
}),
|
||||
child: textField,
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user