No Description

main.dart 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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(BuildContext context, AssetEntity entity, Color themeColor) {
  80. return Center(
  81. child: Container(
  82. width: 50.0,
  83. height: 50.0,
  84. child: CupertinoActivityIndicator(
  85. radius: 25.0,
  86. ),
  87. ),
  88. );
  89. }
  90. @override
  91. Widget buildPreviewLoading(BuildContext context, AssetEntity entity, Color themeColor) {
  92. return Center(
  93. child: Container(
  94. width: 50.0,
  95. height: 50.0,
  96. child: CupertinoActivityIndicator(
  97. radius: 25.0,
  98. ),
  99. ),
  100. );
  101. }
  102. @override
  103. Widget build(BuildContext context) {
  104. return new Scaffold(
  105. appBar: new AppBar(
  106. title: new Text(widget.title),
  107. ),
  108. body: Container(
  109. child: SingleChildScrollView(
  110. child: Column(
  111. children: <Widget>[
  112. IconTextButton(icon: Icons.photo, text: "photo", onTap: () => _pickAsset(PickType.onlyImage)),
  113. IconTextButton(icon: Icons.videocam, text: "video", onTap: () => _pickAsset(PickType.onlyVideo)),
  114. IconTextButton(icon: Icons.album, text: "all", onTap: () => _pickAsset(PickType.all)),
  115. Text(
  116. '$currentSelected',
  117. textAlign: TextAlign.center,
  118. ),
  119. ],
  120. ),
  121. ),
  122. ),
  123. floatingActionButton: new FloatingActionButton(
  124. onPressed: () => _pickAsset(PickType.all),
  125. tooltip: 'pickImage',
  126. child: new Icon(Icons.add),
  127. ),
  128. );
  129. }
  130. }
  131. class IconTextButton extends StatelessWidget {
  132. final IconData icon;
  133. final String text;
  134. final Function onTap;
  135. const IconTextButton({Key key, this.icon, this.text, this.onTap}) : super(key: key);
  136. @override
  137. Widget build(BuildContext context) {
  138. return InkWell(
  139. onTap: onTap,
  140. child: Container(
  141. child: ListTile(
  142. leading: Icon(icon ?? Icons.device_unknown),
  143. title: Text(text ?? ""),
  144. ),
  145. ),
  146. );
  147. }
  148. }