我写了一个函数,返回List of Image并在ListView中使用,但我也想为每个图像创建文件,并将该文件路径存储在SharedPreferences中。
import 'package:image/image.dart' as imglib;
Future<List<Image>> _getListOfImages() async {
final dy = widget.dyCoordinate;
// split image to parts
List<imglib.Image> parts = List<imglib.Image>();
for (int i = 1; i < dy.length; i++) {
parts.add(
imglib.copyCrop(
src,
0,
(dy[i - 1] * aspectRatio).round(),
properties.width,
(dy[i] * aspectRatio).round() - (dy[i - 1] * aspectRatio).round(),
),
);
}
List<Image> images = [];
for (var img in parts) {
images.add(Image.memory(imglib.encodeJpg(img)));
}
return images;
}
@override
Widget build(BuildContext context) {
return Scaffold(
bottomSheet: AbsorbPointer(
absorbing: _isEnable,
child: Container(
margin: EdgeInsets.all(0),
alignment: Alignment.bottomCenter,
height: 48,
color: Colors.black87,
child: ButtonBar(
alignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.max,
buttonMinWidth: MediaQuery.of(context).size.width / 2.2,
children: <Widget>[
FlatButton(
child: Text(
'Record Audio',
style: TextStyle(color: Colors.white70, fontSize: 16),
),
// color: Colors.black45,
onPressed: () {
_showMyDialog();
},
),
FlatButton(
child: Text(
'Select Audio',
style: TextStyle(color: Colors.white70, fontSize: 16),
),
// color: Colors.black45,
onPressed: () {/** */},
),
],
),
)),
backgroundColor: Colors.black,
floatingActionButton: FloatingActionButton(
onPressed: () {},
child: Icon(Icons.check),
),
body: FutureBuilder(
future: _getListOfImages(), // async work
builder: (context, snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.none:
case ConnectionState.waiting:
case ConnectionState.active:
return Container(
alignment: Alignment.center,
child: CircularProgressIndicator(
backgroundColor: Colors.amber[50],
),
);
break;
case ConnectionState.done:
if (snapshot.hasError) {
// return whatever you'd do for this case, probably an error
return Container(
alignment: Alignment.center,
child: Text("Error: ${snapshot.error}"),
);
}
var data = snapshot.data;
return ListView.builder(
reverse: false,
itemBuilder: (_, int index) => ListTile(
dense: true,
onTap: () {
setState(() {
_isEnable = !_isEnable;
});
},
title: Container(
margin: const EdgeInsets.all(0),
child: data[index],
),
),
itemCount: data.length,
);
break;
}
},
),
);
}
实际上,我想实现cropper,它根据某个坐标和widget.dyCoordinate
中的坐标存储来分割图像,并在裁剪存储在"parts"中的图像后使用copyCrop()
中的"image"库,将图像库中的部分图像编码为Flutter的图像并返回图像列表。
你的家伙知道如何将每个图像保存在设备上的某个位置,或者在共享首选项中存储路径后制作文件
如果你想存储多个图像的路径,那么你必须使用json.encode
,这样它就会转换为字符串,然后你可以将它存储在共享的首选项中。
SharedPreferences.setString(key, json.encode(value));
但最好使用任何本地数据库,如sqlite(https://pub.dev/packages/sqflite)或蜂箱(https://pub.dev/packages/hive)。