Ошибки кода FadeAnimation при попытке обновить simple_animations: ^2.2.1 после миграции с использованием sa_v1_migration: ^1.1.2
Я пытаюсь создать анимацию Fade, и она работает, она вызывает проблемы и говорит, что она устарела, и мне нужно выполнить миграцию:
У меня три проблемы:
MultiTrackTween устарел и не должен использоваться. См. Руководство по миграции: https://pub.dev/packages/sa_v1_migration. Попробуйте заменить устаревший элемент на замену.
"Track" устарел и не должен использоваться. См. Руководство по миграции: https://pub.dev/packages/sa_v1_migration. Попробуйте заменить устаревший элемент на замену.
"Track" устарел и не должен использоваться. См. Руководство по миграции: https://pub.dev/packages/sa_v1_migration. Попробуйте заменить устаревший элемент на замену.
следующий код для моего FadeAnimation.dart:
import 'package:flutter/material.dart';
import 'package:simple_animations/simple_animations.dart';
class FadeAnimation extends StatelessWidget {
final double delay;
final Widget child;
FadeAnimation(this.delay, this.child, {AssetImage image});
@override
Widget build(BuildContext context) {
final tween = MultiTrackTween([
Track("opacity").add(Duration(milliseconds: 500), Tween(begin: 0.0, end: 1.0)),
Track("translateY").add(
Duration(milliseconds: 500), Tween(begin: -30.0, end: 0.0),
curve: Curves.easeOut)
]);
return PlayAnimation(
delay: Duration(milliseconds: (500 * delay).round()),
duration: tween.duration,
tween: tween,
child: child,
builder: (context, child, animation) => Opacity(
opacity: animation["opacity"],
child: Transform.translate(
offset: Offset(0, animation["translateY"]),
child: child
),
),
);
}
}
а вот мой pubspec.yaml:
name: MyApp
description: A new Flutter project.
# The following line prevents the package from being accidentally published to
# pub.dev using `pub publish`. This is preferred for private packages.
publish_to: 'none' # Remove this line if you wish to publish to pub.dev
# The following defines the version and build number for your application.
# A version number is three numbers separated by dots, like 1.2.43
# followed by an optional build number separated by a +.
# Both the version and the builder number may be overridden in flutter
# build by specifying --build-name and --build-number, respectively.
# In Android, build-name is used as versionName while build-number used as versionCode.
# Read more about Android versioning at https://developer.android.com/studio/publish/versioning
# In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion.
# Read more about iOS versioning at
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
version: 1.0.0+1
environment:
sdk: ">=2.7.0 <3.0.0"
dependencies:
flutter:
sdk: flutter
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^0.1.3
simple_animations: ^2.2.1
sa_v1_migration: ^1.1.2
google_fonts: ^1.1.0
dev_dependencies:
flutter_test:
sdk: flutter
change_app_package_name: ^0.1.2
flutter_launcher_icons: ^0.7.5
flutter_icons:
android: "launcher_icon"
ios: true
image_path: "assets/icon/icon.png"
# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec
# The following section is specific to Flutter.
flutter:
# The following line ensures that the Material Icons font is
# included with your application, so that you can use the icons in
# the material Icons class.
uses-material-design: true
# To add assets to your application, add an assets section, like this:
# assets:
assets:
- assets/images/
# - images/a_dot_burr.jpeg
# - images/a_dot_ham.jpeg
# An image asset can refer to one or more resolution-specific "variants", see
# https://flutter.dev/assets-and-images/#resolution-aware.
# For details regarding adding assets from package dependencies, see
# https://flutter.dev/assets-and-images/#from-packages
# To add custom fonts to your application, add a fonts section here,
# in this "flutter" section. Each entry in this list should have a
# "family" key with the font family name, and a "fonts" key with a
# list giving the asset and other descriptors for the font. For
# example:
# fonts:
# - family: Schyler
# fonts:
# - asset: fonts/Schyler-Regular.ttf
# - asset: fonts/Schyler-Italic.ttf
# style: italic
# - family: Trajan Pro
# fonts:
# - asset: fonts/TrajanPro.ttf
# - asset: fonts/TrajanPro_Bold.ttf
# weight: 700
#
# For details regarding fonts from package dependencies,
# see https://flutter.dev/custom-fonts/#from-packages
Я не могу обновить код таким образом, чтобы получить желаемый результат. Может ли кто-нибудь помочь мне обновить этот код, чтобы я не получал устаревшие ошибки.
5 ответов
Вы можете скопировать и вставить полный код ниже.
Вы можете определитьenum AniProps
и использовать MultiTween<AniProps>
фрагмент кода
enum AniProps { opacity, translateY }
class FadeAnimation extends StatelessWidget {
final double delay;
final Widget child;
FadeAnimation(this.delay, this.child, {AssetImage image});
@override
Widget build(BuildContext context) {
final tween = MultiTween<AniProps>()
..add(AniProps.opacity, 0.0.tweenTo(1.0), 500.milliseconds)
..add(AniProps.translateY, (-30.0).tweenTo(0.0), 500.milliseconds,
Curves.easeOut);
return PlayAnimation<MultiTweenValues<AniProps>>(
delay: Duration(milliseconds: (500 * delay).round()),
duration: tween.duration,
tween: tween,
child: child,
builder: (context, child, value) => Opacity(
opacity: value.get(AniProps.opacity),
child: Transform.translate(
offset: Offset(0, value.get(AniProps.translateY)), child: child),
),
);
}
}
...
FadeAnimation(
10.0,
Container(
height: 100,
width: 100,
color: Colors.blue,
)),
рабочая демонстрация
полный код
import 'package:flutter/material.dart';
import 'package:simple_animations/simple_animations.dart';
import 'package:supercharged/supercharged.dart';
enum AniProps { opacity, translateY }
class FadeAnimation extends StatelessWidget {
final double delay;
final Widget child;
FadeAnimation(this.delay, this.child, {AssetImage image});
@override
Widget build(BuildContext context) {
final tween = MultiTween<AniProps>()
..add(AniProps.opacity, 0.0.tweenTo(1.0), 500.milliseconds)
..add(AniProps.translateY, (-30.0).tweenTo(0.0), 500.milliseconds,
Curves.easeOut);
/*
final tween = MultiTrackTween([
Track("opacity").add(Duration(milliseconds: 500), Tween(begin: 0.0, end: 1.0)),
Track("translateY").add(
Duration(milliseconds: 500), Tween(begin: -30.0, end: 0.0),
curve: Curves.easeOut)
]);*/
return PlayAnimation<MultiTweenValues<AniProps>>(
delay: Duration(milliseconds: (500 * delay).round()),
duration: tween.duration,
tween: tween,
child: child,
builder: (context, child, value) => Opacity(
opacity: value.get(AniProps.opacity),
child: Transform.translate(
offset: Offset(0, value.get(AniProps.translateY)), child: child),
),
);
}
}
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
FadeAnimation(
10.0,
Container(
height: 100,
width: 100,
color: Colors.blue,
)),
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}
Если вы обновитесь до v5, вы можете использовать код
class FadeAnimation extends StatelessWidget {
final double delay;
final Widget child;
FadeAnimation(this.delay, this.child);
@override
Widget build(BuildContext context) {
final tween = MovieTween()
..tween('opacity', Tween(begin: 0.0, end: 1.0),
duration: const Duration(milliseconds: 500), curve: Curves.easeIn)
..tween(
'translateY',
duration: const Duration(milliseconds: 500),
Tween(begin: -30.0, end: 0.0),
curve: Curves.easeOut);
return CustomAnimationBuilder(
delay: Duration(milliseconds: (500 * delay).round()),
duration: tween.duration,
tween: tween,
child: child,
builder: (BuildContext context, Movie value, Widget? child) {
return Opacity(
opacity: value.get("opacity"),
child: Transform.translate(
offset: Offset(0.0, value.get("translateY")), child: child
)
);
});
}
}
Попробуйте этот код, он заработает. Я внес изменения, чтобы удалить устаревшее использование.
enum AnimationType { opacity, translateX }
class FadeAnimation extends StatelessWidget {
final double delay;
final Widget child;
const FadeAnimation(this.delay, this.child);
@override
Widget build(BuildContext context) {
final tween = MultiTween<AnimationType>()
..add(AnimationType.opacity, Tween(begin: 0.0, end: 1.0),
Duration(milliseconds: 500),)
..add(
AnimationType.translateX,
Tween(begin: 30.0, end: 1.0),
Duration(milliseconds: 500),
);
return PlayAnimation<MultiTweenValues<AnimationType>>(
delay: Duration(milliseconds: (500 * delay).round()),
duration: tween.duration,
tween: tween,
child: child,
builder: (context, child, value) => Opacity(
opacity: value.get(AnimationType.opacity),
child: Transform.translate(
offset: Offset(value.get(AnimationType.translateX), 0), child: child),
),
);
}
}
class FadeAnimation extends StatelessWidget {
final double delay;
final Widget child;
FadeAnimation(this.delay, this.child);
@override
Widget build(BuildContext context) {
final tween = new MultiTrackTween([
new Track <dynamic>("opacity")
.add(Duration(milliseconds: 500), Tween(begin: 0.0, end: 1.0)),
Track("translateY").add(
Duration(milliseconds: 500), Tween(begin: -100.0, end: 0.0),
curve: Curves.easeOut)
]);
return ControlledAnimation(
delay: Duration(milliseconds: (500 * delay).round()),
duration: tween.duration,
tween: tween,
child: child,
builderWithChild: (context, child, animation) => Opacity(
opacity: animation["opacity"],
child: Transform.translate(offset: Offset(0, animation["translateY"])),
),
);
}
}