Skip to content

Commit 0ec6f3a

Browse files
committed
✨ added print option. #22
1 parent b083410 commit 0ec6f3a

File tree

12 files changed

+542
-197
lines changed

12 files changed

+542
-197
lines changed

lib/home.dart

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,20 @@ import 'package:file_picker/file_picker.dart';
66
import 'package:flutter/foundation.dart';
77
import 'package:flutter/material.dart';
88
import 'package:flutter/services.dart';
9+
import 'package:htmltopdfwidgets/htmltopdfwidgets.dart' as html2pdf;
10+
import 'package:markdown/markdown.dart' as md;
911
import 'package:markdown_editor/device_preference_notifier.dart';
1012
import 'package:markdown_editor/l10n/generated/app_localizations.dart';
1113
import 'package:markdown_editor/widgets/MarkdownBody/custom_image_config.dart';
1214
import 'package:markdown_editor/widgets/MarkdownBody/custom_text_node.dart';
1315
import 'package:markdown_editor/widgets/MarkdownBody/latex_node.dart';
1416
import 'package:markdown_editor/widgets/MarkdownTextInput/markdown_text_input.dart';
1517
import 'package:markdown_widget/markdown_widget.dart';
18+
import 'package:pdf/widgets.dart' as pw;
19+
import 'package:printing/printing.dart';
1620
import 'package:url_launcher/url_launcher.dart';
1721

18-
enum MenuItem { switchTheme, switchView, open, clear, save, donate }
22+
enum MenuItem { switchTheme, switchView, open, clear, save, print, donate }
1923

2024
class Home extends StatefulWidget {
2125
final DevicePreferenceNotifier devicePreferenceNotifier;
@@ -206,6 +210,28 @@ class _HomeState extends State<Home> {
206210
}
207211
}
208212

213+
Future<void> _printFile() async {
214+
if (_inputText.isEmpty) {
215+
ScaffoldMessenger.of(context).showSnackBar(
216+
SnackBar(
217+
content: Text(AppLocalizations.of(context)!.emptyInputTextContent),
218+
),
219+
);
220+
return;
221+
} else {
222+
final htmlFromMarkdown = md.markdownToHtml(_inputText);
223+
await Printing.layoutPdf(
224+
usePrinterSettings: true,
225+
onLayout: (format) async {
226+
final pdf = pw.Document();
227+
final widgets = await html2pdf.HTMLToPdf().convert(htmlFromMarkdown);
228+
pdf.addPage(pw.MultiPage(build: (context) => widgets));
229+
return pdf.save();
230+
},
231+
);
232+
}
233+
}
234+
209235
Future<bool> _showExitConfirmationDialog() async {
210236
return await showDialog<bool>(
211237
context: context,
@@ -366,6 +392,9 @@ class _HomeState extends State<Home> {
366392
case MenuItem.save:
367393
await _saveFile();
368394
break;
395+
case MenuItem.print:
396+
await _printFile();
397+
break;
369398
case MenuItem.donate:
370399
await launchUrl(
371400
Uri.parse("https://buymeacoffee.com/adeeteya"),
@@ -429,6 +458,16 @@ class _HomeState extends State<Home> {
429458
],
430459
),
431460
),
461+
PopupMenuItem(
462+
value: MenuItem.print,
463+
child: Row(
464+
children: [
465+
const Icon(Icons.print),
466+
const SizedBox(width: 8),
467+
Text(AppLocalizations.of(context)!.print),
468+
],
469+
),
470+
),
432471
PopupMenuItem(
433472
value: MenuItem.donate,
434473
child: Row(

lib/l10n/app_en.arb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,5 +134,9 @@
134134
"donate": "Donate",
135135
"@donate": {
136136
"description": "Menu Item for Donate"
137+
},
138+
"print": "Print",
139+
"@print": {
140+
"description": "Menu Item for Print"
137141
}
138142
}

linux/flutter/generated_plugin_registrant.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@
66

77
#include "generated_plugin_registrant.h"
88

9+
#include <printing/printing_plugin.h>
910
#include <url_launcher_linux/url_launcher_plugin.h>
1011

1112
void fl_register_plugins(FlPluginRegistry* registry) {
13+
g_autoptr(FlPluginRegistrar) printing_registrar =
14+
fl_plugin_registry_get_registrar_for_plugin(registry, "PrintingPlugin");
15+
printing_plugin_register_with_registrar(printing_registrar);
1216
g_autoptr(FlPluginRegistrar) url_launcher_linux_registrar =
1317
fl_plugin_registry_get_registrar_for_plugin(registry, "UrlLauncherPlugin");
1418
url_launcher_plugin_register_with_registrar(url_launcher_linux_registrar);

linux/flutter/generated_plugins.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#
44

55
list(APPEND FLUTTER_PLUGIN_LIST
6+
printing
67
url_launcher_linux
78
)
89

macos/Flutter/GeneratedPluginRegistrant.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ import FlutterMacOS
66
import Foundation
77

88
import file_picker
9+
import printing
910
import shared_preferences_foundation
1011
import url_launcher_macos
1112

1213
func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
1314
FilePickerPlugin.register(with: registry.registrar(forPlugin: "FilePickerPlugin"))
15+
PrintingPlugin.register(with: registry.registrar(forPlugin: "PrintingPlugin"))
1416
SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin"))
1517
UrlLauncherPlugin.register(with: registry.registrar(forPlugin: "UrlLauncherPlugin"))
1618
}

macos/Runner/DebugProfile.entitlements

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,7 @@
88
<true/>
99
<key>com.apple.security.network.server</key>
1010
<true/>
11+
<key>com.apple.security.print</key>
12+
<true/>
1113
</dict>
1214
</plist>

macos/Runner/Release.entitlements

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,7 @@
44
<dict>
55
<key>com.apple.security.app-sandbox</key>
66
<true/>
7+
<key>com.apple.security.print</key>
8+
<true/>
79
</dict>
810
</plist>

pubspec.lock

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
# Generated by pub
22
# See https://dart.dev/tools/pub/glossary#lockfile
33
packages:
4+
archive:
5+
dependency: transitive
6+
description:
7+
name: archive
8+
sha256: "2fde1607386ab523f7a36bb3e7edb43bd58e6edaf2ffb29d8a6d578b297fdbbd"
9+
url: "https://pub.dev"
10+
source: hosted
11+
version: "4.0.7"
412
args:
513
dependency: transitive
614
description:
@@ -17,6 +25,22 @@ packages:
1725
url: "https://pub.dev"
1826
source: hosted
1927
version: "2.13.0"
28+
barcode:
29+
dependency: transitive
30+
description:
31+
name: barcode
32+
sha256: "7b6729c37e3b7f34233e2318d866e8c48ddb46c1f7ad01ff7bb2a8de1da2b9f4"
33+
url: "https://pub.dev"
34+
source: hosted
35+
version: "2.2.9"
36+
bidi:
37+
dependency: transitive
38+
description:
39+
name: bidi
40+
sha256: "77f475165e94b261745cf1032c751e2032b8ed92ccb2bf5716036db79320637d"
41+
url: "https://pub.dev"
42+
source: hosted
43+
version: "2.0.13"
2044
boolean_selector:
2145
dependency: transitive
2246
description:
@@ -57,6 +81,14 @@ packages:
5781
url: "https://pub.dev"
5882
source: hosted
5983
version: "0.3.4+2"
84+
crypto:
85+
dependency: transitive
86+
description:
87+
name: crypto
88+
sha256: "1e445881f28f22d6140f181e07737b22f1e099a5e1ff94b0af2f9e4a463f4855"
89+
url: "https://pub.dev"
90+
source: hosted
91+
version: "3.0.6"
6092
csslib:
6193
dependency: transitive
6294
description:
@@ -197,6 +229,14 @@ packages:
197229
url: "https://pub.dev"
198230
source: hosted
199231
version: "0.15.6"
232+
htmltopdfwidgets:
233+
dependency: "direct main"
234+
description:
235+
name: htmltopdfwidgets
236+
sha256: e2c42dc784fa884eee0f29bbced6555a1a4cb0a3171c7d334cb5ca64a3854d77
237+
url: "https://pub.dev"
238+
source: hosted
239+
version: "1.1.3"
200240
http:
201241
dependency: transitive
202242
description:
@@ -213,6 +253,14 @@ packages:
213253
url: "https://pub.dev"
214254
source: hosted
215255
version: "4.1.2"
256+
image:
257+
dependency: transitive
258+
description:
259+
name: image
260+
sha256: "4e973fcf4caae1a4be2fa0a13157aa38a8f9cb049db6529aa00b4d71abc4d928"
261+
url: "https://pub.dev"
262+
source: hosted
263+
version: "4.5.4"
216264
intl:
217265
dependency: "direct main"
218266
description:
@@ -349,6 +397,22 @@ packages:
349397
url: "https://pub.dev"
350398
source: hosted
351399
version: "2.3.0"
400+
pdf:
401+
dependency: "direct main"
402+
description:
403+
name: pdf
404+
sha256: "28eacad99bffcce2e05bba24e50153890ad0255294f4dd78a17075a2ba5c8416"
405+
url: "https://pub.dev"
406+
source: hosted
407+
version: "3.11.3"
408+
pdf_widget_wrapper:
409+
dependency: transitive
410+
description:
411+
name: pdf_widget_wrapper
412+
sha256: c930860d987213a3d58c7ec3b7ecf8085c3897f773e8dc23da9cae60a5d6d0f5
413+
url: "https://pub.dev"
414+
source: hosted
415+
version: "1.0.4"
352416
permission_handler:
353417
dependency: "direct main"
354418
description:
@@ -421,6 +485,22 @@ packages:
421485
url: "https://pub.dev"
422486
source: hosted
423487
version: "2.1.8"
488+
posix:
489+
dependency: transitive
490+
description:
491+
name: posix
492+
sha256: "6323a5b0fa688b6a010df4905a56b00181479e6d10534cecfecede2aa55add61"
493+
url: "https://pub.dev"
494+
source: hosted
495+
version: "6.0.3"
496+
printing:
497+
dependency: "direct main"
498+
description:
499+
name: printing
500+
sha256: "482cd5a5196008f984bb43ed0e47cbfdca7373490b62f3b27b3299275bf22a93"
501+
url: "https://pub.dev"
502+
source: hosted
503+
version: "5.14.2"
424504
provider:
425505
dependency: transitive
426506
description:
@@ -429,6 +509,14 @@ packages:
429509
url: "https://pub.dev"
430510
source: hosted
431511
version: "6.1.5+1"
512+
qr:
513+
dependency: transitive
514+
description:
515+
name: qr
516+
sha256: "5a1d2586170e172b8a8c8470bbbffd5eb0cd38a66c0d77155ea138d3af3a4445"
517+
url: "https://pub.dev"
518+
source: hosted
519+
version: "3.0.2"
432520
scroll_to_index:
433521
dependency: transitive
434522
description:

pubspec.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,13 @@ dependencies:
2020
flutter_svg: ^2.2.1
2121
flutter_widget_from_html_core: ^0.17.0
2222
html: ^0.15.6
23+
htmltopdfwidgets: ^1.1.3
2324
intl: any
2425
markdown: ^7.3.0
2526
markdown_widget: ^2.3.2+8
27+
pdf: ^3.11.3
2628
permission_handler: ^12.0.1
29+
printing: ^5.14.2
2730
shared_preferences: ^2.5.3
2831
url_launcher: ^6.3.2
2932

0 commit comments

Comments
 (0)