Advertisement
Guest User

Untitled

a guest
May 25th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 5.01 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2. import 'dart:async';
  3. import 'package:flutter/services.dart';
  4. import 'package:multi_image_picker/multi_image_picker.dart';
  5. import 'dart:typed_data';
  6. import 'dart:io';
  7. import 'package:image/image.dart' as v3;
  8. import 'dart:ui' as v2;
  9.  
  10. //import 'package:flutter/widgets.dart' as v2;
  11.  
  12. void main() => runApp(new MyApp());
  13.  
  14.  
  15. class MyApp extends StatefulWidget {
  16.   @override
  17.   _MyAppState createState() => new _MyAppState();
  18. }
  19.  
  20. class _MyAppState extends State<MyApp> {
  21.   List<Asset> images = List<Asset>();
  22.   //List<Image> grayImages = List<Image>();
  23.   Widget _image = new Container();
  24.   @override
  25.   void initState() {
  26.     super.initState();
  27.   }
  28. // image.File()
  29.   Widget buildGridView() {
  30.     return GridView.count(
  31.       crossAxisCount: 3, //images.length
  32.       children: List.generate(images.length, (index) {
  33.         Asset asset1 = images[index];
  34.         var x = asset1.identifier;
  35.         print("The asset's name is $x");
  36.         //print("The string rep is $grayScaleImage(asset1).toString()");
  37.  
  38.         return AssetThumb(
  39.           asset: asset1,
  40.           width: 300,
  41.           height: 300,
  42.         );
  43.  
  44.       }), // displays the selected images in a grid view
  45.     );
  46.   }
  47.  
  48. //  Future<AssetThumb> assetThumbMaker(Asset asset1) async {
  49. ////    await saveImage(asset1);
  50. ////    return await assetThumbMaker2(asset1);
  51. ////
  52. ////  }
  53. ////
  54. ////  Future<AssetThumb> assetThumbMaker2(Asset asset1) async {
  55. ////    await Future.delayed(const Duration(seconds: 5), (){});
  56. ////    print("got here 3");
  57. ////    return AssetThumb(
  58. ////      asset: asset1,
  59. ////      width: 300,
  60. ////      height: 300,
  61. ////    );
  62. ////  }
  63.  
  64. //  Future<void> saveImage(Asset asset1) async {
  65. //    var z = await grayScaleImage(asset1);
  66. //    v3.Image image1 = v3.decodeJpg(z);
  67. //    v3.Image thumbnail = v3.copyResize(image1, width: 120);
  68. //    File('assets/thumbnail-test.jpeg').writeAsBytesSync(v3.encodeJpg(thumbnail));
  69. //    print("got here");
  70. //    //Image.memory(z);
  71. //  }
  72.  
  73.   Future<void> deleteAssets() async {
  74.     await MultiImagePicker.deleteImages(assets: images);
  75.     setState(() {
  76.       images = List<Asset>();
  77.     });
  78.   }
  79.  
  80.   Future<void> grayScaleImage(Asset asset) async {
  81.     print("here 1");
  82.     ByteData bd = await asset.requestThumbnail(
  83.         asset.originalWidth, asset.originalHeight,
  84.         quality: 100); // width and height
  85.     var a2 = bd.buffer.asUint8List();
  86.     var input1 =
  87.         v3.Image.fromBytes(asset.originalWidth, asset.originalHeight, a2);
  88.     v3.Image grayImage = v3.grayscale(input1);
  89.     print("here 2");
  90.  
  91.     Image.memory(Uint8List.fromList(v3.encodeJpg(grayImage)));
  92.     var img = Image.memory(Uint8List.fromList(v3.encodeJpg(grayImage)));;
  93.     setState(() => _image = img);
  94.     //Uint8List.fromList(v3.encodeJpg(grayImage));
  95.     //return v3.encodeJpg(grayImage);
  96.     // Image(image: MemoryImage(listaU8L[0]));
  97.   }
  98.  
  99.   Future<void> loadAssets() async {
  100.     List<Asset> resultList = List<Asset>();
  101.     String error = 'No Error Dectected';
  102.  
  103.     try {
  104.       resultList = await MultiImagePicker.pickImages(
  105.           maxImages: 300,
  106.           enableCamera: true,
  107.           selectedAssets: images,
  108.           cupertinoOptions: CupertinoOptions(takePhotoIcon: "chat"),
  109.           materialOptions: MaterialOptions(
  110.             allViewTitle: "All Photos",
  111.           ));
  112.     } on PlatformException catch (e) {
  113.       error = e.message;
  114.       print(error);
  115.     }
  116.  
  117.     // If the widget was removed from the tree while the asynchronous platform
  118.     // message was in flight, we want to discard the reply rather than calling
  119.     // setState to update our non-existent appearance.
  120.     if (!mounted) return;
  121.  
  122.     setState(() {
  123.       images = resultList;
  124.       print(error);
  125.     });
  126.   }
  127.  
  128.   @override
  129.   Widget build(BuildContext context) {
  130.     return new MaterialApp(
  131.       debugShowCheckedModeBanner: false,
  132.       home: new Scaffold(
  133.         appBar: new AppBar(
  134.           title: const Text('GrayScale Maker'),
  135.           centerTitle: true,
  136.           backgroundColor: Colors.deepOrangeAccent,
  137.         ),
  138.         body: Stack(
  139.           children: <Widget>[
  140.             new Container(
  141.               decoration: new BoxDecoration(
  142.                 image: new DecorationImage(
  143.                   image: new AssetImage("assets/background.jpg"),
  144.                   fit: BoxFit.cover,
  145.                 ), // adds background image
  146.               ),
  147.             ),
  148.             Column(
  149.               children: <Widget>[
  150.                 RaisedButton(
  151.                   child: Text("Pick images"),
  152.                   onPressed: loadAssets,
  153.                 ),
  154.                 images.length > 0
  155.                     ? RaisedButton(
  156.                         child: Text("Remove All Images"),
  157.                         onPressed: deleteAssets,
  158.                       )
  159.                     : Container(),
  160.                 Expanded(
  161.                   child: _image,
  162.                 )
  163.               ],
  164.             ),
  165.           ],
  166.         ),
  167.       ),
  168.     );
  169.   }
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement