main.dart 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import 'package:flutter/material.dart';
  2. import 'dart:async';
  3. import 'package:flutter/services.dart';
  4. import 'package:multimage_picker/multimage_picker.dart';
  5. void main() => runApp(new MyApp());
  6. class MyApp extends StatefulWidget {
  7. @override
  8. _MyAppState createState() => new _MyAppState();
  9. }
  10. class _MyAppState extends State<MyApp> {
  11. List<Asset> images = List<Asset>();
  12. String _error = 'No Error Dectected';
  13. @override
  14. void initState() {
  15. super.initState();
  16. }
  17. Widget buildGridView() {
  18. return GridView.count(
  19. crossAxisCount: 3,
  20. children: List.generate(images.length, (index) {
  21. Asset asset = images[index];
  22. return AssetThumb(
  23. asset: asset,
  24. width: 300,
  25. height: 300,
  26. );
  27. }),
  28. );
  29. }
  30. Future<void> deleteAssets() async {
  31. await MultiImagePicker.deleteImages(assets: images);
  32. setState(() {
  33. images = List<Asset>();
  34. });
  35. }
  36. Future<void> loadAssets() async {
  37. List<Asset> resultList = List<Asset>();
  38. String error = 'No Error Dectected';
  39. try {
  40. resultList = await MultiImagePicker.pickImages(
  41. maxImages: 300,
  42. enableCamera: true,
  43. selectedAssets: images,
  44. cupertinoOptions: CupertinoOptions(takePhotoIcon: "chat"),
  45. materialOptions: MaterialOptions(
  46. actionBarColor: "#abcdef",
  47. actionBarTitle: "Example App",
  48. allViewTitle: "All Photos",
  49. selectCircleStrokeColor: "#000000",
  50. ));
  51. } on PlatformException catch (e) {
  52. error = e.message;
  53. }
  54. // If the widget was removed from the tree while the asynchronous platform
  55. // message was in flight, we want to discard the reply rather than calling
  56. // setState to update our non-existent appearance.
  57. if (!mounted) return;
  58. setState(() {
  59. images = resultList;
  60. _error = error;
  61. });
  62. }
  63. @override
  64. Widget build(BuildContext context) {
  65. return new MaterialApp(
  66. home: new Scaffold(
  67. appBar: new AppBar(
  68. title: const Text('Plugin example app'),
  69. ),
  70. body: Column(
  71. children: <Widget>[
  72. Center(child: Text('Error: $_error')),
  73. RaisedButton(
  74. child: Text("Pick images"),
  75. onPressed: loadAssets,
  76. ),
  77. images.length > 0
  78. ? RaisedButton(
  79. child: Text("Delete images"),
  80. onPressed: deleteAssets,
  81. )
  82. : Container(),
  83. Expanded(
  84. child: buildGridView(),
  85. )
  86. ],
  87. ),
  88. ),
  89. );
  90. }
  91. }