flutter 淡入/淡出动画不工作:状态不是参数

6bc51xsx  于 2023-08-07  发布在  Flutter
关注(0)|答案(1)|浏览(150)

我试图应用淡入过渡到屏幕上的图像,但面临错误,这说,我正在使用的状态不是一个参数。
我是按照官方教程:https://www.youtube.com/watch?v=rLwWVbv3xDQ
资料来源:

class _MyFadeInState extends State<MyFadeIn>
    with SingleTickerProviderStateMixin {
  // ignore: inference_failure_on_function_return_type
  MyFadeIn({@required this.child});

  late AnimationController _controller;
  late Animation _animation;

  @override
  void initState() {
    _controller = AnimationController(
      vsync: this,
      duration: Duration(seconds: 2),
    );
    _animation = Tween(
      begin: 0.0,
      end: 1.0,
    ).animate(_controller);
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext, context) {
    _controller.forward();
    createState() => _MyFadeInState();
    final controller = AnimationController(
      vsync: this,
      duration: Duration(seconds: 2),
    );
    final animation = Tween(
      begin: 0.0,
      end: 1.0,
    ).animate(controller);
    controller.forward();
    return FadeTransition(opacity: _animation, child: widget.child);
  }
}

字符串

svgewumm

svgewumm1#

这里有一个实现,它会在传入MyFadeIn的任何子部件中淡出(见注解):

/// Stateful widgets are composed of two classes: [StatefulWidget] and [State].
///
/// The [StatefulWidget] creates its state object in the [createState] method.
///
/// [MyFadeIn] <<<<<<<<<<<< This is the stateful widget.
/// [_MyFadeInState] <<<<<< This is the associated state object type.
class MyFadeIn extends StatefulWidget {
  const MyFadeIn({super.key, required this.child});

  final Widget child;

  /// Note that there isn't a [build] method here. Only a [createState] method.
  @override
  State<MyFadeIn> createState() => _MyFadeInState();
}

class _MyFadeInState extends State<MyFadeIn> with TickerProviderStateMixin {
  /// We will mark this as 'final', because once initialized in 'initState', it
  /// will never change.
  late final AnimationController _controller;

  /// Specify a type for the generic type argument of Animation<T>.
  ///
  /// In this case, since we're animating from 0.0 to 1.0, it is 'double'.
  ///
  /// Marked as final; see above.
  late final Animation<double> _animation;

  @override
  void initState() {
    super.initState();

    _controller = AnimationController(
      vsync: this,
      duration: const Duration(seconds: 2),
    );

    _animation = Tween(
      begin: 0.0,
      end: 1.0,
    ).animate(_controller);
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  /// The state object is the one that overrides the [build] method.
  @override
  Widget build(BuildContext context) {
    _controller.forward();

    /// 1. This is not necessary, because we will use '_controller' instead.
    ///
    /// Notes:
    /// Additionally, instantiating a second 'AnimationController' will cause
    /// an exception because this class uses a 'SingleTickerProviderStateMixin'.
    ///
    /// A 'SingleTickerProviderStateMixin' is a 'TickerProviderStateMixin' that
    /// only supports a single ticker; think of this as only supporting a single
    /// AnimationController.

    // final controller = AnimationController(
    //   vsync: this,
    //   duration: const Duration(seconds: 2),
    // );

    /// 2. This is not necessary, because we will use '_animation' instead.

    // final animation = Tween(
    //   begin: 0.0,
    //   end: 1.0,
    // ).animate(controller);

    return FadeTransition(opacity: _animation, child: widget.child);
  }
}

字符串
使用方法:

MyFadeIn(child: Container(color: Colors.pink))
///             ^^^^^ Replace with your image.


我看到了一些关于widget的误解,所以我建议学习以下内容:https://docs.flutter.dev/ui/widgets-intro

相关问题