Hibok
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

93 wiersze
2.6 KiB

  1. import 'dart:typed_data';
  2. import 'package:chat/photo/engine/lru_cache.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:photo_manager/photo_manager.dart';
  5. class FirstPhotoItem extends StatefulWidget {
  6. final AssetPathEntity pathEntity;
  7. final double size;
  8. final Function onSelectFolder;
  9. const FirstPhotoItem(
  10. {Key key, this.pathEntity, this.size = 50, this.onSelectFolder})
  11. : super(key: key);
  12. @override
  13. _FirstPhotoItemState createState() => _FirstPhotoItemState();
  14. }
  15. class _FirstPhotoItemState extends State<FirstPhotoItem> {
  16. AssetEntity entity;
  17. @override
  18. void initState() {
  19. super.initState();
  20. getAssetEntity();
  21. }
  22. Widget _buildImg(Uint8List data) {
  23. return ClipRRect(
  24. borderRadius: BorderRadius.circular(8),
  25. child: Image.memory(
  26. data,
  27. width: widget.size,
  28. height: widget.size,
  29. fit: BoxFit.contain,
  30. ));
  31. }
  32. Future getAssetEntity() async {
  33. var entityList = await widget.pathEntity.getAssetListPaged(0, 1);
  34. setState(() {
  35. entity = entityList.first;
  36. });
  37. }
  38. @override
  39. Widget build(BuildContext context) {
  40. Widget leading;
  41. if (entity == null) {
  42. leading = Container(
  43. width: widget.size,
  44. height: widget.size,
  45. child: CircularProgressIndicator(),
  46. padding: EdgeInsets.all(8));
  47. } else {
  48. var thumb = ImageLruCache.getData(entity);
  49. if (thumb != null) {
  50. leading = _buildImg(thumb);
  51. } else {
  52. leading = FutureBuilder<Uint8List>(
  53. future: entity.thumbDataWithSize(
  54. widget.size.toInt(), widget.size.toInt()),
  55. builder: (BuildContext context, AsyncSnapshot<Uint8List> snapshot) {
  56. var futureData = snapshot.data;
  57. if (snapshot.connectionState == ConnectionState.done &&
  58. futureData != null) {
  59. ImageLruCache.setData(entity, widget.size.toInt(), futureData);
  60. return _buildImg(futureData);
  61. } else {
  62. return Center(
  63. child: Container(
  64. width: 30.0,
  65. height: 30.0,
  66. child: CircularProgressIndicator(
  67. valueColor: AlwaysStoppedAnimation(Colors.blueAccent),
  68. ),
  69. ),
  70. );
  71. }
  72. });
  73. }
  74. }
  75. return ListTile(
  76. leading: leading,
  77. title: Text(widget.pathEntity.name),
  78. subtitle: Text('${widget.pathEntity.assetCount}'),
  79. onTap:widget.onSelectFolder);
  80. }
  81. }