简体   繁体   English

Flutter 中的自定义 StateFull 小部件

[英]Custom StateFull widget in Flutter

I am creating my custom statefull widget in flutter. I have tried for stateless widget.我正在 flutter 中创建我的自定义有状态小部件。我已经尝试过无状态小部件。 It was working.它正在工作。 But I can't use for stateful widget.但我不能用于有状态的小部件。 There is an red underline error in the code of child: child. 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
        ),
      ),
    );
  }
}

I am creating this custom widget for use slide and opacity transation on the easiest way.我正在创建此自定义小部件,以最简单的方式使用幻灯片和不透明度转换。 But It is not working.但它不起作用。 How to define child method for the widget?如何为小部件定义子方法?

When you receiving some variable in StatefulWidget constructor, you should use widget.当您在StatefulWidget构造函数中接收到一些变量时,您应该使用widget. to access that.访问它。 So instead of using child use widget.child :所以不要使用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM