blob: 9b46571b90050e853df99c09495b66b5b145d29a [file] [log] [blame]
// 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 'dart:io';
import 'package:file_selector_platform_interface/file_selector_platform_interface.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
/// Screen that allows the user to select an image file using
/// `openFiles`, then displays the selected images in a gallery dialog.
class OpenImagePage extends StatelessWidget {
/// Default Constructor
const OpenImagePage({super.key});
Future<void> _openImageFile(BuildContext context) async {
const XTypeGroup typeGroup = XTypeGroup(
label: 'images',
extensions: <String>['jpg', 'png'],
uniformTypeIdentifiers: <String>['public.image'],
final XFile? file = await FileSelectorPlatform.instance
.openFile(acceptedTypeGroups: <XTypeGroup>[typeGroup]);
if (file == null) {
// Operation was canceled by the user.
final String fileName =;
final String filePath = file.path;
if (context.mounted) {
await showDialog<void>(
context: context,
builder: (BuildContext context) => ImageDisplay(fileName, filePath),
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Open an image'),
body: Center(
child: Column(
children: <Widget>[
style: ElevatedButton.styleFrom(
foregroundColor: Colors.white,
child: const Text('Press to open an image file(png, jpg)'),
onPressed: () => _openImageFile(context),
/// Widget that displays an image in a dialog.
class ImageDisplay extends StatelessWidget {
/// Default Constructor.
const ImageDisplay(this.fileName, this.filePath, {super.key});
/// The name of the selected file.
final String fileName;
/// The path to the selected file.
final String filePath;
Widget build(BuildContext context) {
return AlertDialog(
title: Text(fileName),
// On web the filePath is a blob url
// while on other platforms it is a system path.
content: kIsWeb ? : Image.file(File(filePath)),
actions: <Widget>[
child: const Text('Close'),
onPressed: () {