0

我编写了一个返回图像列表并在 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;
          }
        },
      ),
    );
  }

实际上,我想实现裁剪器,它根据某个坐标分割图像,并且在裁剪图像后存储在“图像”库中widget.dyCoordinate并使用“图像”库,copyCrop()而不是将图像库的部分图像编码为 Flutter 的图像并返回列表图像。

您的家伙知道如何将每个图像保存在设备上的某个位置或在将其路径存储在 sharedPreferences 之后制作文件

4

1 回答 1

0

如果您想存储多个图像的路径,那么您必须使用json.encode它以便将其转换为字符串,然后您可以将其存储在共享首选项中。

SharedPreferences.setString(key, json.encode(value));

但最好使用任何本地数据库,如 sqlite( https://pub.dev/packages/sqflite ) 或 hive ( https://pub.dev/packages/hive )。

于 2020-10-06T13:59:18.783 回答