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

网站建设与管理维护说课网络推广如何收费

网站建设与管理维护说课,网络推广如何收费,mac 用什么软件做网站好,重庆高端网站建设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.dtcms.com/wzjs/206501.html

相关文章:

  • 做网站的流程 优帮云seo推广技巧
  • 服务器 做网站seo竞价排名
  • 建设网站平台需要的设备产品怎么做市场推广
  • 东莞大岭山网站制作网络推广视频
  • 网站后台上传缩略图手机关键词排名优化
  • 如何阿里巴巴网站做推广方案seo课程哪个好
  • 书法 wordpress长沙百度网站优化
  • wordpress 评论添加表情郑州网站推广优化公司
  • 怎么免费注册自己的网站桂林网站设计制作
  • 龙江网站建设百度网站排名查询
  • 做网站的草图 用什么画站长之家seo查找
  • 南京做网站的写软文一篇多少钱合适
  • 特价网站建设官网巨量算数关键词查询
  • 教育机构网站是seo快排技术教程
  • 企业网站的主要类型aso排名服务公司
  • 动画制作专业大学排名百度词条优化工作
  • 福州光电网站建设可以推广的软件
  • 东莞厚街网站建设百度指数的需求指数
  • 网站如何做问卷调查问卷网站策划是干什么的
  • 正常做一个网站多少钱sem seo
  • 室内设计作品集案例赏析seo中文全称是什么
  • 开发网站网络公司排行网络优化工资一般多少
  • win10做网站服务器百度商业平台
  • 湛江市建设规划局网站榜单优化
  • 如何做试玩类网站seopc流量排行榜企业
  • 湘潭做网站找磐石网络一流推广引流图片
  • 网站安装百度商桥网络推广法
  • 逆袭做富豪官方网站百度收录网址
  • wordpress下载网站营销存在的问题及改进
  • 学做网站的书籍国内企业网站模板