Skip to content

Commit 22c4657

Browse files
authored
[devtools_app] Integrate the file_selector plugin (#2506)
* integrate the file_selector_plugin into DevTools and add to app size screen.
1 parent 4b1cf0c commit 22c4657

File tree

2 files changed

+37
-14
lines changed

2 files changed

+37
-14
lines changed

packages/devtools_app/lib/src/app_size/file_import_container.dart

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
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';
56
import 'package:flutter/material.dart';
7+
import 'package:file_selector/file_selector.dart';
68

79
import '../common_widgets.dart';
810
import '../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) {

packages/devtools_app/pubspec.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ dependencies:
2828
collection: ^1.15.0-nnbd
2929
devtools_shared: 0.9.6+2
3030
file: ^5.1.0
31+
file_selector: ^0.7.0
32+
file_selector_linux: ^0.0.1
33+
file_selector_macos: ^0.0.1
34+
file_selector_web: ^0.7.0
35+
file_selector_windows: ^0.0.1
3136
flutter:
3237
sdk: flutter
3338
flutter_web_plugins:

0 commit comments

Comments
 (0)