blob: 593a1d60aed8ad1ad6b31d1307c92b113e40fcd5 [file] [log] [blame]
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:file_selector/file_selector.dart';
import 'package:flutter/material.dart';
/// Screen that shows an example of openFiles
class OpenImagePage extends StatelessWidget {
void _openImageFile(BuildContext context) async {
final XTypeGroup typeGroup = XTypeGroup(
label: 'images',
extensions: ['jpg', 'png'],
);
final List<XFile> files = await openFiles(acceptedTypeGroups: [typeGroup]);
final XFile file = files[0];
final String fileName = file.name;
final String filePath = file.path;
await showDialog(
context: context,
builder: (context) => ImageDisplay(fileName, filePath),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Open an image"),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ElevatedButton(
style: ElevatedButton.styleFrom(
primary: Colors.blue,
onPrimary: Colors.white,
),
child: Text('Press to open an image file(png, jpg)'),
onPressed: () => _openImageFile(context),
),
],
),
),
);
}
}
/// Widget that displays a text file in a dialog
class ImageDisplay extends StatelessWidget {
/// Image's name
final String fileName;
/// Image's path
final String filePath;
/// Default Constructor
ImageDisplay(this.fileName, this.filePath);
@override
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.network(filePath) : Image.file(File(filePath)),
actions: [
TextButton(
child: const Text('Close'),
onPressed: () {
Navigator.pop(context);
},
),
],
);
}
}