You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter实现每日随机展示单个列表项的方法

实现Flutter每日随机展示列表项的方案

核心思路是通过SharedPreferences缓存当日日期和对应的随机项,每次启动时先校验当前日期与缓存日期是否一致:一致则复用缓存的内容,不一致则重新随机并更新缓存。

步骤1:添加依赖

在pubspec.yaml中添加shared_preferences依赖:

dependencies:
  flutter:
    sdk: flutter
  shared_preferences: ^2.2.2 # 可替换为最新版本

步骤2:封装核心逻辑

创建管理类封装日期校验、随机选择和缓存操作:

import 'package:shared_preferences/shared_preferences.dart';
import 'dart:math';

class DailyRandomItemManager {
  final List<String> items;
  static const _cacheDateKey = 'daily_random_date';
  static const _cacheItemKey = 'daily_random_item';

  DailyRandomItemManager(this.items);

  Future<String> getTodayItem() async {
    // 处理空列表情况
    if (items.isEmpty) return '无可用内容';

    final prefs = await SharedPreferences.getInstance();
    // 格式化当日日期(精确到日,忽略时分秒)
    final now = DateTime.now();
    final today = '${now.year}-${now.month.toString().padLeft(2, '0')}-${now.day.toString().padLeft(2, '0')}';
    
    final savedDate = prefs.getString(_cacheDateKey);
    final savedItem = prefs.getString(_cacheItemKey);

    // 日期匹配则返回缓存内容
    if (savedDate == today && savedItem != null) {
      return savedItem;
    }

    // 重新随机选择并更新缓存
    final randomIndex = Random().nextInt(items.length);
    final newItem = items[randomIndex];
    await prefs.setString(_cacheDateKey, today);
    await prefs.setString(_cacheItemKey, newItem);
    return newItem;
  }
}

步骤3:在Widget中使用

在页面中调用管理类获取当日内容:

import 'package:flutter/material.dart';

class DailyRandomScreen extends StatefulWidget {
  // 传入你的String列表
  final List<String> contentList;

  const DailyRandomScreen({super.key, required this.contentList});

  @override
  State<DailyRandomScreen> createState() => _DailyRandomScreenState();
}

class _DailyRandomScreenState extends State<DailyRandomScreen> {
  String? _todayContent;
  bool _isLoading = true;

  @override
  void initState() {
    super.initState();
    _loadDailyContent();
  }

  Future<void> _loadDailyContent() async {
    final manager = DailyRandomItemManager(widget.contentList);
    final content = await manager.getTodayItem();
    setState(() {
      _todayContent = content;
      _isLoading = false;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('每日随机内容')),
      body: Center(
        child: _isLoading
            ? const CircularProgressIndicator()
            : Text(
                _todayContent!,
                style: const TextStyle(fontSize: 24, fontWeight: FontWeight.w500),
                textAlign: TextAlign.center,
              ),
      ),
    );
  }
}

关键注意事项

  • 日期格式化必须精确到年-月-日,如果包含时分秒,会导致每次打开APP都重新随机。
  • 确保传入的列表非空,可根据需求在管理类中添加空列表的处理逻辑(比如返回默认提示或抛出异常)。
  • Random().nextInt(items.length)会生成0到items.length-1的随机整数,不会出现数组越界问题。

内容的提问来源于stack exchange,提问作者Elijah Loola

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 08:03:17