When using animation, it is usually written like this
AnimationController _controller;
Animation<double> _animation;
Animation<Offset> _offsetAnimation;
@override
void initState() {
super.initState();
_controller =
AnimationController(vsync: this, duration: Duration(seconds: 2))
..repeat(reverse: true);
_animation = CurvedAnimation(parent: _controller, curve: Curves.easeIn);
_offsetAnimation = Tween<Offset>(begin: Offset.zero, end: Offset(1.5, 0.0))
.animate(_controller);
}
However, Vsync: this in the animationcontroller will report an error (when Vsync exists, it will prevent off screen animation (when the UI of the animation is not on the current screen) from consuming unnecessary resources.)
Here, we need to add singletickerproviderstatemixin to the class definition, and use the statefulwidget class object as the value of this in Vsync. If you want to use a custom state object as Vsync, you can also use tickerproviderstatemixin
Writing method I
class DemoSizeTransition extends StatefulWidget {
@override
_DemoSizeTransitionState createState() => _DemoSizeTransitionState();
}
class _DemoSizeTransitionState extends State<DemoSizeTransition>
{
with SingleTickerProviderStateMixin {
.....//
}
}
Writing method 2
import 'package:flutter/material.dart';
class DemoSizeTransition extends StatefulWidget {
@override
_DemoSizeTransitionState createState() => _DemoSizeTransitionState();
}
class _DemoSizeTransitionState extends State<DemoSizeTransition> with SingleTickerProviderStateMixin
{
AnimationController _controller;
Animation<double> _animation;
Animation<Offset> _offsetAnimation;
@override
void initState() {
super.initState();
_controller =
AnimationController(vsync: this, duration: Duration(seconds: 2))
..repeat(reverse: true);
_animation = CurvedAnimation(parent: _controller, curve: Curves.easeIn);
_offsetAnimation = Tween<Offset>(begin: Offset.zero, end: Offset(1.5, 0.0))
.animate(_controller);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
FadeTransition(
opacity: _animation,
child: FlutterLogo(
size: 150,
),
),
SlideTransition(
position: _offsetAnimation,
child: FlutterLogo(
size: 150,
),
),
],
),
),
);
}
}
Read More:
- The problem of error reporting caused by fluent swift
- [Solved] Flutter Error: No MediaQuery widget ancestor found.
- [Solved] Fluent filesysterexception: exists failed, path =’d: \ as’. Plugins’ (OS error: incorrect file name, directory name or volume slogan)
- Template cannot be rendered due to the joint query of populate in mongoose: syntax error: unexpected token r in JSON at position 0
- Error in node when PM2 starts multiple processes in docker
- [Solved] RuntimeError: Error(s) in loading state_dict for FasterRCNN: Missing key(s) in state_dict:……
- [Solved] RuntimeError: Error(s) in loading state_dict for Model: Missing key(s) in state_dict
- [Solved] Android Error: AAPT: error: unescaped apostrophe in string;Multiple substitutions specified in non-positi
- Notes on atom editor_ (11) Editor previews HTML page in real time (add in: atom HTML preview)
- How to Solve Tabbarview error in Column 2021
- The tree component in easyUI does not display data or displays undefined solutions
- [Solved] MindSpore Error: ValueError: `padding_idx` in `Embedding` out of range
- [Solved] RuntimeError: each element in list of batch should be of equal size
- [Solved] There is no getter for property named ‘distinct’ in…
- [Solved] Websocket Error: WebSocket is already in CLOSING or CLOSED state
- [Solved] redis Error: Can‘t save in background fork Cannot allocate memory
- Eslint Error:“Identifier xxx is not in camel case“
- How to Solve “Status bar could not find cached time string image. Rendering in-process.” in Xcode
- [Solved] Error: Cannot fit requested classes in a single dex file (# methods: 149346 > 65536)
- [Solved] JNI DETECTED ERROR IN APPLICATION: GetStringUTFChars received NULL jstring