Brak opisu

main.dart 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. import 'package:flutter/cupertino.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:oktoast/oktoast.dart';
  4. import 'package:photo/photo.dart';
  5. import 'package:photo_manager/photo_manager.dart';
  6. import './preview.dart';
  7. import 'icon_text_button.dart';
  8. import 'picked_example.dart';
  9. void main() => runApp(MyApp());
  10. class MyApp extends StatelessWidget {
  11. // This widget is the root of your application.
  12. @override
  13. Widget build(BuildContext context) {
  14. return OKToast(
  15. child: MaterialApp(
  16. title: 'Pick Image Demo',
  17. theme: ThemeData(
  18. primaryColor: Color(0xFF01AAFF),
  19. // colorScheme: ColorScheme.light().copyWith(surface: Colors.white, onPrimary: Color(0xFFFFFFFF)),
  20. // appBarTheme: AppBarTheme(
  21. // color: Color(0xFFFFFFFF),
  22. // elevation: 0,
  23. // iconTheme: IconThemeData(
  24. // color: Color(0xFF595959),
  25. // ),
  26. // actionsIconTheme: IconThemeData(
  27. // color: Color(0xFF01AAFF),
  28. // ),
  29. // textTheme: TextTheme(
  30. // headline6: TextStyle(
  31. // fontSize: 18,
  32. // fontWeight: FontWeight.w500,
  33. // color: Color(0xFF595959),
  34. // ),
  35. // ),
  36. // ),
  37. // dividerColor: Color(0xFFF5F5F5),
  38. ),
  39. home: MyHomePage(title: 'Pick Image Demo'),
  40. ),
  41. );
  42. }
  43. }
  44. class MyHomePage extends StatefulWidget {
  45. MyHomePage({Key key, this.title}) : super(key: key);
  46. final String title;
  47. @override
  48. _MyHomePageState createState() => _MyHomePageState();
  49. }
  50. class _MyHomePageState extends State<MyHomePage> with LoadingDelegate {
  51. String currentSelected = "";
  52. @override
  53. Widget buildBigImageLoading(
  54. BuildContext context, AssetEntity entity, Color themeColor) {
  55. return Center(
  56. child: Container(
  57. width: 50.0,
  58. height: 50.0,
  59. child: CupertinoActivityIndicator(
  60. radius: 25.0,
  61. ),
  62. ),
  63. );
  64. }
  65. @override
  66. Widget buildPreviewLoading(
  67. BuildContext context, AssetEntity entity, Color themeColor) {
  68. return Center(
  69. child: Container(
  70. width: 50.0,
  71. height: 50.0,
  72. child: CupertinoActivityIndicator(
  73. radius: 25.0,
  74. ),
  75. ),
  76. );
  77. }
  78. @override
  79. Widget build(BuildContext context) {
  80. return Scaffold(
  81. appBar: AppBar(
  82. title: Text(widget.title),
  83. actions: <Widget>[
  84. FlatButton(
  85. child: Icon(Icons.image),
  86. onPressed: _testPhotoListParams,
  87. ),
  88. ],
  89. ),
  90. body: Container(
  91. child: SingleChildScrollView(
  92. child: Column(
  93. children: <Widget>[
  94. IconTextButton(
  95. icon: Icons.photo,
  96. text: "photo",
  97. onTap: () => _pickAsset(PickType.onlyImage),
  98. ),
  99. IconTextButton(
  100. icon: Icons.photo,
  101. text: "single photo",
  102. onTap: () => _pickAsset(PickType.onlyImage, single: true),
  103. ),
  104. IconTextButton(
  105. icon: Icons.videocam,
  106. text: "video",
  107. onTap: () => _pickAsset(PickType.onlyVideo),
  108. ),
  109. IconTextButton(
  110. icon: Icons.album,
  111. text: "all",
  112. onTap: () => _pickAsset(PickType.all),
  113. ),
  114. IconTextButton(
  115. icon: Icons.camera,
  116. text: "camera",
  117. onTap: () {
  118. PhotoPicker.pickCamera();
  119. },
  120. ),
  121. IconTextButton(
  122. icon: CupertinoIcons.reply_all,
  123. text: "Picked asset example.",
  124. onTap: () => routePage(PickedExample()),
  125. ),
  126. // Container(
  127. // height: 210,
  128. // child: PhotoPicker.buildGallery(
  129. // context: context,
  130. // padding: 4.0,
  131. // itemHeight: 210,
  132. // itemWidth: 180,
  133. // provider: I18nProvider.chinese,
  134. // sortDelegate: SortDelegate.common,
  135. // loadingDelegate: this,
  136. // pickType: PickType.onlyImage,
  137. // photoPathList: null,
  138. // onSelected: (List<AssetEntity> value) {
  139. // print(value);
  140. // }
  141. // ),
  142. // )
  143. ],
  144. ),
  145. ),
  146. ),
  147. floatingActionButton: FloatingActionButton(
  148. onPressed: () => _pickAsset(PickType.all),
  149. tooltip: 'pickImage',
  150. child: Icon(Icons.add),
  151. ),
  152. );
  153. }
  154. void _testPhotoListParams() async {
  155. var assetPathList =
  156. await PhotoManager.getAssetPathList(type: RequestType.image);
  157. _pickAsset(PickType.all, pathList: assetPathList);
  158. }
  159. void _pickAsset(PickType type, {List<AssetPathEntity> pathList, bool single = false}) async {
  160. /// context is required, other params is optional.
  161. /// context is required, other params is optional.
  162. /// context is required, other params is optional.
  163. List<AssetPathEntity> pathList2 = await PhotoManager.getAssetPathList(
  164. hasAll: true, type: RequestType.image,);
  165. if (!single) {
  166. PickedEntity entity = await PhotoPicker.pickAsset(
  167. context: context,
  168. padding: 4.0,
  169. itemRadio: 1,
  170. maxSelected: 9,
  171. provider: I18nProvider.english,
  172. rowCount: 4,
  173. thumbSize: 150,
  174. sortDelegate: SortDelegate.common,
  175. pickType: type,
  176. photoPathList: pathList2,
  177. );
  178. if (entity == null || entity.asset.isEmpty) {
  179. showToast("No pick item.");
  180. return;
  181. } else {
  182. List<String> r = [];
  183. for (var e in entity.asset) {
  184. var file = await e.file;
  185. r.add(file.absolute.path);
  186. }
  187. currentSelected = r.join("\n\n");
  188. List<AssetEntity> preview = [];
  189. preview.addAll(entity.asset);
  190. Navigator.push(context,
  191. MaterialPageRoute(builder: (_) => PreviewPage(list: preview)));
  192. }
  193. } else {
  194. final AssetEntity asset = await PhotoPicker.pickSingleAsset(
  195. context: context,
  196. padding: 4.0,
  197. itemRadio: 1,
  198. provider: I18nProvider.english,
  199. rowCount: 4,
  200. thumbSize: 150,
  201. sortDelegate: SortDelegate.common,
  202. pickType: type,
  203. photoPathList: pathList2,
  204. );
  205. print(asset);
  206. }
  207. setState(() {});
  208. }
  209. void routePage(Widget widget) {
  210. Navigator.push(
  211. context,
  212. MaterialPageRoute(
  213. builder: (BuildContext context) {
  214. return widget;
  215. },
  216. ),
  217. );
  218. }
  219. }