Animation starts while restarting the app - flutter

I have created a flip animation. The function is when i tap the container it flips. But what i want to have is, I just want to flip the container when onTap is called. But the animation starts while opening the app. What mistake i have mage her and please help me in fixing this. What mistake i have mage her and please help me in fixing this.
class _ChangeContainerState extends State<ChangeContainer>
with TickerProviderStateMixin {
Animation animation;
AnimationController animationController;
Naming selectedContainer = Naming.three;
#override
void initState() {
animationController =
AnimationController(duration: Duration(milliseconds: 500), vsync: this);
animation = Tween(begin: 0.0, end: 1.0).animate(animationController);
super.initState();
}
#override
Widget build(BuildContext context) {
animationController.forward();
return AnimatedBuilder(
animation: animationController,
builder: (BuildContext context, Widget child) {
return Scaffold(
body: Center(
child: selectedContainer == Naming.one
? GestureDetector(
onTap: () {
setState(() {
selectedContainer = Naming.two;
animationController.repeat();
});
},
child: Transform(
transform: Matrix4.identity()
..setEntry(3, 2, 0.002)
..rotateX(pi + (pi * animation.value)),
alignment: FractionalOffset.center,
child: Container(
height: 200,
width: 200,
color: Colors.green,
child: Center(
child: Text(
'Container 2',
style: TextStyle(
fontSize: 30.0, color: Colors.white),
),
),
),
),
)
: selectedContainer == Naming.two
? GestureDetector(
onTap: () {
setState(() {
selectedContainer = Naming.three;
animationController.repeat();
});
},
child: Transform(
transform: Matrix4.identity()
..setEntry(3, 2, 0.002)
..rotateX(pi + (pi * animation.value)),
alignment: FractionalOffset.center,
child: Container(
height: 200,
width: 200,
color: Colors.purpleAccent,
child: Center(
child: Text(
'Container 3',
style: TextStyle(
fontSize: 30.0, color: Colors.white),
),
),
),
),
)
: GestureDetector(
onTap: () {
setState(() {
selectedContainer = Naming.one;
animationController.repeat();
});
},
child: Transform(
transform: Matrix4.identity()
..setEntry(3, 2, 0.002)
..rotateX(
pi + (pi * animation.value),
),
alignment: FractionalOffset.center,
child: Container(
height: 200,
width: 200,
color: Colors.yellow,
child: Center(
child: Text(
'Container 1',
style: TextStyle(fontSize: 30.0),
),
),
),
),
),
),
);
});

Whatever you add inside the init state will run as soon as the page opens. So you want the animation variable to be in the onTap method instead.
You will move your animationController and animation outside the initState method.
Full Code:
class _ChangeContainerState extends State<ChangeContainer>
with TickerProviderStateMixin {
Animation animation;
AnimationController animationController;
Naming selectedContainer = Naming.three;
#override
void initState() {
super.initState();
}
animationController =
AnimationController(duration: Duration(milliseconds: 500), vsync: this);
animation = Tween(begin: 0.0, end: 1.0).animate(animationController);
#override
Widget build(BuildContext context) {
animationController.forward();
return AnimatedBuilder(
animation: animationController,
builder: (BuildContext context, Widget child) {
return Scaffold(
body: Center(
child: selectedContainer == Naming.one
? GestureDetector(
onTap: () {
setState(() {
selectedContainer = Naming.two;
animationController.repeat();
});
},
child: Transform(
transform: Matrix4.identity()
..setEntry(3, 2, 0.002)
..rotateX(pi + (pi * animation.value)),
alignment: FractionalOffset.center,
child: Container(
height: 200,
width: 200,
color: Colors.green,
child: Center(
child: Text(
'Container 2',
style: TextStyle(
fontSize: 30.0, color: Colors.white),
),
),
),
),
)
: selectedContainer == Naming.two
? GestureDetector(
onTap: () {
setState(() {
selectedContainer = Naming.three;
animationController.repeat();
});
},
child: Transform(
transform: Matrix4.identity()
..setEntry(3, 2, 0.002)
..rotateX(pi + (pi * animation.value)),
alignment: FractionalOffset.center,
child: Container(
height: 200,
width: 200,
color: Colors.purpleAccent,
child: Center(
child: Text(
'Container 3',
style: TextStyle(
fontSize: 30.0, color: Colors.white),
),
),
),
),
)
: GestureDetector(
onTap: () {
setState(() {
selectedContainer = Naming.one;
animationController.repeat();
});
},
child: Transform(
transform: Matrix4.identity()
..setEntry(3, 2, 0.002)
..rotateX(
pi + (pi * animation.value),
),
alignment: FractionalOffset.center,
child: Container(
height: 200,
width: 200,
color: Colors.yellow,
child: Center(
child: Text(
'Container 1',
style: TextStyle(fontSize: 30.0),
),
),
),
),
),
),
);
});

Related

dependOnInheritedWidgetOfExactType<_InheritedTheme>() or dependOnInheritedElement() was called before _DetailsState.initState() completed

I am doing animated Appbar and I want to determine color of AppBar on theme of application but when I did this I received this error:
dependOnInheritedWidgetOfExactType<_InheritedTheme>() or dependOnInheritedElement() was called before _DetailsState.initState() completed
here is my initState code, I tried here to determine color depending on theme of application:
late AnimationController _ColorAnimationController;
late AnimationController _TextAnimationController;
late Animation _colorTween, _iconColorTween, _bgLeadingColor;
#override
void initState() {
_ColorAnimationController =
AnimationController(vsync: this, duration: Duration(seconds: 0));
_colorTween = ColorTween(begin: Colors.transparent, end: Theme.of(context).brightness == Brightness.dark ? Colors.black : Colors.white)
.animate(_ColorAnimationController);
_iconColorTween = ColorTween(begin: Colors.grey, end: Colors.white)
.animate(_ColorAnimationController);
_bgLeadingColor = ColorTween(begin: Colors.black26, end: Colors.transparent)
.animate(_ColorAnimationController);
_TextAnimationController =
AnimationController(vsync: this, duration: Duration(seconds: 0));
super.initState();
}
and
Widget build(BuildContext context) {
final size = widget.size;
return Scaffold(
body: NotificationListener<ScrollNotification>(
onNotification: (ScrollNotification scrollInfo) {
if (scrollInfo.metrics.axis == Axis.vertical) {
_ColorAnimationController.animateTo(scrollInfo.metrics.pixels / 350);
_TextAnimationController.animateTo(
(scrollInfo.metrics.pixels - 350) / 50);
return true;
}
return true;
},
child: SafeArea(
child: Stack(
children: [
SingleChildScrollView(
controller: _scrollController,
child: Column(
children: [
SizedBox(
height: size.height * 0.49,
child: PhotoViewer(
/*
*/
)
),
SquareBoxMovableBuilder(
/*
*/
),
],
),
),
Container(
height: size.height * 0.075,
child: AnimatedBuilder(
animation: _ColorAnimationController,
builder: (context, child) => AppBar(
leading: GestureDetector(
onTap: () {
Navigator.of(context).pop();
},
child: Container(
decoration: BoxDecoration(
color: _bgLeadingColor.value,
borderRadius: BorderRadius.circular(10)
),
padding: EdgeInsets.all(size.width * 0.02),
margin: EdgeInsets.only(
top: size.height * 0.01,
left: size.height * 0.01,
right: size.height * 0.01,
bottom: size.height * 0.01,
),
child: Icon(
Icons.arrow_back,
color: Colors.white,
)
),
),
backgroundColor: _colorTween.value,
elevation: 0,
titleSpacing: 0.0,
iconTheme: IconThemeData(
color: _iconColorTween.value,
),
),
),
),
],
),
),
),
);
}
My question is how to avoid this error and still determine color of appbar depending on theme of application?

Flutter Linear progress indicator for each page of pageview builder

I want to build a progress indicator for each page of the pageview builder. Pageview changes after 10 seconds and the progress bar shifts to the next bar. I have successfully implemented this, but encounter an issue of 4 bars loading at the same time, while going back and forth to the page view screen or when I minimize and reopen the app.
This is what I already have achieved with the following code:
This is my code:
class WelcomeScreen extends StatefulWidget {
const WelcomeScreen({Key? key}) : super(key: key);
#override
State<WelcomeScreen> createState() => _WelcomeScreenState();
}
class _WelcomeScreenState extends State<WelcomeScreen>
with TickerProviderStateMixin {
final WelcomeController controller = Get.put(WelcomeController());
late AnimationController animationControllerOne;
late AnimationController animationControllerTwo;
late AnimationController animationControllerThree;
late AnimationController animationControllerFour;
late Animation<double> animation;
#override
void initState() {
super.initState();
animationControllerOne =
AnimationController(duration: const Duration(seconds: 10), vsync: this);
animation = Tween(begin: 0.0, end: 1.0).animate(animationControllerOne)
..addListener(() {
setState(() {
});
});
animationControllerOne.forward();
animationControllerTwo =
AnimationController(duration: const Duration(seconds: 10), vsync: this);
animation = Tween(begin: 1.0, end: 2.0).animate(animationControllerTwo)
..addListener(() {
setState(() {
});
});
Timer(const Duration(seconds: 10), () {
animationControllerTwo.forward();
});
animationControllerThree =
AnimationController(duration: const Duration(seconds: 10), vsync: this);
animation = Tween(begin: 2.0, end: 3.0).animate(animationControllerThree)
..addListener(() {
setState(() {});
});
Timer(const Duration(seconds: 20), () {
animationControllerThree.forward();
});
animationControllerFour =
AnimationController(duration: const Duration(seconds: 10), vsync: this);
animation = Tween(begin: 3.0, end: 4.0).animate(animationControllerFour)
..addListener(() {
setState(() {});
});
Timer(const Duration(seconds: 30), () {
animationControllerFour.forward();
});
animationControllerFour.addStatusListener((status) {
if (status == AnimationStatus.completed) {
animationControllerOne.reset();
animationControllerTwo.reset();
animationControllerThree.reset();
animationControllerFour.reset();
animationControllerOne.forward();
}
animationControllerOne.addStatusListener((status) {
if (status == AnimationStatus.completed) {
animationControllerTwo.reset();
animationControllerTwo.forward();
}
});
animationControllerTwo.addStatusListener((status) {
if (status == AnimationStatus.completed) {
animationControllerThree.reset();
animationControllerThree.forward();
}
});
animationControllerThree.addStatusListener((status) {
if (status == AnimationStatus.completed) {
animationControllerFour.reset();
animationControllerFour.forward();
}
});
});
}
#override
void dispose() {
animationControllerOne.dispose();
animationControllerTwo.dispose();
animationControllerThree.dispose();
animationControllerFour.dispose();
super.dispose();
}
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: context.theme.scaffoldBackgroundColor,
appBar: AppBar(
systemOverlayStyle: SystemUiOverlayStyle(
statusBarColor: Get.isDarkMode ? darkBackground : lightBackground,
statusBarIconBrightness:
Get.isDarkMode ? Brightness.light : Brightness.dark,
),
title: Padding(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: Row(
key: const Key('loading bar'),
children: [
Expanded(
child: LinearProgressIndicator(
color: primaryBlue,
backgroundColor: const Color(0xFFD9D9D9),
minHeight: 8,
value: animationControllerOne.value,
),
),
const SizedBox(
width: 10,
),
Expanded(
child: LinearProgressIndicator(
color: primaryBlue,
backgroundColor: const Color(0xFFD9D9D9),
minHeight: 8,
value: animationControllerTwo.value,
),
),
const SizedBox(
width: 10,
),
Expanded(
child: LinearProgressIndicator(
color: primaryBlue,
backgroundColor: const Color(0xFFD9D9D9),
minHeight: 8,
value: animationControllerThree.value,
),
),
const SizedBox(
width: 10,
),
Expanded(
child: LinearProgressIndicator(
backgroundColor: const Color(0xFFD9D9D9),
color: primaryBlue,
minHeight: 8,
value: animationControllerFour.value,
),
),
],
),
),
elevation: 0,
backgroundColor: Get.isDarkMode ? darkBackground : lightBackground,
centerTitle: true,
),
body: Padding(
padding: const EdgeInsets.symmetric(vertical: 15),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
'Welcome to Demonstration Bank!'.tr,
key: const Key('message'),
textAlign: TextAlign.center,
style: TextStyle(
color: Get.isDarkMode ? lightBackground : darkBackground,
fontSize: Get.height * 0.027,
fontWeight: FontWeight.w600,
),
),
SizedBox(
height: Get.height * 0.05,
),
SizedBox(
height: Get.height * 0.4,
width: Get.width * 0.8,
child: PageView.builder(
controller: controller.gifsController,
physics: const NeverScrollableScrollPhysics(),
itemCount: controller.images.length,
scrollDirection: Axis.horizontal,
itemBuilder: (context, index) {
return Image.asset(
controller.images[index],
fit: BoxFit.fitWidth,
);
},
),
),
SizedBox(
height: Get.height * 0.15,
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
CustomButton(
key: const Key('sign in button'),
height: Get.height * 0.069,
width: Get.width * 0.335,
fontSize: Get.height * 0.021,
color: Get.isDarkMode ? lightBackground : darkBackground,
textColor: Get.isDarkMode ? darkBackground : lightBackground,
text: 'Sign in'.tr,
onPressed: () => Get.to(() => const SigninScreen()),
),
const SizedBox(
width: 19,
),
CustomButton(
key: const Key('sign up button'),
height: Get.height * 0.069,
width: Get.width * 0.335,
fontSize: Get.height * 0.021,
color: primaryBlue,
textColor: Colors.white,
text: 'Sign Up'.tr,
onPressed: () => Get.to(() => CreateAccountScreen()),
),
],
),
const SizedBox(
height: 24,
),
],
),
),
);
}
}
Unexpected result: ( Note: this problem does not happen always )

How to implement multiple AnimationControllers in flutter GetxController

I want to implement multiple animations with animation controllers by using getXController in flutter dart, but when I run the application it says animation controllers are paused and when I click on one of my TextFormFields animation controllers will work for just one second, and all of the data that I have entered in TextFormFields are removed.
This is my code LoginController code.
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
class LoginController extends GetxController with GetTickerProviderStateMixin //
{
late final Rx<AnimationController> _controller1;
late final Rx<AnimationController> _controller2;
late Rx<Animation<double>> _animation1;
late Rx<Animation<double>> _animation2;
late Rx<Animation<double>> _animation3;
late Rx<Animation<double>> _animation4;
get controller1 => _controller1.value;
set controller1(value) => _controller1.value = value;
get controller2 => _controller2.value;
set controller2(value) => _controller2.value = value;
get animation1 => _animation1.value;
set animation1(value) => _animation1.value = value;
get animation2 => _animation2.value;
set animation2(value) => _animation2.value = value;
get animation3 => _animation3.value;
set animation3(value) => _animation3.value = value;
get animation4 => _animation4.value;
set animation4(value) => _animation4.value = value;
#override
void onInit() {
super.onInit();
_controller1 = AnimationController(
vsync: this,
duration: const Duration(
seconds: 5,
),
).obs;
_animation1 = (Tween<double>(begin: .1, end: .15).animate(
CurvedAnimation(
parent: controller1,
curve: Curves.easeInOut,
),
)
..addListener(() => update())
..addStatusListener(
(status) {
if (status == AnimationStatus.completed) {
update([controller1.reverse()]);
} else if (status == AnimationStatus.dismissed) {
update([controller1.forward()]);
}
},
))
.obs;
_animation2 = (Tween<double>(begin: .02, end: .04).animate(
CurvedAnimation(
parent: controller1,
curve: Curves.easeInOut,
),
)..addListener(() => update()))
.obs;
_controller2 = AnimationController(
vsync: this,
duration: const Duration(
seconds: 5,
),
).obs;
_animation3 = (Tween<double>(begin: .41, end: .38).animate(
CurvedAnimation(
parent: controller2,
curve: Curves.easeInOut,
),
)
..addListener(() => update())
..addStatusListener(
(status) {
if (status == AnimationStatus.completed) {
update([controller2.reverse()]);
} else if (status == AnimationStatus.dismissed) {
update([controller2.forward()]);
}
},
))
.obs;
_animation4 = (Tween<double>(begin: 170, end: 190).animate(
CurvedAnimation(
parent: controller2,
curve: Curves.easeInOut,
),
)..addListener(() => update()))
.obs;
Timer(
const Duration(milliseconds: 2500),
() {
update([controller1.reverse()]);
},
);
update([controller1.forward()]);
}
#override
void onReady() {
super.onReady();
}
#override
void onClose() {
controller1.dispose();
controller2.dispose();
super.onClose();
}
}
and this is my GetViewPage.
void main() async {
WidgetsFlutterBinding.ensureInitialized();
runApp(
const MaterialApp(
home: LoginPage(),
title: 'Login Page',
),
);
SystemChrome.setSystemUIOverlayStyle(
const SystemUiOverlayStyle(
statusBarColor: Colors.transparent,
statusBarIconBrightness: Brightness.light,
),
);
}
class LoginPage extends GetView<LoginController> {
final String? action;
const LoginPage({Key? key, this.action}) : super(key: key);
#override
Widget build(BuildContext context) {
Size size = MediaQuery.of(context).size;
final _userController = TextEditingController();
final _passwordController = TextEditingController();
return Scaffold(
backgroundColor: darkBackGround,
body: Form(
child: ScrollConfiguration(
behavior: Behavior(),
child: SingleChildScrollView(
child: SizedBox(
height: size.height,
child: Stack(
children: [
Positioned(
top: size.height * (controller.animation2.value + .58),
left: size.width * .21,
child: CustomPaint(
painter: CustomizedPainter(
radius: 50,
color1: darkLinearCustomPaintColor1,
color2: darkLinearCustomPaintColor2,
),
),
),
Positioned(
top: size.height * .98,
left: size.width * .1,
child: CustomPaint(
painter: CustomizedPainter(
radius: controller.animation4.value - 30,
color1: darkLinearCustomPaintColor1,
color2: darkLinearCustomPaintColor2,
),
),
),
Positioned(
top: size.height * .5,
left: size.width * (controller.animation2.value + .8),
child: CustomPaint(
painter: CustomizedPainter(
radius: 30,
color1: darkLinearCustomPaintColor1,
color2: darkLinearCustomPaintColor2,
),
),
),
Positioned(
top: size.height * controller.animation3.value,
left: size.width * (controller.animation1.value + .1),
child: CustomPaint(
painter: CustomizedPainter(
radius: 60,
color1: darkLinearCustomPaintColor1,
color2: darkLinearCustomPaintColor2,
),
),
),
Positioned(
top: size.height * .1,
left: size.width * .8,
child: CustomPaint(
painter: CustomizedPainter(
radius: controller.animation4.value,
color1: darkLinearCustomPaintColor1,
color2: darkLinearCustomPaintColor2,
),
),
),
Column(
children: [
Expanded(
flex: 5,
child: Padding(
padding: EdgeInsets.only(top: size.height * .1),
child: Text(
'',
style: TextStyle(
color: Colors.black.withOpacity(.7),
fontSize: 30,
fontWeight: FontWeight.bold,
letterSpacing: 1,
wordSpacing: 4,
fontFamily: 'pacific',
),
),
),
),
Expanded(
flex: 7,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
DarkThemeInput(
icon: Icons.account_circle_outlined,
hintText: 'User',
isPassword: false,
isEmail: false,
isPhone: false,
controller: _userController,
validator: (String? value) {
if (value!.isEmpty) {
return 'Enter your username or Email ';
}
return '';
},
),
DarkThemeInput(
icon: Icons.lock_outline,
hintText: 'Password...',
isPassword: true,
isEmail: false,
isPhone: false,
controller: _passwordController,
validator: (String? value) {
if (value!.isEmpty) {
return 'Password must be filled';
}
if (value.length < 6) {
return 'Enter more characters';
}
return '';
}),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
DarkThemeButton(
text: 'LOGIN', width: 2.58, onTap: () {}),
SizedBox(width: size.width / 20),
DarkThemeButton(
text: 'Forgotten password!',
width: 2.58,
onTap: () {},
),
],
),
],
),
),
Expanded(
flex: 6,
child: Column(
mainAxisAlignment: MainAxisAlignment.end,
children: [
DarkThemeButton(
text: 'Create a new Account',
width: 2,
onTap: () {
Get.toNamed('/signup');
},
),
SizedBox(height: size.height * .05),
],
),
),
],
),
],
),
),
),
),
),
);
}
}
I want to notice that DarkThemeInput is a customized TextFormField and when I press them animation controllers work just for one second.

how to make a container draggable in horizontal direction in flutter

How do I get a draggable container like this, which when reaches a certain point gets vanished and a function could be called at that time. Also, the visibility of text is also decreasing and arrow icon is rotating according to the length. How can I achieve this functionality?
I have written a code, and I can add visibility widget for same. But, how to constraint it and what is the best way of doing it? Please help me out -
import 'package:flutter/material.dart';
class SwipeButton extends StatefulWidget {
const SwipeButton({Key? key}) : super(key: key);
#override
_SwipeButtonState createState() => _SwipeButtonState();
}
class _SwipeButtonState extends State<SwipeButton> {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Container(
color: Colors.orange,
width: MediaQuery.of(context).size.width * 0.9,
height: 50,
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 8.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Draggable<int>(
data: 10,
feedback: Container(
height: 20.0,
width: 20.0,
color: Colors.white,
child: const Center(
child: Icon(
Icons.arrow_forward,
size: 12,
),
),
),
axis: Axis.horizontal,
childWhenDragging: Container(
height: 20.0,
width: 20.0,
color: Colors.orange,
child: const Center(
child: Text(''),
),
),
child: Container(
height: 20.0,
width: 20.0,
color: Colors.white,
child: const Center(
child: Icon(
Icons.arrow_forward,
size: 12,
),
),
),
),
DragTarget<int>(
builder: (
BuildContext context,
List<dynamic> accepted,
List<dynamic> rejected,
) {
return Container(
height: 20.0,
width: 20.0,
color: Colors.orange,
child: const Center(
child: Text(''),
),
);
},
onAccept: (int data) {
setState(() {
print(data);
});
},
),
],
),
),
),
),
);
}
}
For the arrow, we can use Transform.rotate.
Transform.rotate(
angle: deg2rad(-(180 + 20) * (posX / maxWidth)), // 20 comes from icon size
child: const Icon(
Icons.arrow_forward,
size: 12,
),
),
I'm not sure why onDragUpdate is not updating the UI, you can get current position of drag from that, I store it on posX.
I am using GestureDetector for test case, apply the same logic you will get the result.
Run on dartPad
class SwipeButton extends StatefulWidget {
const SwipeButton({Key? key}) : super(key: key);
#override
_SwipeButtonState createState() => _SwipeButtonState();
}
class _SwipeButtonState extends State<SwipeButton> {
double deg2rad(double deg) => deg * pi / 180;
double posX = 0.0;
#override
Widget build(BuildContext context) {
final maxWidth = MediaQuery.of(context).size.width * 0.9;
return Scaffold(
body: Center(
child: Container(
color: Colors.orange,
width: maxWidth * 0.9,
height: 50,
child: Stack(
alignment: Alignment.center,
children: [
AnimatedPositioned(
duration: const Duration(milliseconds: 100),
left: posX,
key: const ValueKey("item 1"),
child: Container(
height: 20.0,
width: 20.0,
color: Colors.white,
alignment: Alignment.center,
child: Transform.rotate(
angle: deg2rad(-(180 + 20) * (posX / maxWidth)),
child: const Icon(
Icons.arrow_forward,
size: 12,
),
),
),
),
GestureDetector(
onPanEnd: (details) {
setState(() {
posX = 0;
});
},
onPanUpdate: (details) {
setState(() {
posX = details.localPosition.dx;
});
if (posX / maxWidth > .95) {
debugPrint("on success drag");
}
},
)
],
),
),
),
);
}
}

how to fix inverted 'ok' text in flatbutton?

I'm beginner in flutter. trying to design login page. when user clicks on 'ok' button then 'ok' button gets invisible and 'approved' named button gets visible using some animation. problem is that 'ok' displayed in inverted form as shown in pic. how dI correct this issue?
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
// TODO: implement build
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Animation class',
home: HomePage(),
);
}
}
class HomePage extends StatefulWidget {
#override
State<StatefulWidget> createState() => stateClass();
}
class stateClass extends State<HomePage> with SingleTickerProviderStateMixin {
AnimationController animationController;
Animation<double> animation;
Animation<double> sizeAnimation;
int currentState = 0;
#override
void initState() {
super.initState();
animationController = AnimationController(
duration: Duration(milliseconds: 1000), vsync: this);
animation = Tween<double>(begin: 0, end: 60).animate(animationController)
..addListener(() {
setState(() {});
});
sizeAnimation = Tween<double>(begin: 0, end: 1).animate(CurvedAnimation(
parent: animationController, curve: Curves.fastOutSlowIn))
..addListener(() {
setState(() {});
});
}
#override
Widget build(BuildContext context) {
// TODO: implement build
return Scaffold(
appBar: AppBar(
title: Text('Animation login'),
),
body: Container(
child: SingleChildScrollView(
child: Column(
children: <Widget>[
Container(
height: 100,
//color: Colors.black,
child: Center(
child: Image.asset('assets/fluttericon.png'),
)),
Container(
margin: EdgeInsets.all(10),
padding: EdgeInsets.all(10),
child: TextFormField(
decoration: InputDecoration(
labelText: 'enter your email id',
),
),
),
Container(
margin: EdgeInsets.all(10),
padding: EdgeInsets.all(10),
child: TextFormField(
obscureText: true,
decoration: InputDecoration(
labelText: 'enter your password',
),
),
),
Container(
// color: Colors.teal,
// height: 0,
child: Center(
child: Transform.scale(
scale: sizeAnimation.value - 1,
child: FlatButton(
onPressed: animationController.forward,
color: Colors.redAccent[200],
child: Text(
'ok',
style: TextStyle(fontSize: 17, color: Colors.black),
),
),
)),
),
Container(
//color: Colors.teal,
height: 80,
child: Center(
child: Transform.scale(
scale: sizeAnimation.value,
child: FlatButton(
onPressed: animationController.reverse,
color: Colors.redAccent[200],
child: Text(
'approved',
style: TextStyle(fontSize: 17, color: Colors.black),
),
),
)),
),
],
),
)
)
);
}
}
Text container
Container(
// color: Colors.teal,
// height: 0,
child: Center(
child: Transform.scale(
scale: sizeAnimation.value-1 ,
child: FlatButton(
onPressed: animationController.forward,
color: Colors.redAccent[200],
child: Text(
'ok',
style: TextStyle(fontSize: 17, color: Colors.black),
),
),
)),
),
When I change sizeAnimation.value-1 to sizeAnimation. then 'ok' word is in erect form. but ok button is not invisible.
Screenshot preview
A possible solution for what you are trying to do is to use Fade and Rotate transitions with separate controllers for each Text widget. Here's an example of that:
class ButtonTextFadeRotation extends StatefulWidget {
#override
_ButtonTextFadeRotationState createState() => _ButtonTextFadeRotationState();
}
class _ButtonTextFadeRotationState extends State<ButtonTextFadeRotation> with TickerProviderStateMixin {
AnimationController _okAnimationController;
AnimationController _approvedAnimationController;
#override
void initState() {
_okAnimationController = AnimationController(
vsync: this,
duration: Duration(milliseconds: 200),
value: 1
);
_approvedAnimationController = AnimationController(
vsync: this,
duration: Duration(milliseconds: 200),
value: 0
);
super.initState();
}
#override
Widget build(BuildContext context) {
return Center(
child: ClipRect(
child: RaisedButton(
onPressed: () {
_okAnimationController.reverse();
_approvedAnimationController.forward();
},
child: Stack(
alignment: Alignment.center,
children: <Widget>[
FadeTransition(
opacity: _okAnimationController,
child: RotationTransition(
turns: _okAnimationController,
alignment: Alignment.center,
child: Text('Ok'),
),
),
FadeTransition(
opacity: _approvedAnimationController,
child: RotationTransition(
turns: _approvedAnimationController,
alignment: Alignment.center,
child: Text('Approved'),
),
),
],
),
),
),
);
}
}