暂无描述

main.dart 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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.videocam,
  101. text: "video",
  102. onTap: () => _pickAsset(PickType.onlyVideo),
  103. ),
  104. IconTextButton(
  105. icon: Icons.album,
  106. text: "all",
  107. onTap: () => _pickAsset(PickType.all),
  108. ),
  109. IconTextButton(
  110. icon: Icons.camera,
  111. text: "camera",
  112. onTap: () {
  113. PhotoPicker.pickCamera();
  114. },
  115. ),
  116. IconTextButton(
  117. icon: CupertinoIcons.reply_all,
  118. text: "Picked asset example.",
  119. onTap: () => routePage(PickedExample()),
  120. ),
  121. // Container(
  122. // height: 210,
  123. // child: PhotoPicker.buildGallery(
  124. // context: context,
  125. // padding: 4.0,
  126. // itemHeight: 210,
  127. // itemWidth: 180,
  128. // provider: I18nProvider.chinese,
  129. // sortDelegate: SortDelegate.common,
  130. // loadingDelegate: this,
  131. // pickType: PickType.onlyImage,
  132. // photoPathList: null,
  133. // onSelected: (List<AssetEntity> value) {
  134. // print(value);
  135. // }
  136. // ),
  137. // )
  138. ],
  139. ),
  140. ),
  141. ),
  142. floatingActionButton: FloatingActionButton(
  143. onPressed: () => _pickAsset(PickType.all),
  144. tooltip: 'pickImage',
  145. child: Icon(Icons.add),
  146. ),
  147. );
  148. }
  149. void _testPhotoListParams() async {
  150. var assetPathList =
  151. await PhotoManager.getAssetPathList(type: RequestType.image);
  152. _pickAsset(PickType.all, pathList: assetPathList);
  153. }
  154. void _pickAsset(PickType type, {List<AssetPathEntity> pathList}) async {
  155. /// context is required, other params is optional.
  156. /// context is required, other params is optional.
  157. /// context is required, other params is optional.
  158. List<AssetPathEntity> pathList2 = await PhotoManager.getAssetPathList(
  159. hasAll: true, type: RequestType.image,);
  160. PickedEntity entity = await PhotoPicker.pickAsset(
  161. context: context,
  162. padding: 4.0,
  163. itemRadio: 1,
  164. maxSelected: 2,
  165. provider: I18nProvider.english,
  166. rowCount: 4,
  167. thumbSize: 150,
  168. sortDelegate: SortDelegate.common,
  169. pickType: PickType.onlyVideo,
  170. photoPathList: pathList2,
  171. );
  172. if (entity == null || entity.asset.isEmpty) {
  173. showToast("No pick item.");
  174. return;
  175. } else {
  176. List<String> r = [];
  177. for (var e in entity.asset) {
  178. var file = await e.file;
  179. r.add(file.absolute.path);
  180. }
  181. currentSelected = r.join("\n\n");
  182. List<AssetEntity> preview = [];
  183. preview.addAll(entity.asset);
  184. Navigator.push(context,
  185. MaterialPageRoute(builder: (_) => PreviewPage(list: preview)));
  186. }
  187. setState(() {});
  188. }
  189. void routePage(Widget widget) {
  190. Navigator.push(
  191. context,
  192. MaterialPageRoute(
  193. builder: (BuildContext context) {
  194. return widget;
  195. },
  196. ),
  197. );
  198. }
  199. }