image_cache.dart 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import 'package:flutter/material.dart';
  2. import '../utils/utils.dart';
  3. class ShowImageCache extends StatefulWidget {
  4. const ShowImageCache({Key? key}) : super(key: key);
  5. @override
  6. _ShowImageCacheState createState() => _ShowImageCacheState();
  7. }
  8. class _ShowImageCacheState extends State<ShowImageCache> {
  9. ImageCache? get imageCache => _imageCache;
  10. ImageCache? _imageCache = PaintingBinding.instance?.imageCache;
  11. @override
  12. void initState() {
  13. super.initState();
  14. _imageCache = PaintingBinding.instance?.imageCache;
  15. }
  16. void refresh() {
  17. setState(() {});
  18. }
  19. @override
  20. Widget build(BuildContext context) {
  21. return Column(mainAxisAlignment: MainAxisAlignment.center, children: [
  22. const SizedBox(height: 8),
  23. ElevatedButton(onPressed: refresh, child: const Text('刷新缓存信息')),
  24. const SizedBox(height: 8),
  25. Text(
  26. '当前图像缓存数量: ${imageCache?.currentSize ?? 0} 大小: ${imageCache?.currentSizeBytes ?? 0}bytes = ${byte2MB(imageCache?.currentSizeBytes ?? 0)}MB',
  27. style: const TextStyle(fontSize: 12),
  28. ),
  29. const SizedBox(height: 8),
  30. ]);
  31. }
  32. }
  33. // ImageCache? get imageCache => PaintingBinding.instance?.imageCache;
  34. // @override
  35. // Widget build(BuildContext context) {
  36. // return Column(mainAxisAlignment: MainAxisAlignment.center, children: [
  37. // const SizedBox(height: 8),
  38. // Text(
  39. // '当前图像缓存数量: ${imageCache?.currentSize ?? 0}',
  40. // style: const TextStyle(fontSize: 12),
  41. // ),
  42. // const SizedBox(height: 8),
  43. // Text(
  44. // '当前图像缓存大小: ${imageCache?.currentSizeBytes ?? 0}bytes = ${byte2MB(imageCache?.currentSizeBytes ?? 0)}MB',
  45. // style: const TextStyle(fontSize: 12),
  46. // ),
  47. // const SizedBox(height: 8),
  48. // ]);
  49. // }