Created
April 16, 2020 18:25
-
-
Save pedromassango/7fbbb26346af5636342a15723a0d6c5c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/cupertino.dart'; | |
import 'package:flutter/material.dart'; | |
import 'package:flutter/services.dart'; | |
import 'package:multi_image_picker/multi_image_picker.dart'; | |
import 'package:permission_handler/permission_handler.dart'; | |
void main() { | |
WidgetsFlutterBinding.ensureInitialized(); | |
SystemChrome.setEnabledSystemUIOverlays([ | |
SystemUiOverlay.bottom | |
]); | |
runApp(MyApp()); | |
} | |
class MyApp extends StatefulWidget { | |
@override | |
_MyAppState createState() => new _MyAppState(); | |
} | |
class _MyAppState extends State<MyApp> { | |
List<Asset> _images = List<Asset>(); | |
String _error = 'No Error Dectected'; | |
@override | |
void initState() { | |
super.initState(); | |
} | |
Widget buildGridView() { | |
return GridView.count( | |
crossAxisCount: 3, | |
children: List.generate(_images.length, (index) { | |
Asset asset = _images[index]; | |
return AssetThumb( | |
asset: asset, | |
width: 300, | |
height: 300, | |
); | |
}), | |
); | |
} | |
Future<void> loadAssets() async { | |
if (!await Permission.camera.request().isGranted) { | |
setState(() { | |
_error = "Permissão não garantida!"; | |
}); | |
return; | |
} | |
List<Asset> resultList = List<Asset>(); | |
String error = 'No Error Dectected'; | |
try { | |
resultList = await MultiImagePicker.pickImages( | |
maxImages: 10, | |
enableCamera: true, | |
selectedAssets: _images, | |
cupertinoOptions: CupertinoOptions(takePhotoIcon: "chat"), | |
materialOptions: MaterialOptions( | |
actionBarColor: "#abcdef", | |
actionBarTitle: "ImageX", | |
allViewTitle: "All Photos", | |
useDetailsView: false, | |
selectCircleStrokeColor: "#ffffff", | |
), | |
); | |
} on Exception catch (e) { | |
error = e.toString(); | |
} | |
// If the widget was removed from the tree while the asynchronous platform | |
// message was in flight, we want to discard the reply rather than calling | |
// setState to update our non-existent appearance. | |
if (!mounted) return; | |
setState(() { | |
_images = resultList; | |
_error = error; | |
}); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return new MaterialApp( | |
home: new Scaffold( | |
appBar: new AppBar( | |
title: const Text('MUlti Image Picker App'), | |
), | |
body: Column( | |
children: <Widget>[ | |
const SizedBox(height: 32,), | |
Center(child: Text('LOG: $_error', style: TextStyle(fontSize: 18),)), | |
const SizedBox(height: 32,), | |
RaisedButton( | |
child: Text("Pick images"), | |
onPressed: loadAssets, | |
), | |
Expanded( | |
child: buildGridView(), | |
) | |
], | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment