繁体   English   中英

Flutter 中的自定义 StateFull 小部件

[英]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.

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