我已经对仅选择的 GIF 文件进行了大量研发,所有结果都只显示或显示 GIF 文件,但我只想从本地设备中选择 GIF 文件,如图像或视频。
1 回答
2
您可以使用file_picker包并将其设置allowedExtensions
为gif
.
allowedExtensions: ['gif']
这是一个例子:
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:file_picker/file_picker.dart';
void main() {
runApp(MaterialApp(
home: MyApp(),
));
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
var _selectedFile;
String customAppLogoName;
_openFileManager() async {
FilePickerResult result = await FilePicker.platform.pickFiles(
type: FileType.custom,
allowedExtensions: ['gif'],
);
if (result != null) {
PlatformFile selectedFile = result.files.first;
setState(() {
_selectedFile = File(selectedFile.path);
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(
title: Text("File Picker"),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
_openFileManager();
},
child: Icon(Icons.image),
),
body: Center(
child: Container(
child: _selectedFile == null
? Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
Icons.broken_image,
size: 100,
),
Text('no image'),
],
)
: Image(
image: FileImage(_selectedFile),
),
),
),
);
}
}
结果:
于 2021-04-20T06:53:31.197 回答