No Description

main.dart 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. primarySwatch: Colors.lime,
  19. ),
  20. home: MyHomePage(title: 'Pick Image Demo'),
  21. ),
  22. );
  23. }
  24. }
  25. class MyHomePage extends StatefulWidget {
  26. MyHomePage({Key key, this.title}) : super(key: key);
  27. final String title;
  28. @override
  29. _MyHomePageState createState() => _MyHomePageState();
  30. }
  31. class _MyHomePageState extends State<MyHomePage> with LoadingDelegate {
  32. String currentSelected = "";
  33. @override
  34. Widget buildBigImageLoading(
  35. BuildContext context, AssetEntity entity, Color themeColor) {
  36. return Center(
  37. child: Container(
  38. width: 50.0,
  39. height: 50.0,
  40. child: CupertinoActivityIndicator(
  41. radius: 25.0,
  42. ),
  43. ),
  44. );
  45. }
  46. @override
  47. Widget buildPreviewLoading(
  48. BuildContext context, AssetEntity entity, Color themeColor) {
  49. return Center(
  50. child: Container(
  51. width: 50.0,
  52. height: 50.0,
  53. child: CupertinoActivityIndicator(
  54. radius: 25.0,
  55. ),
  56. ),
  57. );
  58. }
  59. @override
  60. Widget build(BuildContext context) {
  61. return Scaffold(
  62. appBar: AppBar(
  63. title: Text(widget.title),
  64. actions: <Widget>[
  65. FlatButton(
  66. child: Icon(Icons.image),
  67. onPressed: _testPhotoListParams,
  68. ),
  69. ],
  70. ),
  71. body: Container(
  72. child: SingleChildScrollView(
  73. child: Column(
  74. children: <Widget>[
  75. IconTextButton(
  76. icon: Icons.photo,
  77. text: "photo",
  78. onTap: () => _pickAsset(PickType.onlyImage),
  79. ),
  80. IconTextButton(
  81. icon: Icons.videocam,
  82. text: "video",
  83. onTap: () => _pickAsset(PickType.onlyVideo),
  84. ),
  85. IconTextButton(
  86. icon: Icons.album,
  87. text: "all",
  88. onTap: () => _pickAsset(PickType.all),
  89. ),
  90. IconTextButton(
  91. icon: CupertinoIcons.reply_all,
  92. text: "Picked asset example.",
  93. onTap: () => routePage(PickedExample()),
  94. ),
  95. ],
  96. ),
  97. ),
  98. ),
  99. floatingActionButton: FloatingActionButton(
  100. onPressed: () => _pickAsset(PickType.all),
  101. tooltip: 'pickImage',
  102. child: Icon(Icons.add),
  103. ),
  104. );
  105. }
  106. void _testPhotoListParams() async {
  107. var assetPathList =
  108. await PhotoManager.getAssetPathList(type: RequestType.image);
  109. _pickAsset(PickType.all, pathList: assetPathList);
  110. }
  111. void _pickAsset(PickType type, {List<AssetPathEntity> pathList}) async {
  112. /// context is required, other params is optional.
  113. /// context is required, other params is optional.
  114. /// context is required, other params is optional.
  115. List<AssetEntity> imgList = await PhotoPicker.pickAsset(
  116. // BuildContext required
  117. context: context,
  118. /// The following are optional parameters.
  119. themeColor: Colors.green,
  120. // the title color and bottom color
  121. textColor: Colors.white,
  122. // text color
  123. padding: 1.0,
  124. // item padding
  125. dividerColor: Colors.grey,
  126. // divider color
  127. disableColor: Colors.grey.shade300,
  128. // the check box disable color
  129. itemRadio: 0.88,
  130. // the content item radio
  131. maxSelected: 8,
  132. // max picker image count
  133. // provider: I18nProvider.english,
  134. provider: I18nProvider.chinese,
  135. // i18n provider ,default is chinese. , you can custom I18nProvider or use ENProvider()
  136. rowCount: 3,
  137. // item row count
  138. thumbSize: 150,
  139. // preview thumb size , default is 64
  140. sortDelegate: SortDelegate.common,
  141. // default is common ,or you make custom delegate to sort your gallery
  142. checkBoxBuilderDelegate: DefaultCheckBoxBuilderDelegate(
  143. activeColor: Colors.white,
  144. unselectedColor: Colors.white,
  145. checkColor: Colors.green,
  146. ),
  147. // default is DefaultCheckBoxBuilderDelegate ,or you make custom delegate to create checkbox
  148. loadingDelegate: this,
  149. // if you want to build custom loading widget,extends LoadingDelegate, [see example/lib/main.dart]
  150. badgeDelegate: const DurationBadgeDelegate(),
  151. // badgeDelegate to show badge widget
  152. pickType: type,
  153. photoPathList: pathList,
  154. );
  155. if (imgList == null || imgList.isEmpty) {
  156. showToast("No pick item.");
  157. return;
  158. } else {
  159. List<String> r = [];
  160. for (var e in imgList) {
  161. var file = await e.file;
  162. r.add(file.absolute.path);
  163. }
  164. currentSelected = r.join("\n\n");
  165. List<AssetEntity> preview = [];
  166. preview.addAll(imgList);
  167. Navigator.push(context,
  168. MaterialPageRoute(builder: (_) => PreviewPage(list: preview)));
  169. }
  170. setState(() {});
  171. }
  172. void routePage(Widget widget) {
  173. Navigator.push(
  174. context,
  175. MaterialPageRoute(
  176. builder: (BuildContext context) {
  177. return widget;
  178. },
  179. ),
  180. );
  181. }
  182. }