diff --git a/.dart_tool/package_config.json b/.dart_tool/package_config.json index 691ec40..f8ce131 100644 --- a/.dart_tool/package_config.json +++ b/.dart_tool/package_config.json @@ -247,12 +247,6 @@ "packageUri": "lib/", "languageVersion": "2.5" }, - { - "name": "image_save", - "rootUri": "file:///Users/fnuser/.pub-cache/hosted/pub.flutter-io.cn/image_save-3.1.1", - "packageUri": "lib/", - "languageVersion": "2.1" - }, { "name": "intl", "rootUri": "file:///Users/fnuser/.pub-cache/hosted/pub.flutter-io.cn/intl-0.16.1", @@ -379,6 +373,12 @@ "packageUri": "lib/", "languageVersion": "2.6" }, + { + "name": "save_image", + "rootUri": "file:///Users/fnuser/.pub-cache/hosted/pub.flutter-io.cn/save_image-1.0.1", + "packageUri": "lib/", + "languageVersion": "2.1" + }, { "name": "shared_preferences", "rootUri": "file:///Users/fnuser/.pub-cache/hosted/pub.flutter-io.cn/shared_preferences-0.5.12", @@ -584,7 +584,7 @@ "languageVersion": "2.1" } ], - "generated": "2020-09-27T12:18:22.038779Z", + "generated": "2020-09-28T01:22:36.945080Z", "generator": "pub", "generatorVersion": "2.7.2" } diff --git a/lib/pages/favorite_page.dart b/lib/pages/favorite_page.dart deleted file mode 100644 index 21da51b..0000000 --- a/lib/pages/favorite_page.dart +++ /dev/null @@ -1,79 +0,0 @@ -import 'dart:typed_data'; -import 'dart:ui' as ui; - -import 'package:flutter/material.dart'; -import 'package:flutter/rendering.dart'; -import 'package:zhiying_base_widget/template/goods_share_template/goods_share_template.dart'; -import 'package:zhiying_comm/zhiying_comm.dart'; -import 'package:save_image/save_image.dart'; - -class FavoritePage extends StatefulWidget { - @override - _FavoritePageState createState() => _FavoritePageState(); -} - -class _FavoritePageState extends State { - GlobalKey globalKey = GlobalKey(); - - @override - Widget build(BuildContext context) { - return Scaffold( - appBar: AppBar( - title: Text('收藏夹'), - ), - body: Column( - children: [ - Container( - width: 100, - height: 200, - child: Transform.scale(scale: 0.2, child: - RepaintBoundary( - key: globalKey, - child: GoodsShareTemplate() - ),), - ), - FlatButton( - child: Icon(Icons.add), - onPressed: () async { - try { - BuildContext buildContext = globalKey.currentContext; - - if (null != buildContext) { - RenderRepaintBoundary boundary = - buildContext.findRenderObject(); - var image = await boundary.toImage(); - ByteData byteData = - await image.toByteData(format: ui.ImageByteFormat.png); - - // final result = await ImageGallerySaver.saveImage( - // byteData.buffer.asUint8List()); - // var response = await Dio().get( - // "https://ss0.baidu.com/94o3dSag_xI4khGko9WTAnF6hhy/image/h%3D300/sign=a62e824376d98d1069d40a31113eb807/838ba61ea8d3fd1fc9c7b6853a4e251f94ca5f46.jpg", - // options: Options(responseType: ResponseType.bytes)); - // final _imageSaver = ImageSaver(); - // final success = await _imageSaver.saveImages( - // imageBytes: byteData.buffer.asUint8List() - // ); - // final success = await ImageGallerySaver.saveImage( - // byteData.buffer.asUint8List(), - // quality: 80, - // name: "hello"); - // bool success = await ImageSave.saveImageToSandbox( - // byteData.buffer.asUint8List(), "demo.png"); - // print('${success}'); - // print(result); - bool isSaveSuccess = - await SaveImage.save( - imageBytes: byteData.buffer.asUint8List()); - print(isSaveSuccess ? "save success" : 'save fail'); - } - } catch (err) { - Logger.error(err); - } - }, - ) - ], - ), - ); - } -} diff --git a/lib/pages/favorite_page/favorite_page.dart b/lib/pages/favorite_page/favorite_page.dart new file mode 100644 index 0000000..f0b8405 --- /dev/null +++ b/lib/pages/favorite_page/favorite_page.dart @@ -0,0 +1,82 @@ +import 'dart:typed_data'; +import 'dart:ui' as ui; + +import 'package:flutter/material.dart'; +import 'package:flutter/rendering.dart'; +import 'package:flutter/services.dart'; +import 'package:fluttertoast/fluttertoast.dart'; +import 'package:save_image/save_image.dart'; +import 'package:zhiying_base_widget/pages/favorite_page/preview_page.dart'; +import 'package:zhiying_base_widget/template/goods_share_template/goods_share_template.dart'; +import 'package:zhiying_comm/zhiying_comm.dart'; + +class FavoritePage extends StatefulWidget { + @override + _FavoritePageState createState() => _FavoritePageState(); +} + +class _FavoritePageState extends State { + GlobalKey _globalKey = GlobalKey(); + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar( + title: Text('收藏夹'), + ), + body: Column( + children: [ + Container( + width: 100, + height: 200, + child: Transform.scale( + scale: 0.2, + child: GoodsShareTemplate( + contentKey: _globalKey, + ), + ), + ), + FlatButton( + child: Icon(Icons.add), + onPressed: () async { + try { + BuildContext buildContext = _globalKey.currentContext; + + if (null != buildContext) { + RenderRepaintBoundary boundary = + buildContext.findRenderObject(); + ui.Image image = await boundary.toImage(pixelRatio: 2.0); + // 注意:png是压缩后格式,如果需要图片的原始像素数据,请使用rawRgba + ByteData byteData = + await image.toByteData(format: ui.ImageByteFormat.png); + Uint8List pngBytes = byteData.buffer.asUint8List(); + + if (false) { + bool isSaveSuccess = + await SaveImage.save(imageBytes: pngBytes); + if (isSaveSuccess) { + Fluttertoast.showToast(msg: '保存成功'); + } else { + Fluttertoast.showToast(msg: '保存失败'); + } + } + + Navigator.push( + context, + MaterialPageRoute( + builder: (context) => PreviewPage( + image: Image.memory(pngBytes), + ), + ), + ); + } + } catch (err) { + Logger.error(err); + } + }, + ) + ], + ), + ); + } +} diff --git a/lib/pages/favorite_page/preview_page.dart b/lib/pages/favorite_page/preview_page.dart new file mode 100644 index 0000000..026967e --- /dev/null +++ b/lib/pages/favorite_page/preview_page.dart @@ -0,0 +1,17 @@ +import 'package:flutter/material.dart'; + +class PreviewPage extends StatelessWidget { + final Image image; + + const PreviewPage({Key key, this.image}) : super(key: key); + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar(), + body: Center( + child: image, + ), + ); + } +} diff --git a/lib/register.dart b/lib/register.dart index 2610e7f..15f5de4 100644 --- a/lib/register.dart +++ b/lib/register.dart @@ -1,4 +1,4 @@ -import 'package:zhiying_base_widget/pages/favorite_page.dart'; +import 'package:zhiying_base_widget/pages/favorite_page/favorite_page.dart'; import 'package:zhiying_base_widget/pages/goods_details_page/goods_details_page.dart'; import 'package:zhiying_base_widget/pages/invited_friends/invited_friends.dart'; import 'package:zhiying_base_widget/pages/launch_page/launch_page.dart'; diff --git a/lib/template/goods_share_template/goods_share_template.dart b/lib/template/goods_share_template/goods_share_template.dart index 78a3d3d..cd08192 100644 --- a/lib/template/goods_share_template/goods_share_template.dart +++ b/lib/template/goods_share_template/goods_share_template.dart @@ -1,25 +1,32 @@ +import 'package:cached_network_image/cached_network_image.dart'; import 'package:flutter/material.dart'; import 'package:zhiying_comm/zhiying_comm.dart'; // 商品详情分享,合成模板 class GoodsShareTemplate extends StatelessWidget { - const GoodsShareTemplate({Key key}) : super(key: key); + final GlobalKey contentKey; + + const GoodsShareTemplate({Key key, this.contentKey}) : super(key: key); @override Widget build(BuildContext context) { return OverflowBox( maxHeight: double.infinity, - maxWidth: 375, - child: Container( - width: double.infinity, - padding: EdgeInsets.all(20), - color: Colors.white, - child: Column( - children: [ - _createHeader(), - _createImage(), - _createBottom(), - ], + maxWidth: 375, //固定宽度,保证所以手机下生成的模板一致 + child: RepaintBoundary( + key: contentKey, // 用于导出模板样式的key + child: Container( + // 模板的内容 + width: double.infinity, + padding: EdgeInsets.all(20), + color: Colors.white, + child: Column( + children: [ + _createHeader(), + _createImage(), + _createBottom(), + ], + ), ), ), ); @@ -56,13 +63,21 @@ class GoodsShareTemplate extends StatelessWidget { ); } + // 商品图 Widget _createImage() { - return AspectRatio( - aspectRatio: 1, - child: Container( - margin: EdgeInsets.only(top: 20, bottom: 20), - width: double.infinity, - color: Colors.redAccent, + return Padding( + padding: const EdgeInsets.only(top: 20, bottom: 20), + child: AspectRatio( + aspectRatio: 1, + child: Container( + width: double.infinity, + child: CachedNetworkImage( + imageUrl: + 'https://img.alicdn.com/bao/uploaded/i1/2200818126974/O1CN01HvqWcg21O8mq9EPwf_!!0-item_pic.jpg_500x500.jpg', + fit: BoxFit.fitWidth, + ), + // color: Colors.redAccent, + ), ), ); } @@ -93,7 +108,11 @@ class GoodsShareTemplate extends StatelessWidget { margin: EdgeInsets.only(bottom: 12), width: 124, height: 124, - color: Colors.redAccent, + // color: Colors.redAccent, + child: CachedNetworkImage( + imageUrl: + 'https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=1410436569,3239963707&fm=26&gp=0.jpg', + ), ), Container( child: Row(