Sin descripción

main.dart 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 = await PhotoManager.getImageAsset();
  108. _pickAsset(PickType.all, pathList: assetPathList);
  109. }
  110. void _pickAsset(PickType type, {List<AssetPathEntity> pathList}) async {
  111. /// context is required, other params is optional.
  112. /// context is required, other params is optional.
  113. /// context is required, other params is optional.
  114. List<AssetEntity> imgList = await PhotoPicker.pickAsset(
  115. // BuildContext required
  116. context: context,
  117. /// The following are optional parameters.
  118. themeColor: Colors.green,
  119. // the title color and bottom color
  120. textColor: Colors.white,
  121. // text color
  122. padding: 1.0,
  123. // item padding
  124. dividerColor: Colors.grey,
  125. // divider color
  126. disableColor: Colors.grey.shade300,
  127. // the check box disable color
  128. itemRadio: 0.88,
  129. // the content item radio
  130. maxSelected: 8,
  131. // max picker image count
  132. // provider: I18nProvider.english,
  133. provider: I18nProvider.chinese,
  134. // i18n provider ,default is chinese. , you can custom I18nProvider or use ENProvider()
  135. rowCount: 3,
  136. // item row count
  137. thumbSize: 150,
  138. // preview thumb size , default is 64
  139. sortDelegate: SortDelegate.common,
  140. // default is common ,or you make custom delegate to sort your gallery
  141. checkBoxBuilderDelegate: DefaultCheckBoxBuilderDelegate(
  142. activeColor: Colors.white,
  143. unselectedColor: Colors.white,
  144. checkColor: Colors.green,
  145. ),
  146. // default is DefaultCheckBoxBuilderDelegate ,or you make custom delegate to create checkbox
  147. loadingDelegate: this,
  148. // if you want to build custom loading widget,extends LoadingDelegate, [see example/lib/main.dart]
  149. badgeDelegate: const DurationBadgeDelegate(),
  150. // badgeDelegate to show badge widget
  151. pickType: type,
  152. photoPathList: pathList,
  153. );
  154. if (imgList == null || imgList.isEmpty) {
  155. showToast("No pick item.");
  156. } else {
  157. List<String> r = [];
  158. for (var e in imgList) {
  159. var file = await e.file;
  160. r.add(file.absolute.path);
  161. }
  162. currentSelected = r.join("\n\n");
  163. List<AssetEntity> preview = [];
  164. preview.addAll(imgList);
  165. Navigator.push(context,
  166. MaterialPageRoute(builder: (_) => PreviewPage(list: preview)));
  167. }
  168. setState(() {});
  169. }
  170. void routePage(Widget widget) {
  171. Navigator.push(
  172. context,
  173. MaterialPageRoute(
  174. builder: (BuildContext context) {
  175. return widget;
  176. },
  177. ),
  178. );
  179. }
  180. }