22// Use of this source code is governed by a BSD-style license that can be
33// found in the LICENSE file.
44
5+ import 'dart:convert' ;
56import 'package:flutter/material.dart' ;
7+ import 'package:file_selector/file_selector.dart' ;
68
79import '../common_widgets.dart' ;
810import '../config_specific/drag_and_drop/drag_and_drop.dart' ;
@@ -19,6 +21,7 @@ class FileImportContainer extends StatefulWidget {
1921 this .onAction,
2022 this .onFileSelected,
2123 this .onError,
24+ this .extensions = const ['json' ],
2225 Key key,
2326 }) : super (key: key);
2427
@@ -35,6 +38,9 @@ class FileImportContainer extends StatefulWidget {
3538
3639 final void Function (String error) onError;
3740
41+ /// The file's extensions where we are going to get the data from.
42+ final List <String > extensions;
43+
3844 @override
3945 _FileImportContainerState createState () => _FileImportContainerState ();
4046}
@@ -118,8 +124,7 @@ class _FileImportContainerState extends State<FileImportContainer> {
118124 child: _buildImportedFileDisplay (),
119125 ),
120126 ),
121- // TODO(kenz): uncomment once file picker support is added
122- // _buildImportButton(),
127+ _buildImportButton (),
123128 // Horizontal spacer with flex value of 1.
124129 const Flexible (
125130 child: SizedBox (height: rowHeight),
@@ -131,25 +136,25 @@ class _FileImportContainerState extends State<FileImportContainer> {
131136 Widget _buildImportedFileDisplay () {
132137 return Text (
133138 importedFile? .path ?? 'No File Selected' ,
139+ overflow: TextOverflow .ellipsis,
134140 style: TextStyle (
135141 color: Theme .of (context).textTheme.headline1.color,
136142 ),
137143 textAlign: TextAlign .left,
138144 );
139145 }
140146
141- // TODO(kenz): uncomment once file picker support is added
142- // Widget _buildImportButton() {
143- // return Row(
144- // mainAxisAlignment: MainAxisAlignment.center,
145- // children: [
146- // OutlinedButton(
147- // onPressed: () {},
148- // child: const MaterialIconLabel(Icons.file_upload, 'Import File'),
149- // ),
150- // ],
151- // );
152- // }
147+ Widget _buildImportButton () {
148+ return Row (
149+ mainAxisAlignment: MainAxisAlignment .center,
150+ children: [
151+ OutlinedButton (
152+ onPressed: _importFile,
153+ child: const MaterialIconLabel (Icons .file_upload, 'Import File' ),
154+ ),
155+ ],
156+ );
157+ }
153158
154159 Widget _buildActionButton () {
155160 return Column (
@@ -174,6 +179,19 @@ class _FileImportContainerState extends State<FileImportContainer> {
174179 );
175180 }
176181
182+ void _importFile () async {
183+ final acceptedTypeGroups = [XTypeGroup (extensions: widget.extensions)];
184+ final file = await openFile (acceptedTypeGroups: acceptedTypeGroups);
185+ final data = jsonDecode (await file.readAsString ());
186+ final lastModifiedTime = await file.lastModified ();
187+ final devToolsJsonFile = DevToolsJsonFile (
188+ data: data,
189+ name: file.name,
190+ lastModifiedTime: lastModifiedTime,
191+ );
192+ _handleImportedFile (devToolsJsonFile);
193+ }
194+
177195 // TODO(kenz): add error handling to ensure we only allow importing supported
178196 // files.
179197 void _handleImportedFile (DevToolsJsonFile file) {
0 commit comments