Brak opisu

main.dart 5.8KB

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