我对flutter/dart还不熟悉,所以在try 制作应用程序的同时,我也试图理解为什么事情会以某种方式发展.在Flatter文档中,有一个有状态小部件的示例代码,如图所示:
class YellowBird extends StatefulWidget {
const YellowBird({ Key key }) : super(key: key);
@override
_YellowBirdState createState() => new _YellowBirdState();
}
class _YellowBirdState extends State<YellowBird> {
@override
Widget build(BuildContext context) {
return new Container(color: const Color(0xFFFFE306));
}
}
问题:
为什么它们被定义为两类而不是一类?我猜State类可以在其他地方使用,所以最好分开.
据我所知,
createState()
函数返回State
类型的对象,所以有_YellowBirdState extends State
是有意义的,但为什么YellowBird
会被传递到State
的泛型类中呢?我猜这与Yellowbird
扩展StatefulWidget
类有关,但不太确定.