From aabed77f768a26c5973dab8dc67d9bccb63c33d2 Mon Sep 17 00:00:00 2001 From: Antonio Jesus Caballero Encinas Date: Thu, 21 Dec 2023 11:55:24 +0100 Subject: [PATCH] add feature 22 --- .../request_pane/request_body.dart | 3 ++ lib/widgets/editor.dart | 34 ++++++++++++++++--- pubspec.lock | 28 +++++++++++++-- pubspec.yaml | 4 ++- 4 files changed, 62 insertions(+), 7 deletions(-) diff --git a/lib/screens/home_page/editor_pane/details_card/request_pane/request_body.dart b/lib/screens/home_page/editor_pane/details_card/request_pane/request_body.dart index 07c5bf53..53782f18 100644 --- a/lib/screens/home_page/editor_pane/details_card/request_pane/request_body.dart +++ b/lib/screens/home_page/editor_pane/details_card/request_pane/request_body.dart @@ -18,6 +18,8 @@ class _EditRequestBodyState extends ConsumerState { final requestModel = ref .read(collectionStateNotifierProvider.notifier) .getRequestModel(activeId!); + final contentType = ref.watch(activeRequestModelProvider + .select((value) => value?.requestBodyContentType)); return Container( decoration: BoxDecoration( color: Theme.of(context).colorScheme.background, @@ -39,6 +41,7 @@ class _EditRequestBodyState extends ConsumerState { ), Expanded( child: TextFieldEditor( + contentType: contentType, key: Key("$activeId-body"), fieldKey: "$activeId-body-editor", initialValue: requestModel?.requestBody, diff --git a/lib/widgets/editor.dart b/lib/widgets/editor.dart index 10983528..38d2f21b 100644 --- a/lib/widgets/editor.dart +++ b/lib/widgets/editor.dart @@ -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 createState() => _TextFieldEditorState(); } class _TextFieldEditorState extends State { - 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 { @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 { 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 { 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( diff --git a/pubspec.lock b/pubspec.lock index 88c0a2a2..5c3ddfc8 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -241,6 +241,22 @@ packages: url: "https://pub.dev" source: hosted version: "1.0.1" + extended_text_field: + dependency: transitive + description: + name: extended_text_field + sha256: ed9655c70a47a54c7cc689cf7f89a2bde9ab7b530150b4d1808b7aa7eb8cdf90 + url: "https://pub.dev" + source: hosted + version: "13.0.0" + extended_text_library: + dependency: transitive + description: + name: extended_text_library + sha256: "55d09098ec56fab0d9a8a68950ca0bbf2efa1327937f7cec6af6dfa066234829" + url: "https://pub.dev" + source: hosted + version: "12.0.0" fake_async: dependency: transitive description: @@ -537,6 +553,14 @@ packages: url: "https://pub.dev" source: hosted version: "6.7.1" + json_text_field: + dependency: "direct main" + description: + name: json_text_field + sha256: caec2d687221746f81503a0cfad263d349ecb3d538dd518b820beafc32f1dfce + url: "https://pub.dev" + source: hosted + version: "1.0.0" just_audio: dependency: "direct main" description: @@ -1248,5 +1272,5 @@ packages: source: hosted version: "3.1.2" sdks: - dart: ">=3.2.0-194.0.dev <4.0.0" - flutter: ">=3.13.0" + dart: ">=3.2.0 <4.0.0" + flutter: ">=3.16.0" diff --git a/pubspec.yaml b/pubspec.yaml index 96ab5fbf..0adf1380 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -10,6 +10,7 @@ environment: dependencies: flutter: sdk: flutter + multi_split_view: ^2.4.0 url_launcher: ^6.1.12 flutter_riverpod: ^2.3.7 @@ -46,8 +47,9 @@ dependencies: json_data_explorer: git: url: https://github.com/foss42/json_data_explorer.git - version: ^0.1.1 + version: ^0.1.1 scrollable_positioned_list: ^0.2.3 + json_text_field: ^1.0.0 dev_dependencies: flutter_test: