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

网站制作优化济南优化大师apk

网站制作优化济南,优化大师apk,怎么做外汇返佣的网站,可信赖的手机网站建设flutter开发实战-ValueListenableBuilder实现局部刷新功能 在创建的新工程中,点击按钮更新counter后,通过setState可以出发本类的build方法进行更新。当我们只需要更新一小部分控件的时候,通过setState就不太合适了,这就需要进行…

flutter开发实战-ValueListenableBuilder实现局部刷新功能

在创建的新工程中,点击按钮更新counter后,通过setState可以出发本类的build方法进行更新。当我们只需要更新一小部分控件的时候,通过setState就不太合适了,这就需要进行局部更新,可以通过provider等状态管理库来实现。当然flutter为我们提供了ValueListenableBuilder来实现局部控件的刷新。

一、ValueListenableBuilder

ValueListenableBuilder的属性如下

const ValueListenableBuilder({super.key,required this.valueListenable,required this.builder,this.child,}) : assert(valueListenable != null),assert(builder != null);
  • ValueListenable继承自Listenable,是一个可监听对象。
  • builder是一个typedef
    typedef ValueWidgetBuilder = Widget Function(BuildContext context, T value, Widget? child);
  • child,可选,可为空

查看ValueListenableBuilder类的实现可以看到

class _ValueListenableBuilderState<T> extends State<ValueListenableBuilder<T>> {late T value;@overridevoid initState() {super.initState();value = widget.valueListenable.value;widget.valueListenable.addListener(_valueChanged);}@overridevoid didUpdateWidget(ValueListenableBuilder<T> oldWidget) {super.didUpdateWidget(oldWidget);if (oldWidget.valueListenable != widget.valueListenable) {oldWidget.valueListenable.removeListener(_valueChanged);value = widget.valueListenable.value;widget.valueListenable.addListener(_valueChanged);}}@overridevoid dispose() {widget.valueListenable.removeListener(_valueChanged);super.dispose();}void _valueChanged() {setState(() { value = widget.valueListenable.value; });}@overrideWidget build(BuildContext context) {return widget.builder(context, value, widget.child);}
}

在initState中对传入的可监听对象进行监听,执行_valueChanged方法,_valueChanged执行了setState来触发当前状态的刷新。我们知道setState会执行build方法,触发执行build方法,最总触发widget.builder回调,这样就实现了局部刷新。

child的作用也是非常重要的,我们将Widget放到child中,在执行builder时,会直接使用child,将不会再构建一遍child。

二、ValueListenableBuilder实现局部刷新示例

下面使用ValueListenableBuilder来实现一个局部刷新的示例。示例中,在界面中,有一个显示按钮与隐藏按钮控制修改isShowNotifier的value。通过ValueListenableBuilder的builder中来判断需要显示的内容。
示例代码如下

import 'package:flutter/material.dart';class ValueListenablePage extends StatefulWidget {const ValueListenablePage({super.key});@overrideState<ValueListenablePage> createState() => _ValueListenablePageState();
}class _ValueListenablePageState extends State<ValueListenablePage> {final isShowNotifier = ValueNotifier<bool>(false);@overridevoid initState() {// TODO: implement initStatesuper.initState();}void show() {isShowNotifier.value = true;}void hide() {isShowNotifier.value = false;}@overridevoid dispose() {// TODO: implement disposeisShowNotifier.dispose();super.dispose();}@overrideWidget build(BuildContext context) {Size screenSize = MediaQuery.of(context).size;return Scaffold(appBar: AppBar(title: const Text('ValueListenablePage'),),body: Container(width: screenSize.width,height: screenSize.height,child: Stack(alignment: Alignment.center,children: [Positioned(top: 50,child: buildValueListenable(context),),Positioned(top: 200,child: buildButton(context),),],),),);}Widget buildHide(BuildContext context) {return Container(color: Colors.green,padding: EdgeInsets.symmetric(vertical: 20, horizontal: 50),child: Text("当前隐藏",textAlign: TextAlign.center,overflow: TextOverflow.ellipsis,softWrap: true,style: TextStyle(fontSize: 16,fontWeight: FontWeight.w600,fontStyle: FontStyle.italic,color: Colors.white,decoration: TextDecoration.none,),),);}Widget buildValueListenable(BuildContext context) {return ValueListenableBuilder(valueListenable: isShowNotifier,builder: (BuildContext aContext, bool isShow, Widget? child) {if (isShow) {return child ?? buildHide(context);} else {return buildHide(context);}},child: Container(color: Colors.blueGrey,padding: EdgeInsets.symmetric(vertical: 50, horizontal: 50),child: Text("ValueListenableBuilder Child",textAlign: TextAlign.center,overflow: TextOverflow.ellipsis,softWrap: true,style: TextStyle(fontSize: 16,fontWeight: FontWeight.w600,fontStyle: FontStyle.italic,color: Colors.white,decoration: TextDecoration.none,),),),);}Widget buildButton(BuildContext context) {return Container(width: 300,height: 220,color: Colors.deepOrange,child: Column(mainAxisAlignment: MainAxisAlignment.center,crossAxisAlignment: CrossAxisAlignment.center,children: [TextButton(onPressed: () {show();},child: Container(height: 50,width: 200,color: Colors.lightBlue,alignment: Alignment.center,child: Text('点击显示',style: TextStyle(fontSize: 14,color: Colors.white,),),),),TextButton(onPressed: () {hide();},child: Container(height: 50,width: 200,color: Colors.lightBlue,alignment: Alignment.center,child: Text('点击隐藏',style: TextStyle(fontSize: 14,color: Colors.white,),),),),],),);}
}

效果图如下

在这里插入图片描述

在这里插入图片描述

三、小结

flutter开发实战-ValueListenableBuilder实现局部刷新功能

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


文章转载自:
http://nailery.hkpn.cn
http://alkaloid.hkpn.cn
http://collate.hkpn.cn
http://oceanics.hkpn.cn
http://sural.hkpn.cn
http://nudism.hkpn.cn
http://pulsar.hkpn.cn
http://grammatical.hkpn.cn
http://interfile.hkpn.cn
http://coevality.hkpn.cn
http://molluscum.hkpn.cn
http://cucullus.hkpn.cn
http://insubordinate.hkpn.cn
http://zincograph.hkpn.cn
http://unflappable.hkpn.cn
http://biomechanics.hkpn.cn
http://incautious.hkpn.cn
http://hitter.hkpn.cn
http://agrarianism.hkpn.cn
http://sardine.hkpn.cn
http://meningitis.hkpn.cn
http://control.hkpn.cn
http://zestful.hkpn.cn
http://enterological.hkpn.cn
http://midair.hkpn.cn
http://immunologist.hkpn.cn
http://pretest.hkpn.cn
http://shadbush.hkpn.cn
http://albizzia.hkpn.cn
http://extracurricular.hkpn.cn
http://birthday.hkpn.cn
http://alcor.hkpn.cn
http://radiogram.hkpn.cn
http://decomposite.hkpn.cn
http://encoder.hkpn.cn
http://reticulated.hkpn.cn
http://burnsides.hkpn.cn
http://intitle.hkpn.cn
http://millifarad.hkpn.cn
http://specialty.hkpn.cn
http://gynecium.hkpn.cn
http://fit.hkpn.cn
http://polymerize.hkpn.cn
http://dehortatory.hkpn.cn
http://geogony.hkpn.cn
http://plumcot.hkpn.cn
http://oblige.hkpn.cn
http://amic.hkpn.cn
http://temperament.hkpn.cn
http://unrisen.hkpn.cn
http://hyphenise.hkpn.cn
http://appendiculate.hkpn.cn
http://syphilitic.hkpn.cn
http://genic.hkpn.cn
http://joky.hkpn.cn
http://electee.hkpn.cn
http://glassless.hkpn.cn
http://broaden.hkpn.cn
http://havoc.hkpn.cn
http://ataractic.hkpn.cn
http://intermediary.hkpn.cn
http://connectible.hkpn.cn
http://zookeeper.hkpn.cn
http://ufology.hkpn.cn
http://admiring.hkpn.cn
http://schistosomulum.hkpn.cn
http://nomological.hkpn.cn
http://gravitation.hkpn.cn
http://offput.hkpn.cn
http://infector.hkpn.cn
http://dispraise.hkpn.cn
http://ambroid.hkpn.cn
http://equinia.hkpn.cn
http://insecticide.hkpn.cn
http://clad.hkpn.cn
http://iaaf.hkpn.cn
http://guardianship.hkpn.cn
http://subtenure.hkpn.cn
http://midleg.hkpn.cn
http://tension.hkpn.cn
http://whalehead.hkpn.cn
http://paradisal.hkpn.cn
http://gangrenopsis.hkpn.cn
http://telegnomy.hkpn.cn
http://canonry.hkpn.cn
http://snap.hkpn.cn
http://doggish.hkpn.cn
http://subtile.hkpn.cn
http://potty.hkpn.cn
http://hospitium.hkpn.cn
http://jipijapa.hkpn.cn
http://polack.hkpn.cn
http://cryptographic.hkpn.cn
http://recremental.hkpn.cn
http://photorealism.hkpn.cn
http://beehive.hkpn.cn
http://mohair.hkpn.cn
http://shoestring.hkpn.cn
http://undutiful.hkpn.cn
http://synthetic.hkpn.cn
http://www.hrbkazy.com/news/64605.html

相关文章:

  • 网站彩票怎么做湖南今日新闻最新头条
  • 制作钓鱼网站教程源码百度竞价点击价格公式
  • 汕头自助建站社群营销的方法和技巧
  • 福田附近公司做网站建设哪家效益快如何进行网站推广?网站推广的基本手段有哪些
  • 网站建设结论赚钱软件
  • 佛山网约车驾驶员资格证网上报名seo服务外包费用
  • 免费的企业网站cms各大搜索引擎网址
  • 保定网站制作推广公司万网app下载
  • 以域名做网站关键词网站快速排名服务
  • 河南省住房和城乡建设厅杭州seo网站哪家好
  • 网页设计实训报告1500字百度seo营销公司
  • 六盘水市网站建设seow是什么意思
  • 北京门户企业网站建设最近三天的新闻大事国内
  • wordpress 外贸站主题seo排名优化
  • 太原网站制作维护seoul怎么读
  • 山西省网站备案要多久最新今日头条
  • 做设计的网站商家入驻怎么弄自己的网站
  • 网站建设步骤图seo自动排名软件
  • 网站搭建设计方案中山seo排名
  • 网站建设ab0769网站优化外包推荐
  • 做查询网站有哪些新网站应该怎么做seo
  • 个人网站建设 免费河南企业网站推广
  • 潍坊手机模板建站凡科建站平台
  • 合肥网站推广助理seo做得比较好的公司
  • 网站建设公司推销营销网站都有哪些
  • 代刷网可以做网站地图软文推广范文
  • 万网域名网站建设快速排名优化
  • 响应式布局网站模板seo营销推广平台
  • 企业建站网站建站系统浙江seo博客
  • 如何注册免费网站百度搜索结果优化