Skip to content

Instantly share code, notes, and snippets.

@PaulEibensteiner
Last active April 11, 2022 18:51
Show Gist options
  • Save PaulEibensteiner/d934f1751ab0c4ba2ee0a31b1c9c69e5 to your computer and use it in GitHub Desktop.
Save PaulEibensteiner/d934f1751ab0c4ba2ee0a31b1c9c69e5 to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main() async {
return runApp(MaterialApp(
theme: ThemeData(brightness: Brightness.light, primaryColor: Colors.red),
home: const MyEditor()
// debugShowCheckedModeBanner: false, theme: themeData, home: Document()
));
}
class MyEditor extends StatefulWidget {
const MyEditor({Key? key}) : super(key: key);
@override
_MyEditorController createState() => _MyEditorController();
}
class _MyEditorController extends State<MyEditor>
implements DeltaTextInputClient {
late FocusNode _focusNode;
late TextInputConnection _inputConnection;
// same constructor as in TexEditingValue
TextSelection _selection = const TextSelection.collapsed(offset: -1);
final _currentDoc = ValueNotifier("Initial Value");
static const TextInputConfiguration mainTextInputConfiguration =
TextInputConfiguration(
inputType: TextInputType.multiline,
readOnly: false,
obscureText: false,
autocorrect: false,
// SmartDashesType?
// SmartQuotesType?
enableSuggestions: false,
actionLabel: null,
inputAction: TextInputAction.newline,
keyboardAppearance: Brightness.light,
textCapitalization: TextCapitalization.none,
autofillConfiguration: AutofillConfiguration.disabled,
enableIMEPersonalizedLearning: true,
enableDeltaModel: true,
);
@override
TextEditingValue get currentTextEditingValue =>
TextEditingValue(text: _currentDoc.value, selection: _selection);
@override
void updateEditingValue(TextEditingValue value) {
print("Received: ${value.toString()}");
_currentDoc.value = value.text;
_selection = value.selection;
}
@override
void updateEditingValueWithDeltas(List<TextEditingDelta> textEditingDeltas) {
var editingValue = currentTextEditingValue;
for (var delta in textEditingDeltas) {
print("Received delta: ${delta.toString()}");
delta.apply(editingValue);
}
_currentDoc.value = editingValue.text;
_selection = editingValue.selection;
}
@override
void initState() {
super.initState();
_focusNode = FocusNode();
_attachToInput();
}
void _attachToInput() {
_inputConnection = TextInput.attach(this, mainTextInputConfiguration);
_inputConnection.show();
}
@override
Widget build(BuildContext contex) {
if (!_focusNode.hasFocus) {
_focusNode.requestFocus();
}
return Focus(
focusNode: _focusNode,
autofocus: true,
child: ValueListenableBuilder(
valueListenable: _currentDoc,
builder: (BuildContext context, String doc, Widget? child) {
return Text(doc);
}));
}
// UNIMPLEMENTED METHODS
@override
void connectionClosed() => throw UnimplementedError();
@override
AutofillScope? get currentAutofillScope => throw UnimplementedError();
@override
void performPrivateCommand(String action, Map<String, dynamic> data) {}
@override
void showAutocorrectionPromptRect(int start, int end) =>
throw UnimplementedError();
@override
void updateFloatingCursor(RawFloatingCursorPoint point) =>
throw UnimplementedError();
@override
void performAction(TextInputAction action) => throw UnimplementedError();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment