dart 没有为类型“Widget”定义运算符“[]”,尝试定义运算符“[]”

np8igboo  于 2023-07-31  发布在  其他
关注(0)|答案(3)|浏览(122)

我在下面的代码中有问题,我使用了一个简单的动画Flutter包。此语句中出现问题opacity: animation?["opacity"]。它提示我添加null安全性,所以我这样做了,但是它给出了这个错误The Operator '[]' isn't defined for the type 'Widget'。尝试定义运算符“[]”。如何解决?MovieTween编码为

final tween = MovieTween();
tween.tween('opacity', Tween(begin: 0.0, end: 1.0),
duration: const Duration(milliseconds: 500));

tween.tween('translateY', Tween(begin: 0.0, end: 1.0),
    duration: const Duration(milliseconds: 500),
curve: Curves.easeOut);
return PlayAnimationBuilder<Movie>(
        delay: Duration(milliseconds: (500 * delay).round()),
        tween: tween, // provide tween
        duration: tween.duration,  // total duration obtained from MovieTween
        builder: (context, value, animation) {
          return Opacity(
              opacity: animation?["opacity"],
            child: Transform.translate(
                    offset: Offset(0, animation["translateY"]),
                    child: child
                  ),
          );
        },
    );

字符串
我在下面添加MovieTween类代码:

/// Animates a property and returns the implicitly created scene.
  /// The scene can be used to add further properties to the scene or to
  /// add further scenes to the movie.
  MovieScene thenTween<T>(
    /// Property to animate
    MovieTweenPropertyType property,

    //// Tween that describes the property animation.
    Animatable<T> tween, {

    /// Duration of the scene
    required Duration duration,

    /// Fine-tune the begin time of the next scene by adding a delay.
    /// The value can also be negative.
    Duration delay = Duration.zero,

    /// Custom curve for this property.
    Curve? curve,
    Curve? sceneCurve,
  }) {
    return thenFor(duration: duration, delay: delay, curve: sceneCurve)
        .tween(property, tween, curve: curve);
  }

  /// Adds an additional scene that begins immediately after this scene.
  MovieScene thenFor({
    /// Duration of the scene
    required Duration duration,

    /// Fine-tune the begin time of the next scene by adding a delay.
    /// The value can also be negative.
    Duration delay = Duration.zero,

    /// Custom curve for this scene.
    Curve? curve,
  }) {
    return parent.scene(
      begin: begin + this.duration + delay,
      duration: duration,
      curve: curve,
    );
  }
}

class _SceneItem {
  final MovieTweenPropertyType property;
  final Animatable tween;
  final Curve? curve;
  final Duration shiftBegin;
  final Duration shiftEnd;

  _SceneItem({
    required this.property,
    required this.tween,
    required this.shiftBegin,
    required this.shiftEnd,
    this.curve,
  });
}

/// A snapshot of properties that are animated by a [MovieTween].
/// This class can obtained by using [MovieTween.transform].
class Movie {
  final Map<MovieTweenPropertyType, dynamic> _map;

  Movie({required Map<MovieTweenPropertyType, dynamic> map}) : _map = map;

  /// Returns the value for a given [property].
  V get<V>(MovieTweenPropertyType property) {
    assert(_map.containsKey(property), 'Property $property was not found.');
    return _map[property] as V;
  }
}

class _AbsoluteSceneItem {
  final MovieTweenPropertyType property;
  final Animatable<dynamic> tween;
  final Curve curve;
  final int begin;
  final int end;

  _AbsoluteSceneItem({
    required this.property,
    required this.tween,
    required this.curve,
    required this.begin,
    required this.end,
  });
}

/// Any [Object] can act as a tween property.
typedef MovieTweenPropertyType = Object;

/// Type-safe tween property that can be used as a `const`.
class MovieTweenProperty<T> {
  MovieTweenProperty();

  /// Returns the current value of this property.
  T from(Movie movie) => movie.get(this);
}

ssm49v7z

ssm49v7z1#

尝试(animation as Map? )?["opacity"] as double?? 0

to94eoyn

to94eoyn2#

当我浏览了类似的问题和解决方案,simple_animations文档并阅读了一些代码时,我想我明白了您的代码有什么问题。
It seems likePlayAnimationBuilder中,builder:方法的第三个参数是一个Widget(最好重命名该参数)。我们都知道,我们不能把窗口小部件当作Map。所以,simpleWidget["something"]不起作用。
要设置不透明度,最好的方法是使用value。您可以在value中使用.get()方法来获得所需的配置,或者您也可以直接使用它。
MovieTween动画转换为Movie,它为您提供了一个get()方法来获取单个动画值。
该值表示动画的当前状态,并保存每个属性的插值。按原样使用价值可能是个好主意。

return PlayAnimationBuilder<Movie>(
  delay: Duration(milliseconds: (500 * delay).round()),
  tween: tween,
  duration: tween.duration,
  builder: (context, value, widget) {
    return Opacity(
      opacity: value, // use 'value' directly as the opacity value, not using get() right now
      child: Transform.translate(
        ...
      ),
    );
  },
);

字符串
或者,如果您有一个自订类别,可当做补间动画属性类型安全识别项,您也可以使用这个类别。

return PlayAnimationBuilder<Movie>(
  delay: Duration(milliseconds: (500 * delay).round()),
  tween: tween,
  duration: tween.duration,
  builder: (context, value, widget) {
    return Opacity(
      opacity: value.get(MovieTweenProperty<double>()), // now we use get() <-
      child: Transform.translate(
        offset: Offset(0, value.get(MovieTweenProperty<double>())), // here dont use animate and instead use the value
        child: child,
      ),
    );
  },
);


希望这能解决你的问题。如果您的问题不适用,请随时提供更多信息,以便我们解决您的问题。

enxuqcxy

enxuqcxy3#

builder方法的第三个参数是一个Widget,将其命名为“child”更合适。
为不透明度指定的值可能应该是value。

opacity: value

字符串

相关问题