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

preview for html and csv added #175

Closed
wants to merge 5 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
9 changes: 8 additions & 1 deletion lib/consts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,8 @@ const Map<String, Map<String, List<ResponseBodyView>>>
kTypeText: {
kSubTypeDefaultViewOptions: kRawBodyViewOptions,
kSubTypeCss: kCodeRawBodyViewOptions,
kSubTypeHtml: kCodeRawBodyViewOptions,
kSubTypeHtml: kPreviewCodeRawBodyViewOptions,
kSubTypeCsv: kPreviewCodeRawBodyViewOptions,
kSubTypeJavascript: kCodeRawBodyViewOptions,
kSubTypeMarkdown: kCodeRawBodyViewOptions,
kSubTypeTextXml: kCodeRawBodyViewOptions,
Expand Down Expand Up @@ -483,9 +484,15 @@ const kImageError =
const kSvgError =
"There seems to be an issue rendering this SVG image. Please raise an issue in API Dash GitHub repo so that we can resolve it.";

const kCsvError =
"There seems to be an issue rendering this CSV image. Please raise an issue in API Dash GitHub repo so that we can resolve it.";

const kPdfError =
"There seems to be an issue rendering this pdf. Please raise an issue in API Dash GitHub repo so that we can resolve it.";

const kHtmlError =
"There seems to be an issue rendering this HTML document. Please raise an issue in API Dash GitHub repo so that we can resolve it.";

const kAudioError =
"There seems to be an issue playing this audio. Please raise an issue in API Dash GitHub repo so that we can resolve it.";

Expand Down
74 changes: 74 additions & 0 deletions lib/widgets/previewer.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import 'dart:convert';
import 'package:csv/csv.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_widget_from_html/flutter_widget_from_html.dart';
import 'package:printing/printing.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:vector_graphics_compiler/vector_graphics_compiler.dart';
Expand Down Expand Up @@ -54,6 +57,31 @@ class _PreviewerState extends State<Previewer> {
return const ErrorMessage(message: kSvgError);
}
}
if (widget.type == kTypeText && widget.subtype == kSubTypeHtml) {
try {
return SingleChildScrollView(
child: Row(
children: [
Flexible(
child: HtmlWidget(
widget.body,
),
),
],
),
);
} catch (e) {
return const ErrorMessage(message: kHtmlError);
}
}
if (widget.type == kTypeText && widget.subtype == kSubTypeCsv) {
try {
return CsvPreviewer(body: widget.body);
} catch (e) {
return const ErrorMessage(message: kCsvError);
}
}

if (widget.type == kTypeImage) {
return Image.memory(
widget.bytes,
Expand Down Expand Up @@ -90,3 +118,49 @@ class _PreviewerState extends State<Previewer> {
return ErrorMessage(message: message);
}
}

class CsvPreviewer extends StatelessWidget {
const CsvPreviewer({Key? key, required this.body}) : super(key: key);

final String body;

@override
Widget build(BuildContext context) {
try {
final List<List<dynamic>> csvData =
const CsvToListConverter().convert(body, eol: '\n');
return SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: DataTable(
columns: csvData[0]
.map(
(item) => DataColumn(
label: Text(
item.toString(),
),
),
)
.toList(),
rows: csvData
.skip(1)
.map(
(csvrow) => DataRow(
cells: csvrow
.map(
(csvItem) => DataCell(
Text(
csvItem.toString(),
),
),
)
.toList(),
),
)
.toList(),
),
);
} catch (e) {
return const ErrorMessage(message: kCsvError);
}
}
}
3 changes: 3 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,15 @@ dependencies:
url: https://github.com/foss42/json_data_explorer.git
ref: 9fa58d7b51e65174ab11cbcae17bba88a4194dde
scrollable_positioned_list: ^0.2.3

flutter_widget_from_html: ^0.14.11
file_picker: ^6.1.1
flutter_svg: ^2.0.9
vector_graphics_compiler: ^1.1.9+1
code_builder: ^4.9.0
dart_style: ^2.3.4
json_text_field: ^1.1.0
csv: ^5.1.1

dev_dependencies:
flutter_test:
Expand Down
45 changes: 45 additions & 0 deletions test/widgets/previewer_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -231,4 +231,49 @@ void main() {
await tester.pumpAndSettle();
expect(find.text(kSvgError), findsOneWidget);
});

testWidgets('Testing when type/subtype is text/csv', (tester) async {
String csvDataString =
'Id,Name,Age\n1,John Doe,40\n2,Dbestech,41\n3,Voldermort,71\n4,Joe Biden,80\n5,Ryo Hanamura,35';

await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: Previewer(
type: kTypeText,
subtype: kSubTypeCsv,
bytes: Uint8List.fromList([]),
body: csvDataString,
),
),
),
);

expect(find.byType(DataTable), findsOneWidget);
expect(find.text('John Doe'), findsOneWidget);
expect(find.text('41'), findsOneWidget);
});
testWidgets('Testing when type/subtype is text/html', (tester) async {

String htmlDataString = '<div><p>Hello, <strong>World</strong>!</p></div>';

await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: Previewer(
type: kTypeText,
subtype: kSubTypeHtml,
bytes: Uint8List.fromList([]),
body: htmlDataString,
),
),
),
);

expect(find.byType(SingleChildScrollView), findsOneWidget);
expect(find.byType(Row), findsOneWidget);

expect(find.text('Hello, '), findsOneWidget);
expect(find.text('World'), findsOneWidget);
});
}