【发布时间】:2019-08-19 06:29:47
【问题描述】:
假设我们有 Car (Stateless) 类,它里面有两个类,现在它将是 Wheel (Statefull) 和 Mask (Statefull),我的工作是每当类 Wheel 的状态改变时调用类 Mask 来改变它状态也包含来自 Wheel 的特定数据,但父级也应该有权访问子级数据。我怎样才能实现它?
import 'package:flutter/material.dart';
void main() {
runApp(Car());
}
class Car extends StatelessWidget {
int childMaskVal = ..??????
@override
Widget build(BuildContext context) {
return Container(
child: Scaffold(
appBar: AppBar(
title: Text('App bar'),
),
body: Column(
children: <Widget>[
Wheel(),
Mask(),
],
),
),
);
}
}
class Wheel extends StatefulWidget {
_WheelState createState() => _WheelState();
}
class _WheelState extends State<Wheel> {
int _value = 0;
@override
Widget build(BuildContext context) {
return Container(
child: Column(
children: <Widget>[
RaisedButton(
onPressed: () {
setState(() {
_value++;
});
},
),
Text(_value.toString()),
],
),
);
}
}
class Mask extends StatefulWidget {
_MaskState createState() => _MaskState();
}
class _MaskState extends State<Mask> {
int _value = 13;
@override
Widget build(BuildContext context) {
return Container(
child: Text((_value * Wheel._value).toString()),???????
);
}
}
【问题讨论】: