Skip to content

Commit

Permalink
[file_selector] Fix default accept types on iOS (#4691)
Browse files Browse the repository at this point in the history
Uses `public.data` as the default accept type on iOS, instead of an empty list, since unlike on macOS an empty list of accept types doesn't mean to accept every type, so the default on iOS was not allowing any files.

Adds another page to the implementation package's example app to facilitate manual testing of this behavior for package developers.

Fixes flutter/flutter#132211
  • Loading branch information
stuartmorgan authored Aug 14, 2023
1 parent 08080ab commit 84218b9
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 6 deletions.
3 changes: 2 additions & 1 deletion packages/file_selector/file_selector_ios/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## NEXT
## 0.5.1+5

* Fixes the behavior of no type groups to allow selecting any file.
* Migrates `styleFrom` usage in examples off of deprecated `primary` and `onPrimary` parameters.

## 0.5.1+4
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ class HomePage extends StatelessWidget {
onPressed: () => Navigator.pushNamed(context, '/open/image'),
),
const SizedBox(height: 10),
ElevatedButton(
style: style,
child: const Text('Open any file'),
onPressed: () => Navigator.pushNamed(context, '/open/any'),
),
const SizedBox(height: 10),
ElevatedButton(
style: style,
child: const Text('Open multiple images'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import 'package:flutter/material.dart';

import 'home_page.dart';
import 'open_any_page.dart';
import 'open_image_page.dart';
import 'open_multiple_images_page.dart';
import 'open_text_page.dart';
Expand Down Expand Up @@ -32,6 +33,7 @@ class MyApp extends StatelessWidget {
'/open/images': (BuildContext context) =>
const OpenMultipleImagesPage(),
'/open/text': (BuildContext context) => const OpenTextPage(),
'/open/any': (BuildContext context) => const OpenAnyPage(),
},
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:file_selector_platform_interface/file_selector_platform_interface.dart';
import 'package:flutter/material.dart';

/// Screen that allows the user to select any file file using `openFile`, then
/// displays its path in a dialog.
class OpenAnyPage extends StatelessWidget {
/// Default Constructor
const OpenAnyPage({super.key});

Future<void> _openTextFile(BuildContext context) async {
final XFile? file = await FileSelectorPlatform.instance.openFile();
if (file == null) {
// Operation was canceled by the user.
return;
}

if (context.mounted) {
await showDialog<void>(
context: context,
builder: (BuildContext context) => PathDisplay(file.name, file.path),
);
}
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Open a file'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ElevatedButton(
style: ElevatedButton.styleFrom(
backgroundColor: Colors.blue,
foregroundColor: Colors.white,
),
child: const Text('Press to open a file of any type'),
onPressed: () => _openTextFile(context),
),
],
),
),
);
}
}

/// Widget that displays a text file in a dialog.
class PathDisplay extends StatelessWidget {
/// Default Constructor.
const PathDisplay(this.fileName, this.filePath, {super.key});

/// The name of the selected file.
final String fileName;

/// The contents of the text file.
final String filePath;

@override
Widget build(BuildContext context) {
return AlertDialog(
title: Text(fileName),
content: Text(filePath),
actions: <Widget>[
TextButton(
child: const Text('Close'),
onPressed: () => Navigator.pop(context),
),
],
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,18 @@ class FileSelectorIOS extends FileSelectorPlatform {
// Converts the type group list into a list of all allowed UTIs, since
// iOS doesn't support filter groups.
List<String> _allowedUtiListFromTypeGroups(List<XTypeGroup>? typeGroups) {
// iOS requires a list of allowed types, so allowing all is expressed via
// a root type rather than an empty list.
const List<String> allowAny = <String>['public.data'];

if (typeGroups == null || typeGroups.isEmpty) {
return <String>[];
return allowAny;
}
final List<String> allowedUTIs = <String>[];
for (final XTypeGroup typeGroup in typeGroups) {
// If any group allows everything, no filtering should be done.
if (typeGroup.allowsAny) {
return <String>[];
return allowAny;
}
if (typeGroup.uniformTypeIdentifiers?.isEmpty ?? true) {
throw ArgumentError('The provided type group $typeGroup should either '
Expand Down
2 changes: 1 addition & 1 deletion packages/file_selector/file_selector_ios/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: file_selector_ios
description: iOS implementation of the file_selector plugin.
repository: https://github.com/flutter/packages/tree/main/packages/file_selector/file_selector_ios
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+file_selector%22
version: 0.5.1+4
version: 0.5.1+5

environment:
sdk: ">=2.18.0 <4.0.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,25 @@ void main() {
throwsArgumentError);
});

test('allows a wildcard group', () async {
test('correctly handles no type groups', () async {
await expectLater(plugin.openFile(), completes);
final VerificationResult result = verify(mockApi.openFile(captureAny));
final FileSelectorConfig config =
result.captured[0] as FileSelectorConfig;
expect(listEquals(config.utis, <String>['public.data']), isTrue);
});

test('correctly handles a wildcard group', () async {
const XTypeGroup group = XTypeGroup(
label: 'text',
);

await expectLater(
plugin.openFile(acceptedTypeGroups: <XTypeGroup>[group]), completes);
final VerificationResult result = verify(mockApi.openFile(captureAny));
final FileSelectorConfig config =
result.captured[0] as FileSelectorConfig;
expect(listEquals(config.utis, <String>['public.data']), isTrue);
});
});

Expand Down Expand Up @@ -113,6 +125,7 @@ void main() {
isTrue);
expect(config.allowMultiSelection, isTrue);
});

test('throws for a type group that does not support iOS', () async {
const XTypeGroup group = XTypeGroup(
label: 'images',
Expand All @@ -124,13 +137,25 @@ void main() {
throwsArgumentError);
});

test('allows a wildcard group', () async {
test('correctly handles no type groups', () async {
await expectLater(plugin.openFiles(), completes);
final VerificationResult result = verify(mockApi.openFile(captureAny));
final FileSelectorConfig config =
result.captured[0] as FileSelectorConfig;
expect(listEquals(config.utis, <String>['public.data']), isTrue);
});

test('correctly handles a wildcard group', () async {
const XTypeGroup group = XTypeGroup(
label: 'text',
);

await expectLater(
plugin.openFiles(acceptedTypeGroups: <XTypeGroup>[group]), completes);
final VerificationResult result = verify(mockApi.openFile(captureAny));
final FileSelectorConfig config =
result.captured[0] as FileSelectorConfig;
expect(listEquals(config.utis, <String>['public.data']), isTrue);
});
});
}

0 comments on commit 84218b9

Please sign in to comment.