Flutter 颤振-在子按钮单击时更新parant小部件类UI
我有这种情况Flutter 颤振-在子按钮单击时更新parant小部件类UI,flutter,flutter-layout,Flutter,Flutter Layout,我有这种情况 Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text("Hello"), ), body: Container( child: ChildWidget( listControl: this.sentToScreenBuildJson, notifyParent: refresh, ), ), ); } 这是我
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Hello"),
),
body: Container(
child: ChildWidget(
listControl: this.sentToScreenBuildJson,
notifyParent: refresh,
),
),
);
}
这是我的父构建方法,我在其中添加了ChildWidget和另一个statfulscreen,传递的是json和刷新函数
根据json,子级将能够绘制UI
点击按钮,我就可以得到刷新方法的回调
refresh() {
print("I get refreshed from child");
setState(() {
print("I get refreshed from child in setState");
this.sentToScreenBuildJson = this.newJson;
});
}
在按钮上,单击“打印”“获取”“执行”,但UI未按照newJson更新。
就像我期望的那样,当setState运行时,父级必须调用build并传递更新的json。
这是行不通的
谢谢你的帮助
单击子按钮更新parant小部件类UI
这是颤振中的一个常见用例,颤振已内置于类中用于此类目的。您可以直接将其用于您的目的,也可以使用一些现成的软件包解决方案,这些解决方案在后台使用
InheritedWidget
,例如。当您希望将数据从子级传递到父级时,您应该在父级使用,并从子级分派数据
Notification
类的实例将具有可以使用NotificationListener
在父级中使用的数据
大多数情况下,所有的flatter小部件都使用这种技术,例如,当用户到达最后一个选项卡并仍然尝试滑动时,tab controller会收到OverscrollNotification
以下是您可以用来了解如何在代码中使用NotificationListener
的演示
import 'package:flutter/material.dart';
void main() => runApp(ParentWidget());
class ParentWidget extends StatefulWidget {
ParentWidget({Key key}) : super(key: key);
@override
_ParentWidgetState createState() => _ParentWidgetState();
}
class _ParentWidgetState extends State<ParentWidget> {
String _text = 'You have not pressed the button yet';
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: NotificationListener<IntegerNotification>(
onNotification: (IntegerNotification notification) {
setState(() {
print(notification);
_text = 'You have pressed button ${notification.value} times';
});
return true;
},
child: Column(
children: <Widget>[
Text(_text),
ChildWidget(),
],
)
),
),
);
}
}
class ChildWidget extends StatefulWidget {
const ChildWidget({Key key}) : super(key: key);
@override
_ChildWidgetState createState() => _ChildWidgetState();
}
class _ChildWidgetState extends State<ChildWidget> {
int _counter = 0;
@override
Widget build(BuildContext context) {
return RaisedButton(onPressed: (){
IntegerNotification(++_counter).dispatch(context);
},child: Text('Increment counter'),);
}
}
@immutable
class IntegerNotification extends Notification{
final int value;
const IntegerNotification(this.value);
String toString(){
return value.toString();
}
}
导入“包装:颤振/材料.省道”;
void main()=>runApp(ParentWidget());
类ParentWidget扩展了StatefulWidget{
ParentWidget({Key}):超级(Key:Key);
@凌驾
_ParentWidgetState createState()=>\u ParentWidgetState();
}
类_ParentWidgetState扩展状态{
String _text='您还没有按下按钮';
@凌驾
小部件构建(构建上下文){
返回材料PP(
家:脚手架(
正文:NotificationListener(
onNotification:(集成通知){
设置状态(){
打印(通知);
_text='您已按按钮${notification.value}次';
});
返回true;
},
子:列(
儿童:[
文本(_Text),
ChildWidget(),
],
)
),
),
);
}
}
类ChildWidget扩展StatefulWidget{
constchildwidget({Key}):super(Key:Key);
@凌驾
_ChildWidgetState createState()=>\u ChildWidgetState();
}
类_ChildWidgetState扩展状态{
int _计数器=0;
@凌驾
小部件构建(构建上下文){
返回升起按钮(按下时:(){
IntegerNotification(+++u计数器).dispatch(上下文);
},子项:文本('Increment counter'),);
}
}
@不变的
类IntegerNotification扩展了通知{
最终整数值;
常量整数通知(此.value);
字符串toString(){
返回值.toString();
}
}
除了@Darish的答案,您可以在类1
中声明一个静态变量,在类2
中访问该静态变量,然后在类2
中更新变量的状态
例如:
import 'package:flutter/material.dart';
class Demo extends StatefulWidget {
static UserObject userObject;
@override
_Demo createState() => _Demo();
}
class _Demo extends State<Demo> {
@override
void initState() {
Demo.userObject = new UserObject(name: "EXAMPLE NAME");
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Color(0xfff3f3f3),
appBar: AppBar(title: Text("DEMO")),
body: InkWell(
onTap: () {
Navigator.push(context,
MaterialPageRoute(builder: (context) => HeroClass()));
},
child: Center(
child: Hero(
tag: "tag-demo-id",
child: Container(
color: Colors.black,
padding: EdgeInsets.all(20),
child: Text("${Demo.userObject.name} -> CLICK HERE",
style: TextStyle(color: Colors.white)))))));
}
}
class HeroClass extends StatefulWidget {
@override
_HeroClassState createState() => _HeroClassState();
}
class _HeroClassState extends State<HeroClass> {
final myController = TextEditingController();
@override
void initState() {
myController.text = Demo.userObject.name;
super.initState();
}
@override
void dispose() {
// Clean up the controller when the widget is removed from the widget tree.
// This also removes the _printLatestValue listener.
myController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("DEMO HERO")),
body: Hero(
tag: "tag-demo-id",
child: Container(
child: TextField(
controller: myController,
),
)),
floatingActionButton: FloatingActionButton(
onPressed: () {
setState(() {
Demo.userObject.name = myController.text;
});
},
child: Icon(Icons.save),
));
}
}
// object class
class UserObject {
String name;
UserObject({this.name});
UserObject.fromJson(Map<String, dynamic> json) {
name = json['name'];
}
}
导入“包装:颤振/材料.省道”;
类Demo扩展StatefulWidget{
静态用户对象;
@凌驾
_演示createState()=>_Demo();
}
类_Demo扩展了状态{
@凌驾
void initState(){
Demo.userObject=新的userObject(名称:“示例名称”);
super.initState();
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
背景颜色:颜色(0xFFF3),
appBar:appBar(标题:文本(“演示”),
主体:墨水池(
onTap:(){
Navigator.push(上下文,
MaterialPage路由(生成器:(上下文)=>HeroClass());
},
儿童:中心(
孩子:英雄(
标记:“标记演示id”,
子:容器(
颜色:颜色,黑色,
填充:边缘设置。全部(20),
子项:文本(${Demo.userObject.name}->单击此处“,
样式:TextStyle(颜色:Colors.white‘‘‘‘‘‘‘‘‘)’);
}
}
类HeroClass扩展StatefulWidget{
@凌驾
_HeroClassState createState()=>\u HeroClassState();
}
类的状态扩展了状态{
最终myController=TextEditingController();
@凌驾
void initState(){
myController.text=Demo.userObject.name;
super.initState();
}
@凌驾
无效处置(){
//当小部件从小部件树中移除时,清理控制器。
//这还会删除_printLatestValue侦听器。
myController.dispose();
super.dispose();
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(标题:文本(“演示英雄”),
身体:英雄(
标记:“标记演示id”,
子:容器(
孩子:TextField(
控制器:myController,
),
)),
浮动操作按钮:浮动操作按钮(
已按下:(){
设置状态(){
Demo.userObject.name=myController.text;
});
},
子:图标(Icons.save),
));
}
}
//对象类
类用户对象{
字符串名;
UserObject({this.name});
fromJson(映射json){
name=json['name'];
}
}
您能为ChildWidget发布代码吗?