mirror of
https://github.com/foss42/apidash.git
synced 2025-05-28 20:27:04 +08:00
add feature 22
This commit is contained in:
@ -2,6 +2,7 @@ import 'dart:math' as math;
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:apidash/consts.dart';
|
||||
import 'package:json_text_field/json_text_field.dart';
|
||||
|
||||
class TextFieldEditor extends StatefulWidget {
|
||||
const TextFieldEditor({
|
||||
@ -9,21 +10,23 @@ class TextFieldEditor extends StatefulWidget {
|
||||
required this.fieldKey,
|
||||
this.onChanged,
|
||||
this.initialValue,
|
||||
this.contentType,
|
||||
});
|
||||
|
||||
final String fieldKey;
|
||||
final Function(String)? onChanged;
|
||||
final String? initialValue;
|
||||
final ContentType? contentType;
|
||||
@override
|
||||
State<TextFieldEditor> createState() => _TextFieldEditorState();
|
||||
}
|
||||
|
||||
class _TextFieldEditorState extends State<TextFieldEditor> {
|
||||
final TextEditingController controller = TextEditingController();
|
||||
final JsonTextFieldController controller = JsonTextFieldController();
|
||||
late final FocusNode editorFocusNode;
|
||||
|
||||
void insertTab() {
|
||||
String sp = " ";
|
||||
String sp = " ";
|
||||
int offset = math.min(
|
||||
controller.selection.baseOffset, controller.selection.extentOffset);
|
||||
String text = controller.text.substring(0, offset) +
|
||||
@ -42,6 +45,9 @@ class _TextFieldEditorState extends State<TextFieldEditor> {
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
if (widget.contentType == ContentType.json) {
|
||||
controller.formatJson(sortJson: false);
|
||||
}
|
||||
editorFocusNode = FocusNode(debugLabel: "Editor Focus Node");
|
||||
}
|
||||
|
||||
@ -62,7 +68,22 @@ class _TextFieldEditorState extends State<TextFieldEditor> {
|
||||
insertTab();
|
||||
},
|
||||
},
|
||||
child: TextFormField(
|
||||
child: JsonTextField(
|
||||
stringHighlightStyle: kCodeStyle.copyWith(
|
||||
color: Theme.of(context).colorScheme.secondary,
|
||||
),
|
||||
keyHighlightStyle: kCodeStyle.copyWith(
|
||||
color: Theme.of(context).colorScheme.primary,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
errorContainerDecoration: BoxDecoration(
|
||||
color: Theme.of(context).colorScheme.error.withOpacity(
|
||||
kForegroundOpacity,
|
||||
),
|
||||
borderRadius: kBorderRadius8,
|
||||
),
|
||||
showErrorMessage: true,
|
||||
isFormatting: widget.contentType == ContentType.json,
|
||||
key: Key(widget.fieldKey),
|
||||
controller: controller,
|
||||
focusNode: editorFocusNode,
|
||||
@ -71,7 +92,12 @@ class _TextFieldEditorState extends State<TextFieldEditor> {
|
||||
maxLines: null,
|
||||
style: kCodeStyle,
|
||||
textAlignVertical: TextAlignVertical.top,
|
||||
onChanged: widget.onChanged,
|
||||
onChanged: (value) {
|
||||
widget.onChanged?.call(value);
|
||||
if (widget.contentType == ContentType.json) {
|
||||
controller.formatJson(sortJson: false);
|
||||
}
|
||||
},
|
||||
decoration: InputDecoration(
|
||||
hintText: "Enter content (body)",
|
||||
hintStyle: TextStyle(
|
||||
|
Reference in New Issue
Block a user