paragraph_cache.dart 807 B

1234567891011121314151617181920212223242526272829303132333435
  1. import 'dart:ui';
  2. import 'package:flutter/widgets.dart';
  3. import 'package:quiver/collection.dart';
  4. class ParagraphCache {
  5. ParagraphCache(int maximumSize)
  6. : _cache = LruMap<int, Paragraph>(maximumSize: maximumSize);
  7. final LruMap<int, Paragraph> _cache;
  8. Paragraph? getLayoutFromCache(int key) {
  9. return _cache[key];
  10. }
  11. Paragraph performAndCacheLayout(String text, TextStyle style, int key) {
  12. final builder = ParagraphBuilder(style.getParagraphStyle());
  13. builder.pushStyle(style.getTextStyle());
  14. builder.addText(text);
  15. final paragraph = builder.build();
  16. paragraph.layout(ParagraphConstraints(width: double.infinity));
  17. _cache[key] = paragraph;
  18. return paragraph;
  19. }
  20. void clear() {
  21. _cache.clear();
  22. }
  23. int get length {
  24. return _cache.length;
  25. }
  26. }