- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我正在尝试了解在该 Widget 状态之外控制 StatefulWidget 状态的最佳实践。
我定义了以下接口(interface)。
abstract class StartupView {
Stream<String> get onAppSelected;
set showActivity(bool activity);
set message(String message);
}
我想创建一个实现此接口(interface)的 StatefulWidget StartupPage
。我希望小部件执行以下操作:
当一个按钮被按下时,它会通过 onAppSelected 流发送一个事件。 Controller 甚至会监听并执行一些操作(数据库调用、服务请求等)。
Controller 可以调用 showActivity
或 set message
让 View 通过消息显示进度。
由于 Stateful Widget 不会将其 State 作为属性公开,因此我不知道访问和修改 State 属性的最佳方法。
我希望使用它的方式是这样的:
Widget createStartupPage() {
var page = new StartupPage();
page.onAppSelected.listen((app) {
page.showActivity = true;
//Do some work
page.showActivity = false;
});
}
我曾考虑通过在 createState()
中传入我希望它返回的状态来实例化 Widget,但感觉不对。
我们为什么采用这种方法的一些背景知识:我们目前有一个 Dart Web 应用程序。为了 View Controller 分离、可测试性和对 Flutter 的前瞻性思考,我们决定为应用程序中的每个 View 创建一个接口(interface)。这将允许 WebComponent 或 Flutter Widget 实现此接口(interface)并保持所有 Controller 逻辑相同。
最佳答案
有多种方式可以与其他有状态小部件进行交互。
1. findAncestorStateOfType
第一个也是最直接的方法是通过context.findAncestorStateOfType
方法。
通常包装在 Stateful
的静态方法中像这样的子类:
class MyState extends StatefulWidget {
static of(BuildContext context, {bool root = false}) => root
? context.findRootAncestorStateOfType<_MyStateState>()
: context.findAncestorStateOfType<_MyStateState>();
@override
_MyStateState createState() => _MyStateState();
}
class _MyStateState extends State<MyState> {
@override
Widget build(BuildContext context) {
return Container();
}
}
这就是 Navigator
例如工作。
亲:
缺点:
State
属性或手动调用setState
State
子类当你想访问一个变量时不要使用这个方法。当该变量更改时,您的小部件可能不会重新加载。
<强>2。 Listenable、Stream 和/或 InheritedWidget
有时您可能想要访问某些属性而不是方法。问题是,您很可能希望您的小部件在该值随时间变化时更新。
在这种情况下,飞镖报价Stream
和 Sink
.并且 flutter 添加在它的顶部InheritedWidget
和 Listenable
如ValueNotifier
.它们都做相对相同的事情:订阅值更改事件时与 StreamBuilder
结合使用。/context.dependOnInheritedWidgetOfExactType
/AnimatedBuilder
.
当您想要 State
时,这是首选解决方案暴露一些属性。我不会涵盖所有的可能性,但这里有一个使用 InheritedWidget
的小例子:
首先,我们有一个 InheritedWidget
暴露一个 count
:
class Count extends InheritedWidget {
static of(BuildContext context) =>
context.dependOnInheritedWidgetOfExactType<Count>();
final int count;
Count({Key key, @required Widget child, @required this.count})
: assert(count != null),
super(key: key, child: child);
@override
bool updateShouldNotify(Count oldWidget) {
return this.count != oldWidget.count;
}
}
然后我们有我们的State
实例化这个 InheritedWidget
class _MyStateState extends State<MyState> {
int count = 0;
@override
Widget build(BuildContext context) {
return Count(
count: count,
child: Scaffold(
body: CountBody(),
floatingActionButton: FloatingActionButton(
onPressed: () {
setState(() {
count++;
});
},
),
),
);
}
}
最后,我们有了 CountBody
获取这个暴露的count
class CountBody extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Center(
child: Text(Count.of(context).count.toString()),
);
}
}
优点:
findAncestorStateOfType
更高效await for
或 async*
)缺点:
3.通知
而不是直接调用 State
上的方法,您可以发送Notification
从您的小部件。并制作State
订阅这些通知。
Notification
的示例将是:
class MyNotification extends Notification {
final String title;
const MyNotification({this.title});
}
要发送通知,只需调用 dispatch(context)
在您的通知实例上,它会冒泡。
MyNotification(title: "Foo")..dispatch(context)
Note: you need put above line of code inside a class, otherwise no context, can NOT call notification.
任何给定的小部件都可以使用 NotificationListener<T>
收听其子级发送的通知:
class _MyStateState extends State<MyState> {
@override
Widget build(BuildContext context) {
return NotificationListener<MyNotification>(
onNotification: onTitlePush,
child: Container(),
);
}
bool onTitlePush(MyNotification notification) {
print("New item ${notification.title}");
// true meaning processed, no following notification bubbling.
return true;
}
}
例如 Scrollable
,可以调度ScrollNotification
包括开始/结束/过度滚动。然后被 Scrollbar
使用无需访问 ScrollController
即可了解滚动信息
优点:
State
上做事.它是 State
订阅由其子级触发的事件State
属性缺点:
关于flutter - 从 StatefulWidget 外部控制状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46057353/
我的屏幕上有 3 个标签。都有不同的看法。当用户来到屏幕上时,我必须点击 API 并更新所有 3 个选项卡的 View 。我无法更新选项卡内的 View 。获取 API 响应后无法更新不同选项卡的状态
例如,在下面的代码中,加号按钮可以工作并且能够更新文本,但减号按钮没有。 但如果我们按下 FloatingActionButton 则状态会被刷新。 减号按钮正在更改变量的值,但没有更新父窗口小部件的
我是Flutter的新手。我有一个带有StatefulWidget的default parameter,我已经完成了研究,许多人已经用vivid示例(例如izwebtechnologies和Stack
我正在开发一个 statefulWidget,我的目的是确保在选择一个选项(用这种语言)之前,下一个按钮不可单击。然而它似乎不起作用,我还在代码中添加了Yaesin(回答的人)的答案 ListVie
我正在开发一个 statefulWidget,我的目的是确保在选择一个选项(用这种语言)之前,下一个按钮不可单击。然而它似乎不起作用,我还在代码中添加了Yaesin(回答的人)的答案 ListVie
我有一个包含 ListView 的有状态小部件 MyList。在点击任何列表 block 时,新屏幕 (MyScreen) 打开,返回后(通过按左上角的后退按钮)滚动位置丢失,因为 MyList 被处
StatefulBuilder有什么区别和 StatefulWidget ?什么时候应该使用一个而不是另一个? 乍一看,它们似乎很相似。StatefulBuilder被定义为: A platonic
如何调用movePage(page) Widget1中的函数来自 MaterialButton在小部件树中深深嵌套在下面? 请引用下面的示例代码: class Widget1 extends Stat
我正在构建一个类似tinder的卡片刷卡机制,其中我在Stack小部件中有一个卡片小部件列表。第一张卡被包装在一个 Dismissible 小部件中,在关闭时,关闭顶部的卡并在底部添加一个新的卡。 我
在我的小部件中,女巫是一个StatefulWidget,我的逻辑检查一个 token ,在错误情况下,我导航到我的主页。 我正在等待小部件使用此方法完成构建: WidgetsBinding.insta
我正在尝试访问 build() 中的 moviesList,结果如下。真正的问题是什么? flutter: ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═════
我已经学习了如何通过 StatelessWidget 使用 i18n 进行 flutter 练习,但仍然无法通过 StatefulWidget 工作。 我可以简单地替换下面的代码 title: new
我有一个方法可以根据枚举的值返回两个有状态小部件之一。但是,即使我可以通过调试器看到正在返回“正确”的小部件,UI 也只会显示第一个呈现的小部件。状态对象似乎以某种方式在小部件的不同实例之间共享,或者
我是 Flutter 的新手。我正在访问状态类中的属性 - “startingProduct”。该变量在 A StatefulWidget Class 中定义。但我得到“startingProduct
我正在尝试创建一个 flutter 应用程序。我正在尝试为这样的英雄标签使用构建器函数: import 'package:flutter/material.dart'; void main() =>
我有一个名为 BounceContainer 的 StatefulWidget。此类包含一个参数 child ,就像典型的 Flutter 小部件一样。基本上,只要用户点击它,它就会弹出 child
在我的个人资料页面中,当使用单击编辑图标时有编辑选项,我使用 bool 条件将 Text 小部件更改为 TextField 小部件 widget.isUpdate ? new Flexible(chi
StatelessWidget 的所有代码都在一个类中,甚至包括 build 方法。对于 StatefulWidget,为什么 State 是一个独立于 Widget 的类?不能将它们组合起来,Sta
这个问题的重点是如何处理状态变化,最好由原始祖先自动处理。在我看来,不可能继续将这些类(class)扩展到第一个以 StatefulWidget 作为祖先的 child (示例 1)? 在我看来,唯一
我正在尝试了解在该 Widget 状态之外控制 StatefulWidget 状态的最佳实践。 我定义了以下接口(interface)。 abstract class StartupView {
我是一名优秀的程序员,十分优秀!