Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Implement HTML rendering with browser interactivity using webview_flutter resolving issue #155 #482

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/consts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,7 @@ const kLabelAddFormField = "Add Form Field";
const kLabelNotSent = "Not Sent";
const kLabelResponse = "Response";
const kLabelResponseBody = "Response Body";
const kLabelPreview = "Preview";
const kTooltipClearResponse = "Clear Response";
const kHeaderRow = ["Header Name", "Header Value"];
const kLabelRequestHeaders = "Request Headers";
Expand Down
3 changes: 3 additions & 0 deletions lib/screens/history/history_widgets/his_response_pane.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ class HistoryResponsePane extends ConsumerWidget {
requestHeaders:
historyHttpResponseModel?.requestHeaders ?? {},
),
ResponsePreview(
responseModel: historyHttpResponseModel ?? {},
),
],
),
),
Expand Down
14 changes: 14 additions & 0 deletions lib/screens/home_page/editor_pane/details_card/response_pane.dart
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ class ResponseTabs extends ConsumerWidget {
children: const [
ResponseBodyTab(),
ResponseHeadersTab(),
ResponsePreviewTab(),
],
);
}
Expand Down Expand Up @@ -111,3 +112,16 @@ class ResponseHeadersTab extends ConsumerWidget {
);
}
}

class ResponsePreviewTab extends ConsumerWidget {
const ResponsePreviewTab({super.key});

@override
Widget build(BuildContext context, WidgetRef ref) {
final selectedRequestModel = ref.watch(selectedRequestModelProvider);
final responseModel = selectedRequestModel?.httpResponseModel;
return ResponsePreview(
responseModel: responseModel,
);
}
}
45 changes: 44 additions & 1 deletion lib/widgets/response_widgets.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:apidash/utils/utils.dart';
import 'package:apidash/widgets/widgets.dart';
import 'package:apidash/models/models.dart';
import 'package:apidash/consts.dart';
import 'package:webview_flutter/webview_flutter.dart';

class NotSentWidget extends StatelessWidget {
const NotSentWidget({super.key});
Expand Down Expand Up @@ -193,7 +194,7 @@ class _ResponseTabViewState extends State<ResponseTabView>
void initState() {
super.initState();
_controller = TabController(
length: 2,
length: 3,
animationDuration: kTabAnimationDuration,
vsync: this,
);
Expand All @@ -216,6 +217,9 @@ class _ResponseTabViewState extends State<ResponseTabView>
TabLabel(
text: kLabelHeaders,
),
TabLabel(
text: kLabelPreview,
),
],
),
Expanded(
Expand Down Expand Up @@ -518,3 +522,42 @@ class _BodySuccessState extends State<BodySuccess> {
);
}
}

class ResponsePreview extends StatelessWidget {
const ResponsePreview({super.key, required this.responseModel});
final responseModel;

@override
Widget build(BuildContext context) {
if (responseModel == {} ||
responseModel == null ||
responseModel.body == null) {
return const ErrorMessage(
message: '$kNullResponseModelError $kUnexpectedRaiseIssue');
}

final controller = WebViewController()
..setJavaScriptMode(JavaScriptMode.unrestricted)
..setNavigationDelegate(
NavigationDelegate(
onWebResourceError: (WebResourceError error) {
ErrorMessage(message: error.description);
},
),
);
controller.setNavigationDelegate(
NavigationDelegate(
onWebResourceError: (WebResourceError error) {
ErrorMessage(message: error.description);
},
),
);
WidgetsBinding.instance.addPostFrameCallback((_) {
controller.loadHtmlString(responseModel!.body!);
});

return Scaffold(
body: WebViewWidget(controller: controller),
);
}
}
34 changes: 33 additions & 1 deletion pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1700,6 +1700,38 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.2.1"
webview_flutter:
dependency: "direct main"
description:
name: webview_flutter
sha256: "889a0a678e7c793c308c68739996227c9661590605e70b1f6cf6b9a6634f7aec"
url: "https://pub.dev"
source: hosted
version: "4.10.0"
webview_flutter_android:
dependency: transitive
description:
name: webview_flutter_android
sha256: "74693a212d990b32e0b7055d27db973a18abf31c53942063948cdfaaef9787ba"
url: "https://pub.dev"
source: hosted
version: "4.0.0"
webview_flutter_platform_interface:
dependency: transitive
description:
name: webview_flutter_platform_interface
sha256: d937581d6e558908d7ae3dc1989c4f87b786891ab47bb9df7de548a151779d8d
url: "https://pub.dev"
source: hosted
version: "2.10.0"
webview_flutter_wkwebview:
dependency: transitive
description:
name: webview_flutter_wkwebview
sha256: d4034901d96357beb1b6717ebf7d583c88e40cfc6eb85fe76dd1bf0979a9f251
url: "https://pub.dev"
source: hosted
version: "3.16.0"
win32:
dependency: transitive
description:
Expand Down Expand Up @@ -1750,5 +1782,5 @@ packages:
source: hosted
version: "3.1.2"
sdks:
dart: ">=3.5.0-259.0.dev <3.999.0"
dart: ">=3.5.0 <3.999.0"
flutter: ">=3.24.2"
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ dependencies:
url: https://github.com/google/flutter-desktop-embedding.git
path: plugins/window_size
xml: ^6.3.0
webview_flutter: ^4.10.0

dependency_overrides:
extended_text_field: ^16.0.0
Expand Down
3 changes: 2 additions & 1 deletion test/widgets/response_widgets_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ void main() {
theme: kThemeDataLight,
home: const Scaffold(
body: ResponseTabView(
selectedId: '1', children: [Text('first'), Text('second')]),
selectedId: '1',
children: [Text('first'), Text('second'), Text('third')]),
),
),
);
Expand Down