mirror of
https://gitlab.com/veilid/veilid.git
synced 2024-11-22 00:47:28 -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:loggy/loggy.dart';
|
||||||
import 'package:veilid/veilid.dart';
|
import 'package:veilid/veilid.dart';
|
||||||
|
|
||||||
import 'history_wrapper.dart';
|
import 'history_text_editing_controller.dart';
|
||||||
import 'log.dart';
|
import 'log.dart';
|
||||||
import 'log_terminal.dart';
|
import 'log_terminal.dart';
|
||||||
import 'veilid_theme.dart';
|
import 'veilid_theme.dart';
|
||||||
@ -24,13 +24,13 @@ class _MyAppState extends State<MyApp> with UiLoggy {
|
|||||||
bool _startedUp = false;
|
bool _startedUp = false;
|
||||||
Stream<VeilidUpdate>? _updateStream;
|
Stream<VeilidUpdate>? _updateStream;
|
||||||
Future<void>? _updateProcessor;
|
Future<void>? _updateProcessor;
|
||||||
final _debugHistoryWrapper = HistoryWrapper();
|
late final HistoryTextEditingController _historyController;
|
||||||
String? _errorText;
|
String? _errorText;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
|
_historyController = HistoryTextEditingController(setState: setState);
|
||||||
unawaited(initPlatformState());
|
unawaited(initPlatformState());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -177,10 +177,10 @@ class _MyAppState extends State<MyApp> with UiLoggy {
|
|||||||
padding: const EdgeInsets.all(5),
|
padding: const EdgeInsets.all(5),
|
||||||
child: Row(children: [
|
child: Row(children: [
|
||||||
Expanded(
|
Expanded(
|
||||||
child: pad(_debugHistoryWrapper.wrap(
|
child: pad(
|
||||||
setState,
|
|
||||||
TextField(
|
TextField(
|
||||||
controller: _debugHistoryWrapper.controller,
|
controller: _historyController.controller,
|
||||||
|
focusNode: _historyController.focusNode,
|
||||||
decoration: newInputDecoration(
|
decoration: newInputDecoration(
|
||||||
'Debug Command', _errorText, _startedUp),
|
'Debug Command', _errorText, _startedUp),
|
||||||
textInputAction: TextInputAction.unspecified,
|
textInputAction: TextInputAction.unspecified,
|
||||||
@ -197,16 +197,14 @@ class _MyAppState extends State<MyApp> with UiLoggy {
|
|||||||
}
|
}
|
||||||
final res = await Veilid.instance.debug(v);
|
final res = await Veilid.instance.debug(v);
|
||||||
loggy.info(res);
|
loggy.info(res);
|
||||||
setState(() {
|
_historyController.submit(v);
|
||||||
_debugHistoryWrapper.submit(v);
|
|
||||||
});
|
|
||||||
} on VeilidAPIException catch (e) {
|
} on VeilidAPIException catch (e) {
|
||||||
setState(() {
|
setState(() {
|
||||||
_errorText = e.toDisplayError();
|
_errorText = e.toDisplayError();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
))),
|
)),
|
||||||
pad(
|
pad(
|
||||||
Column(children: [
|
Column(children: [
|
||||||
const Text('Startup'),
|
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