No Description

main.dart 5.3KB

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