how can i make bell notification icon animated in flutter - flutter

I want to make an animation on a notification bell icon to make it rings like these animations:
https://icons8.com/animated-icons/bell
or one of these
https://csshint.com/css-notification-bell-icon/
how this can be achieved with flutter?

This animates the flutter alarm icon to shake a couple times you can play some of the variable to get the effect how you want it. This is like the second example you provided as for the first you would need a custom icon where you could animate just that part of the icon.
AnimationController _animationController;
#override
void initState() {
_animationController =
AnimationController(vsync: this, duration: Duration(milliseconds: 500));
super.initState();
}
void _runAnimation() async {
for (int i = 0; i < 3; i++) {
await _animationController.forward();
await _animationController.reverse();
}
}
#override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
RotationTransition(
turns: Tween(begin: 0.0, end: -.1)
.chain(CurveTween(curve: Curves.elasticIn))
.animate(_animationController),
child: Icon(Icons.alarm)),
RaisedButton(
child: Text('Run Animation'),
onPressed: () => _runAnimation(),
)
],
),
),
);
If you need further explanation on how this works let me know.

You can use this animated_widget flutter package: animated_widget flutter package
For Shake:
ShakeAnimatedWidget(
enabled: this._enabled,
duration: Duration(milliseconds: 1500),
shakeAngle: Rotation.deg(z: 40),
curve: Curves.linear,
child: FlutterLogo(
style: FlutterLogoStyle.stacked,
),
),

Related

Flutter package to create circle instagram story animation

I am creating an app, it has story parts like Instagram. I'm searching a package for it. Loading circle animation.
You can you this package from pubdev
dashed_circle: ^0.0.1
class _MyPageState extends State<MyPage> with SingleTickerProviderStateMixin {
Variables
Animation gap;
Animation base;
Animation reverse;
AnimationController controller;
#override
void initState() {
super.initState();
controller = AnimationController(vsync: this, duration: Duration(seconds: 4));
base = CurvedAnimation(parent: controller, curve: Curves.easeOut);
reverse = Tween<double>(begin: 0.0, end: -1.0).animate(base);
gap = Tween<double>(begin: 3.0, end: 0.0).animate(base)
..addListener(() {
setState(() {});
});
controller.forward();
}
#override
void dispose() {
controller.dispose();
super.dispose();
}
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
body: Container(
alignment: Alignment.center,
child: RotationTransition(
turns: base,
child: DashedCircle(
gapSize: gap.value,
dashes: 40,
color: Color(0XFFED4634),
child: RotationTransition(
turns: reverse,
child: Padding(
padding: const EdgeInsets.all(5.0),
child: CircleAvatar(
radius: 80.0,
backgroundImage: NetworkImage(
"https://images.unsplash.com/photo-1564564295391-7f24f26f568b"
),
),
),
),
),
),
),
);
}
}
I think the best way would be to export the animation and use lottie package to render it on screen.

Tween animation flutter

I want to play an animation when a button is clicked. On the first press, the widget rotates 180 degrees, on the second press, another 180 degrees (that is, it returns to its original position). How can I do this?
Simulated gesture detector button
Expanded(
child: GestureDetector(
onTap: () => setState(() {
if (tapValue == 0) {
tapValue++;
animController.forward();
beginValue = 0.0;
endValue = 0.5;
} else {
tapValue--;
animController.forward();
}
}),
child: Container(
child: Image.asset('assets/images/enableAsset.png'),
),
),
),
The widget I want to rotate
child: CustomPaint (
painter: SmileyPainter(),
child: RotationTransition(
turns: Tween(begin: beginValue, end: endValue,).animate(animController),
child: CustomPaint (
painter: Smile(),
),
),
)
animation controller
#override
void initState() {
animController = AnimationController(
duration: const Duration(milliseconds: 400),
vsync: this,
);
super.initState();
}
If what you want to achieve is only to rotate a widget, I would recommend avoiding a controller. Not only will this simplify your code but it will also save you the chore of disposing it.
I have come to realize that pretty much any controller can be avoided using the TweenAnimationBuilder widget.
Here is an example of how to to make it work for your case:
Scaffold(
floatingActionButton: FloatingActionButton(
onPressed: () {
_rotationAngle += pi;
print(_rotationAngle);
setState(() {
});
},
),
body: Center(
child: TweenAnimationBuilder(
duration: Duration(milliseconds: 300),
tween: Tween<double>(begin: 0, end: _rotationAngle),
builder: (BuildContext context, double value, Widget child) {
return Transform.rotate(
angle: value,
child: child,
);
},
child: Container(
height: 500,
width: 50,
color: Colors.redAccent,
),
),
),
);
just replace
else {tapValue--;
animController.forward();
}
to
else {tapValue--;
animController.reverce();
}

Animations with curve property in flutter

In my app, I have a container that I want to start rotating with a slow-curve on a click, then keep rotation, and then the next click will make it stop with a slow-curve.
How do I make a curve-animation in flutter?
Somthing like this:
https://miro.medium.com/max/960/1*ZLekwO4QthfAWlBgM-9vpA.gif
Make an animation controller and an animation.
AnimationController _animController;
Animation<double> _animation;
#override
void initState() {
super.initState();
_animController = AnimationController(
duration: Duration(seconds: 2),
vsync: this,
);
_animation =
Tween<double>(begin: 0, end: 2 * math.pi).animate(_animController)
..addListener(() {
setState(() {});
});
}
#override
void dispose() {
_animController.dispose();
super.dispose();
}
Define a boolean variable. This variable indicates whether the object is animating or not.
var _animating = false;
End the animation on Stop and repeat the animation on Start.
Scaffold(
backgroundColor: Colors.blueGrey,
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Transform.rotate(
angle: _animation.value,
child: Container(
color: Colors.green,
height: 80,
width: 80,
padding: EdgeInsets.all(30),
),
),
Padding(
padding: const EdgeInsets.only(top: 20.0),
child: RaisedButton(
color: Colors.white,
child: Text(_animating ? "Stop" : "Start"),
onPressed: () {
if (_animating) {
_animController.animateTo(1,
duration: Duration(seconds: 3), curve: Curves.ease);
} else {
_animController.repeat();
}
setState(() => _animating = !_animating);
},
),
),
],
),
),
)
Result:

How to center an Item inside ListView

Suppose I have a List of integer numbers between 0-1000
suppose picture below:
but instead of fruits , I want to show the number.
now I have 3 problem:
1:as you know at last the item must be centered in its box , how can I do that inside ListView
2:I should only controll the ListView with controllers and user shouldn't move it.
3:how to gradually spin it ?
this is my code:
class SpinnerChance extends StatefulWidget {
#override
_SpinnerChanceState createState() => _SpinnerChanceState();
}
class _SpinnerChanceState extends State<SpinnerChance>
with SingleTickerProviderStateMixin {
AnimationController _animationController;
Animation<double> _animation1;
Animation<double> _animation2;
Animation<double> _animation3;
#override
void initState() {
super.initState();
_animationController = AnimationController(
vsync: this,
duration: Duration(seconds: 5),
);
_animation1 = Tween(begin: 0.0, end: 100.0).animate(
CurvedAnimation(
parent: _animationController,
curve: Curves.fastOutSlowIn,
),
)..addListener(() {
setState(() {});
});
_animation2 = Tween(begin: 0.0, end: 30.0).animate(
CurvedAnimation(
parent: _animationController,
curve: Curves.fastOutSlowIn,
),
)..addListener(() {
setState(() {});
});
_animation3 = Tween(begin: 0.0, end: 10.0).animate(
CurvedAnimation(
parent: _animationController,
curve: Curves.fastOutSlowIn,
),
)..addListener(() {
setState(() {});
});
}
List<Widget> items = [];
#override
void dispose() {
_animationController.dispose();
super.dispose();
}
#override
Widget build(BuildContext context) {
return Container(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Container(
child: Text(_animation1?.value?.toStringAsFixed(1) ??
"You Dont Spin It Yet"),
),
Container(
child: Text(_animation2?.value?.toStringAsFixed(1) ??
"You Dont Spin It Yet"),
),
Container(
child: Text(_animation3?.value?.toStringAsFixed(1) ??
"You Dont Spin It Yet"),
),
],
),
RaisedButton(
child: Text("Spin!"),
onPressed: () {
_animationController.forward();
},
)
],
),
);
}
}
I could imagine there's a lot of ways to create the effect you want. It's basically a slot machine but instead of fruits, you use numbers.
I found this tutorial which answers most of your questions on how to achieve the animation and as for how to turn it into numbers, you could use Text widgets instead of images since you would only need the numbers 0-9.
This isn't completely connected to ListViews in general but if you want to center its item then wrap the specific widget you want to center inside an Align widget, something like so:
Align(
alignment: Alignment.center
child: yourWidgetHere
),

AnimatedContainer Rotation gives unwanted rotation

I have been struggling with Flutter with something that in my head seems to simple. I want a container that spins when pressing a button and this should be animated. I have a container in the center of my screen. I have 2 buttons, 1 with "+" and with a "-". When I press the "+" I want the container to rotate 180 degrees in a clockwise rotation, If I press the "+" again I want it to perform another clockwise rotation of 180 degrees. When pressing "-" I want it to spin counter clockwise for 180 degrees.
Currently I have it build so that it will rotate container however the axis is on the top left point of the container instead of the center. I have tried to tackle this but nothing seems to change this behaviour and I found this issue but it's since been closed
https://github.com/flutter/flutter/issues/27419
It's mind boggling to me that I cannot perform such a simple operation and was wondering if someone knows where I'm going wrong.
Some code:
import 'package:flutter/material.dart';
import 'dart:math';
void main() => runApp(Spinner());
class Spinner extends StatefulWidget {
#override
_SpinnerState createState() => _SpinnerState();
}
class _SpinnerState extends State<Spinner> {
double _angle = 0;
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Center(
child: AnimatedContainer(
alignment: FractionalOffset.center,
height: 200,
width: 200,
transform: Matrix4.rotationZ(_angle),
decoration: BoxDecoration(
color: Colors.blue
),
duration: Duration(seconds: 2),
),
),
floatingActionButton: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.end,
children: <Widget>[
FloatingActionButton(onPressed: () {
setState(() {
_angle += 180 * pi / 180;
});
},
child: const Icon(Icons.add),
),
Container(
height: 20,
),
FloatingActionButton(onPressed: () {
setState(() {
_angle -= 180 * pi / 180;
});
},
child: const Icon(Icons.remove),
)
],
)
)
);
}
}
Edit:
I did find this post however when using it the container instantly snaps to the new position and I want this to be animted.
How can I rotate a Container widget in 2D around a specified anchor point?
I found a solution to tackle this problem, instead of using the animatedContained I decided to use an Animation Controller.
my SpinnerState class now looks like this:
class _SpinnerState extends State<RadialMenu> with SingleTickerProviderStateMixin {
AnimationController controller;
#override
void initState() {
super.initState();
controller = AnimationController(duration: Duration(milliseconds: 900), vsync: this);
}
#override
Widget build(BuildContext context) {
return RadialAnimation(controller: controller);
}
}
I then created a stateless widget which contains the animations etc.
class SpinnerAnimation extends StatelessWidget {
SpinnerAnimation({Key key, this.controller})
: rotation = Tween<double>(
begin: 0.0,
end: 180.0,
).animate(
CurvedAnimation(
parent: controller,
curve: Interval(
0.0,
0.75,
curve: Curves.linear,
),
),
),
super(key: key);
final AnimationController controller;
final Animation<double> rotation;
build(context) {
return AnimatedBuilder(
animation: controller,
builder: (context, builder) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
FloatingActionButton(
child: Icon(Icons.add),
onPressed: _open,
backgroundColor: Colors.green),
FloatingActionButton(
child: Icon(Icons.remove),
onPressed: _close,
backgroundColor: Colors.red),
Transform.rotate(
angle: radians(rotation.value),
child: Column(
children: [
Container(
width: 200,
height: 200,
decoration: new BoxDecoration(
image: DecorationImage(
image: AssetImage('images/CRLogo.png'))),
)
],
))
],
);
});
}
_open() {
controller.forward(from: 0);
}
_close() {
controller.reverse(from: 1);
}
}
This has at least solved this problem for me and I hope it helps someone else in the future.