Skip to content

Commit

Permalink
feat: Implement HTML rendering with browser interactivity using webvi…
Browse files Browse the repository at this point in the history
…ew_flutter: foss42#155:
  • Loading branch information
chetanr250 committed Oct 15, 2024
1 parent 04b486d commit f4106e5
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 2 deletions.
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
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,
);
}
}
33 changes: 32 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,30 @@ class _BodySuccessState extends State<BodySuccess> {
);
}
}

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

@override
Widget build(BuildContext context) {
final controller = WebViewController()
..setJavaScriptMode(JavaScriptMode.unrestricted)
..setNavigationDelegate(
NavigationDelegate(
onWebResourceError: (WebResourceError error) {
ErrorMessage(message: error.description);
},
),
);

// Load HTML content
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

0 comments on commit f4106e5

Please sign in to comment.