如何将资产图片保存到共享首选项?

时间:2020-05-24 19:17:54

标签: flutter dart sharedpreferences

我正在将图像从一页传递到另一页,我想知道如何将第二页中的图像保存到共享的首选项中。

注意:此图片是嵌入在应用程序中的资产图片,不需要从图库中提取, 这是用于更多说明的代码,这只是我的项目中的草稿示例:

从此页面传递的图片

 class ImagePage extends StatefulWidget {
  @override
  _ImagePageState createState() => _ImagePageState();
}

class _ImagePageState extends State<ImagePage> {
  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        Image(
          image: AssetImage('images/cake.png'),
        ),
        RaisedButton(
          onPressed: () {
            Navigator.push(
              context,
              MaterialPageRoute(
                builder: (context) => CandyJar(
                  cakeImage: AssetImage('images/cake.png'),
                ),
              ),
            );
          },
          child: Text(
              'Pass cake image to Candy Jar Page'),
        ),
      ],
    );
  }
}`

接收页面

    class CandyJar extends StatefulWidget {
  CandyJar({
    this.cakeImage,
  });
  final ImageProvider cakeImage;
  @override
  _CandyJarState createState() => _CandyJarState();
}

class _CandyJarState extends State<CandyJar> {
  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.all(25.0),
      padding: EdgeInsets.all(10.0),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(25.0),
        color: Colors.white30,
      ),
      child: Image(
        image: widget.cakeImage ?? AssetImage('images/mysterybox.png'),
        height: 70.0,
        width: 70.0,
      ),
    );
  }
}

0 个答案:

没有答案