No Description

main.dart 4.7KB

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