reader_page_agent.dart 869 B

1234567891011121314151617181920212223242526
  1. import 'package:flutter/material.dart';
  2. class ReaderPageAgent {
  3. static List<Map<String, int>> getPageOffsets(String content, double height, double width, double fontSize) {
  4. String tempStr = content;
  5. List<Map<String, int>> pageConfig = [];
  6. int last = 0;
  7. while (true) {
  8. Map<String, int> offset = {};
  9. offset['start'] = last;
  10. TextPainter textPainter = TextPainter(textDirection: TextDirection.ltr);
  11. textPainter.text = TextSpan(text: tempStr, style: TextStyle(fontSize: fontSize));
  12. textPainter.layout(maxWidth: width);
  13. var end = textPainter.getPositionForOffset(Offset(width, height)).offset;
  14. if (end == 0) {
  15. break;
  16. }
  17. tempStr = tempStr.substring(end, tempStr.length);
  18. offset['end'] = last + end;
  19. last = last + end;
  20. pageConfig.add(offset);
  21. }
  22. return pageConfig;
  23. }
  24. }