Android 我应该如何在Flutter中调整我的小部件树?
这是我的应用程序的小部件树Android 我应该如何在Flutter中调整我的小部件树?,android,ios,flutter,dart,flutter-layout,Android,Ios,Flutter,Dart,Flutter Layout,这是我的应用程序的小部件树 在deleteBtn中,小部件是一个按钮。如果我按下按钮,它将向textVahy和bottomRect发送假值。这里有什么简单的方法吗?也许可以用继承的小部件完成,但我不能使用那个小部件。。。那么这里有什么简单的方法可以做到这一点吗?我建议使用一些状态管理,比如BloC或Redux。在Redux的情况下,您可以将您的“下拉列表”包装在StoreProvider中,它允许您处理来自各种小部件的各种事件。这些小部件需要包装在StoreConnector中。从一个连接器向r
在deleteBtn中,小部件是一个按钮。如果我按下按钮,它将向textVahy和bottomRect发送假值。这里有什么简单的方法吗?也许可以用继承的小部件完成,但我不能使用那个小部件。。。那么这里有什么简单的方法可以做到这一点吗?我建议使用一些状态管理,比如BloC或Redux。在Redux的情况下,您可以将您的“下拉列表”包装在StoreProvider中,它允许您处理来自各种小部件的各种事件。这些小部件需要包装在StoreConnector中。从一个连接器向reducer分派操作后,可以说是tour“deleteBtn”,您可以定义另一个连接器的反应方式。看看例子。我完全同意David使用一些
状态管理
,比如提供者
,集团
。但是如果您使用的是单页状态管理,那么您可以暂时不使用它,因为它是高级主题。它还需要一些包导入,例如提供者
,颤振集团
等
您可以使用一个简单版本的状态管理
,即
- 它检查您为其定义的值,即
对象中的值,然后您将其传递给它。这是强制性的ValueNotifier
- 您包装小部件,它将在值更改时重新生成,在本例中为
和textVahy
bottomRect
ValueListenableBuilder
中,用于textVahy
和bottomRect
现在,让我们以你想要理解的方式来交谈
我们使用bool
值,该值必须在ValueNotifier
// taking the initial value as true
ValueNotifier<bool> _myBool = ValueNotifier<bool>(true);
现在,当您想通过deleteBtn
更改\u myBool
的值时,只需通过setState()
点击按钮后,该值将被发送到ValueListenableBuilder()
,并根据bool
,重新生成封装在其中的小部件
ValueListenableBuilder(
// the value is myBool's value only
builder: (BuildContext context, bool value, Widget child) {
// This builder will only get called when _myBool
// is updated.
return Column(
children: <Widget>[
// whatever operation you wanna do based upon your value
// listen to value, since it has a param, it shows text with value
// if the value is false, else show you textVahy and bottomRect
if value ? textVahy : Text('$value'),
if value ? bottomRect : Text('$value')
]
);
},
// passing ValueNotifier object to listen for any changes
valueListenable: _myBool
)
RaisedButton(
onPressed: (){
// please note _myBool is the object of ValueNotifier, but _myBool.value has
// the right variable to store the predefined data
setState(() => _myBool.value = false);
},
child: Text('Delete Me!!')
)