Ingen beskrivning

main.dart 5.1KB

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