[英]Custom StateFull widget in Flutter
我正在 flutter 中創建我的自定義有狀態小部件。我已經嘗試過無狀態小部件。 它正在工作。 但我不能用於有狀態的小部件。 child:child.代碼中有紅色下划線錯誤。
class MyAnimate extends StatefulWidget {
const MyAnimate({required this.child});
final Widget child;
@override
State<MyAnimate> createState() => _MyAnimateState();
}
class _MyAnimateState extends State<MyAnimate> with TickerProviderStateMixin {
late AnimationController controller;
late AnimationController controller2;
late Animation<Offset> animation;
late Animation<double> animation2;
@override
void initState() {
controller = AnimationController(
duration: const Duration(milliseconds: 300), vsync: this);
animation = Tween<Offset>(
begin: Offset(-1, 0),
end: Offset(0, 0),
).animate(CurvedAnimation(parent: controller, curve: Curves.easeOut));
//
controller2 = AnimationController(
duration: const Duration(milliseconds: 300), vsync: this);
animation2 = CurvedAnimation(parent: controller2, curve: Curves.easeIn);
controller.forward();
controller2.forward();
super.initState();
}
@override
Widget build(BuildContext context) {
return SlideTransition(
position: animation,
child: FadeTransition(
opacity: animation2,
child: Container(
color: Colors.indigo,
child: child, //second child is not defined on this line
),
),
);
}
}
我正在創建此自定義小部件,以最簡單的方式使用幻燈片和不透明度轉換。 但它不起作用。 如何為小部件定義子方法?
當您在StatefulWidget
構造函數中接收到一些變量時,您應該使用widget.
訪問它。 所以不要使用child
使用widget.child
:
@override
Widget build(BuildContext context) {
return SlideTransition(
position: animation,
child: FadeTransition(
opacity: animation2,
child: Container(
color: Colors.indigo,
child: widget.child, //<---add here
),
),
);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.