import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:oktoast/oktoast.dart'; import 'package:photo/photo.dart'; import 'package:photo_manager/photo_manager.dart'; import './preview.dart'; import 'icon_text_button.dart'; import 'picked_example.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { // This widget is the root of your application. @override Widget build(BuildContext context) { return OKToast( child: MaterialApp( title: 'Pick Image Demo', theme: ThemeData( primaryColor: Color(0xFF01AAFF), // colorScheme: ColorScheme.light().copyWith(surface: Colors.white, onPrimary: Color(0xFFFFFFFF)), // appBarTheme: AppBarTheme( // color: Color(0xFFFFFFFF), // elevation: 0, // iconTheme: IconThemeData( // color: Color(0xFF595959), // ), // actionsIconTheme: IconThemeData( // color: Color(0xFF01AAFF), // ), // textTheme: TextTheme( // headline6: TextStyle( // fontSize: 18, // fontWeight: FontWeight.w500, // color: Color(0xFF595959), // ), // ), // ), // dividerColor: Color(0xFFF5F5F5), ), home: MyHomePage(title: 'Pick Image Demo'), ), ); } } class MyHomePage extends StatefulWidget { MyHomePage({Key key, this.title}) : super(key: key); final String title; @override _MyHomePageState createState() => _MyHomePageState(); } class _MyHomePageState extends State with LoadingDelegate { String currentSelected = ""; @override Widget buildBigImageLoading( BuildContext context, AssetEntity entity, Color themeColor) { return Center( child: Container( width: 50.0, height: 50.0, child: CupertinoActivityIndicator( radius: 25.0, ), ), ); } @override Widget buildPreviewLoading( BuildContext context, AssetEntity entity, Color themeColor) { return Center( child: Container( width: 50.0, height: 50.0, child: CupertinoActivityIndicator( radius: 25.0, ), ), ); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text(widget.title), actions: [ FlatButton( child: Icon(Icons.image), onPressed: _testPhotoListParams, ), ], ), body: Container( child: SingleChildScrollView( child: Column( children: [ IconTextButton( icon: Icons.photo, text: "photo", onTap: () => _pickAsset(PickType.onlyImage), ), IconTextButton( icon: Icons.photo, text: "single photo", onTap: () => _pickAsset(PickType.onlyImage, single: true), ), IconTextButton( icon: Icons.videocam, text: "video", onTap: () => _pickAsset(PickType.onlyVideo), ), IconTextButton( icon: Icons.album, text: "all", onTap: () => _pickAsset(PickType.all), ), IconTextButton( icon: Icons.camera, text: "camera", onTap: () { PhotoPicker.pickCamera(); }, ), IconTextButton( icon: CupertinoIcons.reply_all, text: "Picked asset example.", onTap: () => routePage(PickedExample()), ), // Container( // height: 210, // child: PhotoPicker.buildGallery( // context: context, // padding: 4.0, // itemHeight: 210, // itemWidth: 180, // provider: I18nProvider.chinese, // sortDelegate: SortDelegate.common, // loadingDelegate: this, // pickType: PickType.onlyImage, // photoPathList: null, // onSelected: (List value) { // print(value); // } // ), // ) ], ), ), ), floatingActionButton: FloatingActionButton( onPressed: () => _pickAsset(PickType.all), tooltip: 'pickImage', child: Icon(Icons.add), ), ); } void _testPhotoListParams() async { var assetPathList = await PhotoManager.getAssetPathList(type: RequestType.image); _pickAsset(PickType.all, pathList: assetPathList); } void _pickAsset(PickType type, {List pathList, bool single = false}) async { /// context is required, other params is optional. /// context is required, other params is optional. /// context is required, other params is optional. List pathList2 = await PhotoManager.getAssetPathList( hasAll: true, type: RequestType.image,); if (!single) { PickedEntity entity = await PhotoPicker.pickAsset( context: context, padding: 4.0, itemRadio: 1, maxSelected: 9, provider: I18nProvider.english, rowCount: 4, thumbSize: 150, sortDelegate: SortDelegate.common, pickType: type, photoPathList: pathList2, ); if (entity == null || entity.asset.isEmpty) { showToast("No pick item."); return; } else { List r = []; for (var e in entity.asset) { var file = await e.file; r.add(file.absolute.path); } currentSelected = r.join("\n\n"); List preview = []; preview.addAll(entity.asset); Navigator.push(context, MaterialPageRoute(builder: (_) => PreviewPage(list: preview))); } } else { final AssetEntity asset = await PhotoPicker.pickSingleAsset( context: context, padding: 4.0, itemRadio: 1, provider: I18nProvider.english, rowCount: 4, thumbSize: 150, sortDelegate: SortDelegate.common, pickType: type, photoPathList: pathList2, ); print(asset); } setState(() {}); } void routePage(Widget widget) { Navigator.push( context, MaterialPageRoute( builder: (BuildContext context) { return widget; }, ), ); } }