Brak opisu

asset_image.dart 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import 'dart:typed_data';
  2. import 'package:flutter/material.dart';
  3. import 'package:photo_manager/photo_manager.dart';
  4. class AssetImageWidget extends StatelessWidget {
  5. final AssetEntity assetEntity;
  6. final double width;
  7. final double height;
  8. final BoxFit boxFit;
  9. const AssetImageWidget({
  10. Key key,
  11. @required this.assetEntity,
  12. this.width,
  13. this.height,
  14. this.boxFit,
  15. }) : super(key: key);
  16. @override
  17. Widget build(BuildContext context) {
  18. if (assetEntity == null) {
  19. return _buildContainer();
  20. }
  21. return FutureBuilder<Size>(
  22. builder: (c, s) {
  23. if (!s.hasData) {
  24. return Container();
  25. }
  26. var size = s.data;
  27. return FutureBuilder<Uint8List>(
  28. builder: (BuildContext context, snapshot) {
  29. if (snapshot.hasData) {
  30. return _buildContainer(
  31. child: Image.memory(
  32. snapshot.data,
  33. width: width,
  34. height: height,
  35. fit: boxFit,
  36. ),
  37. );
  38. } else {
  39. return _buildContainer();
  40. }
  41. },
  42. future: assetEntity.thumbDataWithSize(
  43. size.width.toInt(),
  44. size.height.toInt(),
  45. ),
  46. );
  47. },
  48. future: assetEntity.size,
  49. );
  50. }
  51. Widget _buildContainer({Widget child}) {
  52. child ??= Container();
  53. return Container(
  54. width: width,
  55. height: height,
  56. child: child,
  57. );
  58. }
  59. }