No Description

main.dart 5.3KB

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