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

成都网站建设冠辰网络营销团队

成都网站建设冠辰,网络营销团队,网站制作说明书,企业网站优化怎么提高关键词排名Flutter 如果界面上有数据更新时,目前学习到的有3种: 第一种: 直接用 StatefulWidget组件,然后当数据更新时,调用setState的方法更新数据,页面上的数据会直接更新;第二种: 用 State…

Flutter 如果界面上有数据更新时,目前学习到的有3种:

  • 第一种: 直接用 StatefulWidget组件,然后当数据更新时,调用setState的方法更新数据,页面上的数据会直接更新;
  • 第二种: 用 StatefulWidget组件 和 InheritedWidget 的结合,这种模式比较适用于有比较多层级的场景;这样数据更新时,就不用一层一层的从父类上传递数据;
  • 第三种:用第三方库scoped_model, 这种模式下在StatelessWidget也可以实现UI页面上数据的更新。

如要实现下面这个页面的效果(点击 count:x 和底部"+", 实现count的数据加1):
在这里插入图片描述

1. StatefulWidget 更新数据的使用

这个比较简单,代码如下:

import 'package:flutter/material.dart';class StatemanagerDemo extends StatefulWidget {const StatemanagerDemo({super.key});State<StatemanagerDemo> createState() => _StatemanagerDemoState();
}class _StatemanagerDemoState extends State<StatemanagerDemo> {int _count = 0;void _increaseCount() {// 这个是关键,只有调用 setState 才能更新页面的数据setState(() {_count += 1;});}Widget build(BuildContext context) {return Scaffold(appBar: AppBar(title: Text("StateManagerDemo"),),body: CounterWidget(count: _count, increaseCount: _increaseCount),floatingActionButton:  FloatingActionButton(onPressed: _increaseCount,child: Icon(Icons.add),),);}
}class CounterWidget extends StatelessWidget {final int count;final VoidCallback increaseCount;const CounterWidget({super.key, required this.count, required this.increaseCount});Widget build(BuildContext context) {return Center(child: ActionChip(label: Text("$count"),onPressed: increaseCount));}
}

2. StatefulWidget组件 和 InheritedWidget 的结合

代码如下:

//-------用InheritedWidget 实现数据的更新------
class StateManagerInheriteDemo extends StatefulWidget {const StateManagerInheriteDemo({super.key});State<StateManagerInheriteDemo> createState() => _StateManagerInheriteDemoState();
}class _StateManagerInheriteDemoState extends State<StateManagerInheriteDemo> {int _count = 0;void _increaseCount() {setState(() {_count += 1;});}Widget build(BuildContext context) {return CounterProvider(count: _count, increaseCount: _increaseCount, child: Scaffold(appBar: AppBar(title: Text("StateManagerDemo"),),body: CounterWidgetInherite(),floatingActionButton:  FloatingActionButton(onPressed: _increaseCount,child: Icon(Icons.add),),));}
}// 创建一个provider类来提供全局可用的数据存储和获取
class CounterProvider extends InheritedWidget {final int count;final VoidCallback increaseCount;final Widget child;const CounterProvider({super.key, required this.count, required this.increaseCount, required this.child}) : super(child: child);// 定义一个of方法,可以获取CounterProvider的数据static CounterProvider? of(BuildContext context) => context.dependOnInheritedWidgetOfExactType<CounterProvider>();bool updateShouldNotify(covariant InheritedWidget oldWidget) {return true;}
}// 模拟多个层级1
class CounterWidgetInherite extends StatelessWidget {const CounterWidgetInherite ({super.key});Widget build(BuildContext context) {return Center(child: Counter(),);}
}// 模拟多个层级2
class Counter extends StatelessWidget {const Counter({super.key});Widget build(BuildContext context) {final int? count = CounterProvider.of(context)?.count;final VoidCallback? increaseCount = CounterProvider.of(context)?.increaseCount;return ActionChip(label: Text("$count"),onPressed: increaseCount,);}
}

3. scoped_model 的使用

scoped_model 是第三方库,需要在项目中引入三方库(引入三方库的方法,参考这个文章);其次,该模式引用到的组件主要是:

  • ScopedModel
  • ScopedModelDescendant

原理还没搞明白,只是看着视频实现了功能,代码如下:

import 'package:flutter/material.dart';
import 'package:scoped_model/scoped_model.dart';//-------用Scoped_Model 实现数据的更新-----
class StateManagerScopteModelDemo extends StatelessWidget {const StateManagerScopteModelDemo({super.key});Widget build(BuildContext context) {// 用ScopedModel 包装一下,提供model属性对应的类型return ScopedModel(model: CounterModel(), child: Scaffold(appBar: AppBar(title: Text("StateManagerDemo"),),body: CounterWidgetScopteModel(),// ScopedModelDescendant 的构建方法获取CounterModel对应的数据floatingActionButton:  ScopedModelDescendant<CounterModel>(rebuildOnChange: false,builder: (context, _, model) {return FloatingActionButton(onPressed: model.increaseCount,child: Icon(Icons.add),);}),));}
}class CounterWidgetScopteModel extends StatelessWidget {const CounterWidgetScopteModel ({super.key});Widget build(BuildContext context) {return Center(// ScopedModelDescendant 的构建方法获取CounterModel对应的数据child: ScopedModelDescendant<CounterModel>(builder: (context, _, model) {return ActionChip(label: Text("count: ${model.count}"),onPressed: model.increaseCount,);}),);}
}// 创建继承于Model的业务类,来存放对应的count和用到的方法
class CounterModel extends Model {int _count = 0;int get count => _count;void increaseCount() {_count += 1;// 使用Model小部件的地方会监听Model的变化,可以用notifyListeners方法重建UI notifyListeners();}
}
http://www.wangmingla.cn/news/96556.html

相关文章:

  • 个人做财经类网站品牌整合营销案例
  • 网站更换服务器如何做镜像网站策划是什么
  • 东莞模具网站建设外链工厂
  • 深圳罗湖区网站开发公司西安seo优化培训
  • 创手机网站如何建立网页
  • 包包网站建设策划书图床外链生成工具
  • 网站开发论文研究方法优质的seo网站排名优化软件
  • 做饮品的网站成都计算机培训机构排名前十
  • 易语言如何做网站标题优化方法
  • 网站备案所需材料免费的行情软件网站下载
  • 广州外贸营销型网站建设今日热点新闻头条排行榜
  • 上海软件网站建设网络营销有哪些特点
  • 网站建设 商城襄阳网站seo
  • 鹤壁网站建设厦门seo网站优化
  • 用心做的网站免费网站的软件
  • 安卓盒子 做网站小红书怎么做关键词排名优化
  • bootstrop新闻网站开发怎么做app推广
  • 企业门户网站作用宁波seo关键词优化制作
  • 做网站图网站主页
  • 网站检测器app拉新推广一手接单平台
  • 聊城做网站好的公司seo网站优化建议
  • 深圳网站建设一条龙百度广告
  • 装修案例介绍文案关键词优化包含
  • 成都公司注册核名上海百度推广优化公司
  • 电商网站开发教材百度公司排名多少
  • 虚拟主机建设网站绑定域名发布新闻的平台有哪些
  • 个体工商户经营范围做网站网址和网站的区别
  • 策划书网站项目目标需求分析成都seo达人
  • 股票网站建设淘宝搜索关键词技巧
  • 建筑资料网广州网站快速排名优化