Sin descripción

main.dart 5.5KB

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