gpt4 book ai didi

dart - 如何将数据从子小部件传递到其父小部件

转载 作者:IT老高 更新时间:2023-10-28 12:29:30 25 4
gpt4 key购买 nike

我有下面的自定义小部件,它制作了一个 Switch 并读取其状态(真/假)

那我把这个加到我的主app小部件(父级)中,如何让父级知道开关的值!

import 'package:flutter/material.dart';

class Switchy extends StatefulWidget{
Switchy({Key key}) : super(key: key);

@override
State<StatefulWidget> createState() => new _SwitchyState();
}

class _SwitchyState extends State<Switchy> {
var myvalue = true;

void onchange(bool value) {
setState(() {
this.myvalue = value; // I need the parent to receive this one!
print('value is: $value');
});
}

@override
Widget build(BuildContext context) {
return
new Card(
child: new Container(
child: new Row(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
new Text("Enable/Disable the app in the background",
textAlign: TextAlign.left,
textDirection: TextDirection.ltr,),
new Switch(value: myvalue, onChanged: (bool value) => onchange(value)),
],
),
),
);
}
}

main.dart(父)文件中,我从这个开始:

import 'widgets.dart';
import 'package:flutter/material.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
theme: new ThemeData(
primarySwatch: Colors.deepOrange,
),
home: new MyHomePage(title: 'My App settup'),
);
}
}

class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);

final String title;

@override
_MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {

Widget e = new Switchy();
//...

}

最佳答案

第一种可能性是将回调传递给您的 child ,第二种可能性是为您的有状态小部件使用 of 模式。见下文。


import 'package:flutter/material.dart';

class MyStatefulWidget extends StatefulWidget {
@override
State<StatefulWidget> createState() => new MyStatefulWidgetState();

// note: updated as context.ancestorStateOfType is now deprecated
static MyStatefulWidgetState of(BuildContext context) =>
context.findAncestorStateOfType<MyStatefulWidgetState>();
}

class MyStatefulWidgetState extends State<MyStatefulWidget> {
String _string = "Not set yet";

set string(String value) => setState(() => _string = value);

@override
Widget build(BuildContext context) {
return new Column(
children: <Widget>[
new Text(_string),
new MyChildClass(callback: (val) => setState(() => _string = val))
],
);
}
}

typedef void StringCallback(String val);

class MyChildClass extends StatelessWidget {
final StringCallback callback;

MyChildClass({this.callback});

@override
Widget build(BuildContext context) {
return new Column(
children: <Widget>[
new FlatButton(
onPressed: () {
callback("String from method 1");
},
child: new Text("Method 1"),
),
new FlatButton(
onPressed: () {
MyStatefulWidget.of(context).string = "String from method 2";
},
child: new Text("Method 2"),
)
],
);
}
}

void main() => runApp(
new MaterialApp(
builder: (context, child) => new SafeArea(child: new Material(color: Colors.white, child: child)),
home: new MyStatefulWidget(),
),
);

还有使用 InheritedWidget 代替 StatefulWidget 的替代方案;如果您希望在父窗口小部件的数据更改并且父窗口小部件不是直接父窗口时重建子窗口小部件,这将特别有用。见 inherited widget documentation

关于dart - 如何将数据从子小部件传递到其父小部件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49824461/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com