|
| 1 | +import 'dart:io'; |
| 2 | +import 'package:flutter/foundation.dart'; |
| 3 | +import 'package:file_selector/file_selector.dart'; |
| 4 | +import 'package:flutter/material.dart'; |
| 5 | + |
| 6 | +/// Screen that shows an example of openFiles |
| 7 | +class OpenImagePage extends StatelessWidget { |
| 8 | + void _openImageFile(BuildContext context) async { |
| 9 | + final XTypeGroup typeGroup = XTypeGroup( |
| 10 | + label: 'images', |
| 11 | + extensions: ['jpg', 'png'], |
| 12 | + ); |
| 13 | + final List<XFile> files = await openFiles(acceptedTypeGroups: [typeGroup]); |
| 14 | + final XFile file = files[0]; |
| 15 | + final String fileName = file.name; |
| 16 | + final String filePath = file.path; |
| 17 | + |
| 18 | + await showDialog( |
| 19 | + context: context, |
| 20 | + builder: (context) => ImageDisplay(fileName, filePath), |
| 21 | + ); |
| 22 | + } |
| 23 | + |
| 24 | + @override |
| 25 | + Widget build(BuildContext context) { |
| 26 | + return Scaffold( |
| 27 | + appBar: AppBar( |
| 28 | + title: Text("Open an image"), |
| 29 | + ), |
| 30 | + body: Center( |
| 31 | + child: Column( |
| 32 | + mainAxisAlignment: MainAxisAlignment.center, |
| 33 | + children: <Widget>[ |
| 34 | + RaisedButton( |
| 35 | + color: Colors.blue, |
| 36 | + textColor: Colors.white, |
| 37 | + child: Text('Press to open an image file(png, jpg)'), |
| 38 | + onPressed: () => _openImageFile(context), |
| 39 | + ), |
| 40 | + ], |
| 41 | + ), |
| 42 | + ), |
| 43 | + ); |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +/// Widget that displays a text file in a dialog |
| 48 | +class ImageDisplay extends StatelessWidget { |
| 49 | + /// Image's name |
| 50 | + final String fileName; |
| 51 | + |
| 52 | + /// Image's path |
| 53 | + final String filePath; |
| 54 | + |
| 55 | + /// Default Constructor |
| 56 | + ImageDisplay(this.fileName, this.filePath); |
| 57 | + |
| 58 | + @override |
| 59 | + Widget build(BuildContext context) { |
| 60 | + return AlertDialog( |
| 61 | + title: Text(fileName), |
| 62 | + // On web the filePath is a blob url |
| 63 | + // while on other platforms it is a system path. |
| 64 | + content: kIsWeb ? Image.network(filePath) : Image.file(File(filePath)), |
| 65 | + actions: [ |
| 66 | + FlatButton( |
| 67 | + child: const Text('Close'), |
| 68 | + onPressed: () { |
| 69 | + Navigator.pop(context); |
| 70 | + }, |
| 71 | + ), |
| 72 | + ], |
| 73 | + ); |
| 74 | + } |
| 75 | +} |
0 commit comments