当前位置: 首页 > news >正文

flutter开发实战-第一帧布局完成回调实现

flutter开发实战-第一帧布局完成回调实现
在这里插入图片描述
在开发中,我们有时候需要在第一帧布局完成后调用一些相关的方法。这里记录一下是实现过程。

Flutter中有多种不同的Binding,每种Binding都负责不同的功能。下面是Flutter中常见的Binding:
这里简单说明一下WidgetsBinding

一、WidgetsBinding

WidgetsBinding:负责管理Flutter应用程序的生命周期,包括启动、暂停、恢复和停止等。
WidgetsBinding它用于监听用户设置的更改,如语言的修改。 不仅如此, WidgetsBinding 否是 Widgets 与 Flutter 引擎之间通信的桥梁,有两个主要的功能:
  * 1 负责处理Widgets结构变更的过程;
  * 2 第二个是触发渲染事件。
一些小组件的结构更改是 BuildOwner 来完成的,它跟踪需要重建的小部件,并处理应用于整个小部件结构的其他任务。

二、实现第一帧布局完成后调用相关方法

在WidgetsBinding中,我们可以看到endOfFrame方法,源码如下

/// Returns a Future that completes after the frame completes.////// If this is called between frames, a frame is immediately scheduled if/// necessary. If this is called during a frame, the Future completes after/// the current frame.////// If the device's screen is currently turned off, this may wait a very long/// time, since frames are not scheduled while the device's screen is turned/// off.Future<void> get endOfFrame {if (_nextFrameCompleter == null) {if (schedulerPhase == SchedulerPhase.idle) {scheduleFrame();}_nextFrameCompleter = Completer<void>();addPostFrameCallback((Duration timeStamp) {_nextFrameCompleter!.complete();_nextFrameCompleter = null;});}return _nextFrameCompleter!.future;}

方法中描述如下

该方法返回在帧完成后完成的Future。
如果在帧之前调用的时候,则会立即调度帧。如果在帧期间调用此操作,则Future将在当前帧完成后调用。
如果设备的屏幕当前已关闭,这可能会等待很长时间。

所以我们需要在initState中调用相关方法

WidgetsBinding.instance.endOfFrame.then((value) {if (mounted) {// TODO调用相关方法}},);

实现第一帧布局完成后调用完成代码如下

class AfterLayoutPage extends StatefulWidget {const AfterLayoutPage({super.key});@overrideState<AfterLayoutPage> createState() => _AfterLayoutPageState();
}class _AfterLayoutPageState extends State<AfterLayoutPage> {@overrideWidget build(BuildContext context) {return Scaffold(appBar: AppBar(title: const Text('AfterLayoutPage'),),body: Container(color: Colors.blueGrey,),);}@overridevoid initState() {// TODO: implement initStatesuper.initState();WidgetsBinding.instance.endOfFrame.then((value) {if (mounted) {showHelloWorld();}},);}void showHelloWorld() {showDialog(context: context,builder: (BuildContext context) {return AlertDialog(content: const Text('Hello World'),actions: <Widget>[TextButton(onPressed: () => Navigator.of(context).pop(),child: const Text('DISMISS'),)],);},);}
}

可以将该实现包装成一个Mixin

import 'dart:async';import 'package:flutter/widgets.dart';mixin AfterLayoutMixin<T extends StatefulWidget> on State<T> {@overridevoid initState() {super.initState();WidgetsBinding.instance.endOfFrame.then((_) {if (mounted) afterFirstLayout(context);},);}FutureOr<void> afterFirstLayout(BuildContext context);
}

调整后代码如下

class AfterLayoutPage extends StatefulWidget {const AfterLayoutPage({super.key});@overrideState<AfterLayoutPage> createState() => _AfterLayoutPageState();
}class _AfterLayoutPageState extends State<AfterLayoutPage> with AfterLayoutMixin<AfterLayoutPage> {@overrideWidget build(BuildContext context) {return Scaffold(appBar: AppBar(title: const Text('AfterLayoutPage'),),body: Container(color: Colors.blueGrey,),);}@overridevoid afterFirstLayout(BuildContext context) {// Calling the same function "after layout" to resolve the issue.showHelloWorld();}void showHelloWorld() {showDialog(context: context,builder: (BuildContext context) {return AlertDialog(content: const Text('Hello World'),actions: <Widget>[TextButton(onPressed: () => Navigator.of(context).pop(),child: const Text('DISMISS'),)],);},);}
}

三、小结

flutter开发实战-第一帧布局完成回调实现

学习记录,每天不停进步。

http://www.lryc.cn/news/265246.html

相关文章:

  • Windows11编译VTM源码生成Visual Studio 工程
  • [数据结构进阶 C++] 二叉搜索树(BinarySearchTree)的模拟实现
  • PostGIS学习教程十四:更多的空间连接
  • 【爬虫软件】孔夫子二手书采集
  • P8736 [蓝桥杯 2020 国 B] 游园安排
  • 初识Docker-什么是docker
  • maven的pom.xml设置本地仓库
  • Qt获取屏幕DPI缩放比
  • Spring MVC控制层框架
  • vmware安装银河麒麟V10高级服务器操作系统
  • 掌握Jenknis基础概念
  • AWS 知识二:AWS同一个VPC下的ubuntu实例通过ldapsearch命令查询目录用户信息
  • Ubuntu 常用命令之 fdisk 命令用法介绍
  • 论文中公式怎么降重 papergpt
  • 27. 过滤器
  • 做一个wiki页面是体验HTML语义的好方法
  • 金融CRM有用吗?金融行业CRM有哪些功能
  • @XmlAccessorType+@XmlElement完美解决Java类到XML映射问题
  • 软件渗透测试有哪些测试流程?权威安全测试报告的重要性
  • 安防视频融合云平台/智慧监控平台EasyCVR如何添加验证码调用接口?
  • 浏览器输入一个url,它的解析过程
  • 第29节: Vue3 列表渲染
  • CloudPulse:一款针对AWS云环境的SSL证书搜索与分析引擎
  • 【网络安全】学习Web安全必须知道的一本书
  • 千帆 AppBuilder 初体验,不仅解决解决了我筛选简历的痛苦,更是让提效10倍!
  • Ubuntu 常用命令之 cal 命令用法介绍
  • 项目中webpack优化配置(1)
  • 【Qt之Quick模块】5. QML基本类型及示例用法
  • MySQL运维实战(1.2)安装部署:使用二进制安装部署
  • ChatGPT一周年:开源语言大模型的冲击