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

网站集约化建设存在的困难网上售卖平台有哪些

网站集约化建设存在的困难,网上售卖平台有哪些,如何在百度能搜索到公司网站,网站建设的基本流程包括什么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/160780.html

相关文章:

  • 网站美化工具dw软件怎么制作网页
  • 常用的网站推广的方式方法发布悬赏任务的推广平台
  • 图片设计网站有哪些汕头seo托管
  • 厦门网站建设厦门seo搜索引擎优化怎么优化
  • 有教做素食的网站吗关键词在线试听
  • 网站里的滚动怎么做中山网站建设公司
  • 做的网站错位怎么办十大seo免费软件
  • 营口规划建设局网站广州最新疫情情况
  • 盐城市网站建设公司制作一个网站步骤
  • 哪个科技新闻网站好百度推广方式
  • 做爰真实网站百度权重4网站值多少钱
  • 阿里云服务器做网站django公司品牌宣传方案
  • 建设官方网站首页电脑优化工具
  • 网站建设推广型关键词搜索站长工具
  • 网络营销课程性质seo常见优化技术
  • 天津网站设计seo优化排名技术百度教程
  • 搜索不到的网站湖南网站建设加盟代理
  • 鞍山一般做一个网站需要多少钱百度云网盘资源搜索
  • 做趣味图形的网站搜索引擎是指什么
  • 手机网站一键生成appseo教程排名第一
  • wordpress管理网站想要导航页面推广app
  • 企业网站建设费用需要多少钱新闻10条摘抄大全
  • 国家住房和城乡建设部网站吧seo网站推广目的
  • 论坛类网站怎么建设360优化大师官方下载最新版
  • 素材网站有哪些杭州百度快照推广
  • 商务网站建设与维护(专21春)seo手机端优化
  • 海南建设网网站新闻头条免费下载安装
  • 琼海做网站口碑贵州seo技术查询
  • wordpress复制按钮网站快速优化排名方法
  • 南山做网站多少钱网站优化方案