本文将带您了解在 Flutter中使用Stream.periodic的完整示例
该Stream.periodic构造,顾名思义,是用来创建流,在周期间隔反复广播事件。简单用法:

final Stream _myStream =Stream.periodic(const Duration(seconds: 1), (int count) {// Do something and return something here
});

您可以在文档中找到有关Stream.periodic 的更多信息。但是,如果您觉得单词太无聊和令人困惑,并且只想深入研究代码,请继续阅读下面的示例。

我们将要构建的应用程序的背景颜色会随着时间而变化。它还在屏幕中央显示递增的数字。我们可以通过按下浮动按钮来阻止这些无情的行为。

这是它的工作原理:

main.dart 中的完整源代码和解释:

import 'package:flutter/material.dart';
import 'dart:async';
import 'dart:math';void main() {runApp(const MyApp());
}class MyApp extends StatelessWidget {const MyApp({Key? key}) : super(key: key);@overrideWidget build(BuildContext context) {return MaterialApp(// Hide the debug bannerdebugShowCheckedModeBanner: false,title: 'KindaCode.com',theme: ThemeData(primarySwatch: Colors.indigo,),home: const HomeScreen(),);}
}class HomeScreen extends StatefulWidget {const HomeScreen({Key? key}) : super(key: key);@overrideState<HomeScreen> createState() => _HomeScreenState();
}class _HomeScreenState extends State<HomeScreen> {final Stream _myStream =Stream.periodic(const Duration(seconds: 1), (int count) {return count;});// The subscription on events from _myStreamlate StreamSubscription _sub;// This number will be displayed in the center of the screen// It changes over timeint _computationCount = 0;// Background color// In the beginning, it's indigo but it will be a random color laterColor _bgColor = Colors.indigo;@overridevoid initState() {_sub = _myStream.listen((event) {setState(() {_computationCount = event;// Set the background color to a random color_bgColor = Colors.primaries[Random().nextInt(Colors.primaries.length)];});});super.initState();}@overrideWidget build(BuildContext context) {return Scaffold(backgroundColor: _bgColor,appBar: AppBar(title: const Text('Lucklyの博客'),backgroundColor: Colors.transparent,),body: Center(child: Text(_computationCount.toString(),style: const TextStyle(fontSize: 150, color: Colors.white),),),// This button is used to unsubscribe the stream listenerfloatingActionButton: FloatingActionButton(child: const Icon(Icons.stop,size: 30,),onPressed: () => _sub.cancel(),),);}// Cancel the stream listener on dispose@overridevoid dispose() {_sub.cancel();super.dispose();}
}

结论

我们已经研究了在 Flutter中实现Stream.periodic的实际示例。如果您想了解更多关于流,类似的事情流,请继续关注我!

Flutter:Stream.periodic 示例相关推荐

  1. JDK 8 之 Stream sorted() 示例

    原文链接:http://www.concretepage.com/java/jdk-8/java-8-stream-sorted-example 国外对Java8一系列总结的不错, 翻译过来给大家共享 ...

  2. 【Flutter】Flutter Gallery 官方示例简介 ( 学习示例 | 邮件应用 | 零售应用 | 理财应用 | 旅行应用 | 新闻应用 | 自适应布局应用 )

    文章目录 一.Reply 邮件应用 二.Shrine 零售应用 三.Rally 理财应用 四.Crane 旅行应用 五.Fortnightly 新闻应用 六.Starter 自适应布局应用 Flutt ...

  3. 【Flutter】Flutter Gallery 官方示例简介 ( 项目简介 | 工程构建 )

    文章目录 一.Flutter Gallery 简介 二.Flutter Gallery 项目构建 发现一个很强的 Flutter 开源项目 , 由 Flutter 官方提供的 Flutter Gall ...

  4. 什么是价值流图 (Value Stream)?示例汇总

    价值流图(VSM)是一种精益制造技术,用于分析,设计和管理将产品带给客户所需的材料和信息流.它使用标准符号系统来描述各种工作流和信息流.项目被映射为添加值或不从客户的角度添加值,目的是根除不增加价值的 ...

  5. 【Flutter】Flutter 拍照示例 ( Flutter 插件配置 | Flutter 插件源码示例 | iOS 应用配置 | Android 应用配置 )

    文章目录 一.Flutter 插件配置 二.Flutter 插件源码示例 三.iOS 应用配置 四.Android 应用配置 五.相关资源 一.Flutter 插件配置 Flutter 拍照示例中 , ...

  6. Java 8 Stream.distinct() 列表去重示例

    Java 8 Stream.distinct() 示例 在这篇文章里,我们将提供Java8 Stream distinct()示例. distinct()返回由该流的不同元素组成的流.distinct ...

  7. flutter怎么手动刷新_如何手动刷新或重新加载Flutter Firestore StreamBuilder?

    I have this application that needs a pull to refresh functionality, so I placed the StreamBuilder Wi ...

  8. 写给前端工程师的 Flutter 详细教程

    本文作者:hicc,腾讯 CSIG 前端开发工程师 最爱折腾的就是前端工程师了,从 jQuery 折腾到 AngularJs,再折腾到 Vue.React.最爱跨屏的也是前端工程师,从 phonega ...

  9. Flutter——实现网易云音乐的渐进式卡片切换

    介绍 此功能为 仿网易云音乐App的一部分 : 仿网易云音乐App 预览图 我们可以看到页面下方切换的卡片效果 分析 首先动画以 x轴分为两部分 : 左侧文字 和 右侧图片 右侧图片以 z轴 分为 : ...

最新文章

  1. 1078 Hashing
  2. 【linux系统】IK分词器安装
  3. python数据库哪个好_终于明了python用什么数据库好
  4. UVA 536——Tree Recovery
  5. flowable 集成mongodb
  6. python做炫酷的界面_用python打造可视化爬虫监控系统,酷炫的图形化界面
  7. 最新公布,“中国开发者大调查”第四批中奖名单来啦
  8. PHP函数func_get_args(),func_get_arg(),func_num_args()
  9. [解答]对‘’未定义的引用 collect2: 错误: ld 返回 1
  10. Android 中多点触摸协议
  11. 2021年高处安装、维护、拆除复审考试及高处安装、维护、拆除模拟考试题
  12. 网站SQL注入漏洞检测
  13. 天津等保测评机构项目测评收费价格标准参考
  14. uniapp上班考勤打卡情况日历展示
  15. bootstrap-datepicker属性和方法
  16. 把华为交换机设置成(NTP)时钟源服务器
  17. PHP根据年月获取月初月末时间
  18. 适配USB接口奕力 ILITEK Multi-Touch-V5000
  19. 2022年IT服务行业研究报告
  20. [ZT]网站十种常见盈利模式简介

热门文章

  1. node和java性能_服务端I/O性能大比拼:Node、PHP、Java和Go(二)
  2. win101909要不要更新_win10更新好还是不更新_win10更新有什么用
  3. python的for语句是否只有一种写法_跟老齐学Python之for循环语句
  4. 双稳态电路的两个稳定状态是什么_干货|常见的脉冲电路到底有何用途和特点?终于了解了!|脉冲|晶体管|双稳|单稳|振荡器...
  5. 做了个EMMC分析治具
  6. python3 next_对Python 3.2 迭代器的next函数实例讲解
  7. java本地读取文件的io类_Java File类与文件IO流总结
  8. Transformer architecture的解释
  9. 2016计算机课程设计,2016年计算机组成原理课程设计-硬布线控制器的设计.doc
  10. js 数据类型_js中检测数据类型的方法汇总