基本上,我是新手。我想显示来自特定路径(例如“ Storage / WhatsApp / Media /”)的图像。我想在网格视图中显示所有图像。我该如何在颤抖中实现这一目标。我看到了很多示例,但是每个人都在使用Assets文件夹。这是获取路径的代码。如何在网格视图中显示它们?
Future<String> get localpath async
{
final dir=await getExternalStorageDirectory();
return dir.path;
}
Future<File> get localfile async{
final path=await localpath;
return File('$path/WhatsApp/Media/WhatsApp Images');
}
Future<String> readData() async{
try{
final file=await localfile;
String image_path=await file.readAsString();
return image_path;
}
catch(e) {return e.toString();}
}
现在,既然我已经知道了如何在gridview中显示图像?我应该使用gridview.builder吗?
答案 0 :(得分:1)
提供您的存储路径即可显示图片
Image.file(File(imagePath);
答案 1 :(得分:0)
答案 2 :(得分:0)
我尝试使用与您类似的方法来使用path_provider
来获取文件。但是,即使使用getExternalStorageDirectory()
,该插件也只能获取应用程序的目录。 Stack Overflow post中也指出了这种行为。
相反,我使用ext_storage
来访问设备的存储空间。
此示例应用程序演示了使用ext_storage
获取设备的/ Download文件夹中的文件,使用permission_handler
处理权限,文件扩展名检查以及在GridView上显示路径中的图像。
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:ext_storage/ext_storage.dart';
import 'package:permission_handler/permission_handler.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
Future _futureGetPath;
List<dynamic> listImagePath = List<dynamic>();
var _permissionStatus;
@override
void initState() {
super.initState();
_listenForPermissionStatus();
// Declaring Future object inside initState() method
// prevents multiple calls inside stateful widget
_futureGetPath = _getPath();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Expanded(
flex: 1,
child: FutureBuilder(
future: _futureGetPath,
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.hasData) {
var dir = Directory(snapshot.data);
print('permission status: $_permissionStatus');
if (_permissionStatus) _fetchFiles(dir);
return Text(snapshot.data);
} else {
return Text("Loading");
}
},
),
),
Expanded(
flex: 19,
child: GridView.count(
primary: false,
padding: const EdgeInsets.all(20),
crossAxisSpacing: 10,
mainAxisSpacing: 10,
crossAxisCount: 3,
children: _getListImg(listImagePath),
),
)
],
),
);
}
// Check for storage permission
void _listenForPermissionStatus() async {
final status = await Permission.storage.request().isGranted;
// setState() triggers build again
setState(() => _permissionStatus = status);
}
// Get storage path
// https://pub.dev/documentation/ext_storage/latest/
Future<String> _getPath() {
return ExtStorage.getExternalStoragePublicDirectory(
ExtStorage.DIRECTORY_DOWNLOADS);
}
_fetchFiles(Directory dir) {
List<dynamic> listImage = List<dynamic>();
dir.list().forEach((element) {
RegExp regExp =
new RegExp("\.(gif|jpe?g|tiff?|png|webp|bmp)", caseSensitive: false);
// Only add in List if path is an image
if (regExp.hasMatch('$element')) listImage.add(element);
setState(() {
listImagePath = listImage;
});
});
}
List<Widget> _getListImg(List<dynamic> listImagePath) {
List<Widget> listImages = List<Widget>();
for (var imagePath in listImagePath) {
listImages.add(
Container(
padding: const EdgeInsets.all(8),
child: Image.file(imagePath, fit: BoxFit.cover),
),
);
}
return listImages;
}
}
请注意,这仅涵盖在GridView上显示图像的基本知识。要进行扩展,建议您遵循performance best practices上的本指南。