@@ -99,8 +99,12 @@ class _HomePageState extends State<HomePage> {
9999
100100 // Decoder Demo
101101 _buildSeparator (context, 'Decoder Demo' ),
102- _buildListTile (context, 'Import From JSON' , () {}),
103- _buildListTile (context, 'Import From Markdown' , () {}),
102+ _buildListTile (context, 'Import From JSON' , () {
103+ _importFile (ExportFileType .json);
104+ }),
105+ _buildListTile (context, 'Import From Markdown' , () {
106+ _importFile (ExportFileType .markdown);
107+ }),
104108
105109 // Theme Demo
106110 _buildSeparator (context, 'Theme Demo' ),
@@ -213,4 +217,32 @@ class _HomePageState extends State<HomePage> {
213217 ..click ();
214218 }
215219 }
220+
221+ void _importFile (ExportFileType fileType) async {
222+ final result = await FilePicker .platform.pickFiles (
223+ allowMultiple: false ,
224+ allowedExtensions: [fileType.extension ],
225+ type: FileType .custom,
226+ );
227+ final path = result? .files.single.path;
228+ if (path == null ) {
229+ return ;
230+ }
231+ final plainText = await File (path).readAsString ();
232+ var jsonString = '' ;
233+ switch (fileType) {
234+ case ExportFileType .json:
235+ jsonString = jsonEncode (plainText);
236+ break ;
237+ case ExportFileType .markdown:
238+ jsonString = jsonEncode (markdownToDocument (plainText).toJson ());
239+ break ;
240+ case ExportFileType .html:
241+ throw UnimplementedError ();
242+ }
243+
244+ if (mounted) {
245+ _loadEditor (context, Future <String >.value (jsonString));
246+ }
247+ }
216248}
0 commit comments