暂无描述

main.dart 5.4KB

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