location_analyse_util.dart 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import 'dart:math';
  2. import 'package:lottie/lottie.dart';
  3. class LocationAnalyseUtil {
  4. static LottieDelegates convertKeywordDelegates(List<String> list) {
  5. const int placeholderCount = 6;
  6. // 占位 keyPath 名称,例如 keyword1, keyword2, ..., keyword6
  7. final List<String> keyPaths =
  8. List.generate(placeholderCount, (index) => 'keyword${index + 1}');
  9. final List<String> finalTexts;
  10. if (list.length < placeholderCount) {
  11. // 不足 6 个,循环填充
  12. finalTexts =
  13. List.generate(placeholderCount, (index) => list[index % list.length]);
  14. } else {
  15. // 超过 6 个,随机取 6 个不重复
  16. final List<String> shuffled = List.from(list)..shuffle(Random());
  17. finalTexts = shuffled.take(placeholderCount).toList();
  18. }
  19. // 构建 Lottie 的 ValueDelegate 列表
  20. final List<ValueDelegate> valueDelegates =
  21. List.generate(placeholderCount, (index) {
  22. return ValueDelegate.text(
  23. [keyPaths[index], keyPaths[index]],
  24. value: finalTexts[index],
  25. );
  26. });
  27. // 更新绑定
  28. return LottieDelegates(values: valueDelegates);
  29. }
  30. }