Flutter - Animation Text Fade transition - animation

i wanna make a fade transition on two text , what i mean the value change from text to another by animation ... for example one .. two .. and repeat this value again and again
and that's what i tried to make
Container(
alignment: Alignment.center,
width: 150,
height: 50,
child: FadeTransition(
opacity: controller2,
child: Text('Breathe in ',textDirection: TextDirection.ltr,style: TextStyle(color: Colors.white,fontSize: 30,),),
),
color: Colors.red,
),
How Can I Achieve This ?

I think you can solve this using AnimatedOpacity, where it automatically animates, fading opacity in and out.
This sample code stacks 2 widgets, one red and one black, alternating which one have full opacity.
double opacity = 1.0;
#override
void initState() {
super.initState();
changeOpacity();
}
changeOpacity() {
Future.delayed(Duration(seconds: 1), () {
setState(() {
opacity = opacity == 0.0 ? 1.0 : 0.0;
changeOpacity();
});
});
}
Widget build(BuildContext context) {
return Stack(
children: <Widget>[
AnimatedOpacity(
opacity: opacity,
duration: Duration(seconds: 1),
child: Container(
color: Colors.black,
),
),
AnimatedOpacity(
opacity: opacity == 1 ? 0 : 1,
duration: Duration(seconds: 1),
child: Container(
color: Colors.red,
),
),
]
);
}

Related

Slide transition is very slow

My senario is my image and buttons in the center of the screen but my image on the top of them so i'm using stack , then after 2 seconds my image move slightly to the top and my buttons to the bottom.
so i'm using slide transition to move an Image slightly to the top and other buttons to the bottom, and it works but it's very slow even on a real device after release.
is there any solution to fix it or a better approach ?
thanks in advance
my code
class StartView extends StatefulWidget {
#override
_StartViewState createState() => _StartViewState();
}
class _StartViewState extends State<StartView> with TickerProviderStateMixin {
bool showOptions = false;
AnimationController controller;
Animation<Offset> offset;
Animation<Offset> offset2;
Animation<Offset> offset3;
bool _isLoadingVisitor = false;
#override
void initState() {
super.initState();
controller =
AnimationController(vsync: this, duration: Duration(seconds: 1));
offset = Tween<Offset>(begin: Offset.zero, end: Offset(0, -.5))
.animate(controller);
offset2 = Tween<Offset>(begin: Offset.zero, end: Offset(0, .8))
.animate(controller);
offset3 = Tween<Offset>(begin: Offset.zero, end: Offset(0, 2))
.animate(controller);
}
#override
void dispose() {
// TODO: implement dispose
super.dispose();
controller.dispose();
}
#override
Widget build(BuildContext context) {
ScreenUtil.init(context, width: 376, height: 670, allowFontScaling: false);
Future.microtask(()=>Future.delayed(Duration(seconds: 2), () {
controller.forward();
}));
return SafeArea(
child: Scaffold(
body: Container(
child: Stack(
children: <Widget>[
Align(
alignment: Alignment.center,
child: SlideTransition(
position: offset2,
child: RaisedButton(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(5)),
child: Text(
Localization.of(context).trans('login'),
style: TextStyle(
fontSize: ScreenUtil().setSp(20),
fontFamily: 'JFFlatregular'),
),
elevation: 0,
onPressed: login,
),
)),
Align(
alignment: Alignment.center,
child: SlideTransition(
position: offset3,
child: _isLoadingVisitor
? CircularProgressIndicator()
: FlatButton(
child: Text(
Localization.of(context).trans('login_as_visitor'),
style: TextStyle(
fontSize: ScreenUtil().setSp(20),
fontFamily: 'JFFlatregular'),
),
onPressed: loginVisitor,
),
),
),
Align(
alignment: Alignment.center,
child: SlideTransition(
position: offset,
child: Image.asset(
'assets/images/splash.png',
width: ScreenUtil().setWidth(250),
height: ScreenUtil().setHeight(250),
fit: BoxFit.fill,
filterQuality: FilterQuality.high,
),
),
),
],
),
),
),
);
}
}
Image resolution was too high 2040*2040 when i resized it , it works just fine

How to animate AnimatedContainer to fill all available space

Basically i want to animate an AnimatedContainer's height between 2 values. But here is the problem. When my state is 1 i know the height so i can animate but when my state is 0 i want animated container to expand to available space. I tried to wrap my animated container with Expanded widget but that didn't work.
class _PreviewScreenState extends State<PreviewScreen> {
var selectedTab = 1;
#override
Widget build(BuildContext context) {
double imageWidth = MediaQuery.of(context).size.width;
double imageHeight = selectedTab == 1 ? imageWidth : null;
return Scaffold(
body: DefaultTabController(
length: 3,
initialIndex: selectedTab,
child: Background(
child: SafeArea(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
AppBar(
backgroundColor: Colors.transparent,
elevation: 0,
title: Text('SHARE'),
),
Expanded(
child: AnimatedContainer(
height: imageHeight,
duration: Duration(milliseconds: 600),
color: Colors.red,
),
),
TabBar(
labelStyle: TextStyle(fontSize: 13),
indicator: BoxDecoration(
color: Colors.white24,
borderRadius: BorderRadius.circular(40),
),
onTap: (index) {
setState(() {
selectedTab = index;
});
},
tabs: <Widget>[
Tab(child: Text('INSTAGRAM')),
Tab(child: Text('SQUARE')),
Tab(child: Text('OTHER'))
],
),
Container(
height: 100,
child: Center(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 20.0),
child: ShareButton(
onPressed: () {},
),
),
),
)
],
),
),
),
),
);
}
}
You can use a Flexible widget instead of an Expanded widget. It gives the child "the flexibility to expand to fill the available space in the main axis, but, unlike Expanded, Flexible does not require the child to fill the available space."
Also, you should switch from AnimatedContainer to AnimatedSize as AnimatedContainer throws an error interpolating between double.infinity and a constant height.
So this
Expanded(
child: AnimatedContainer(
height: imageHeight,
duration: Duration(milliseconds: 600),
color: Colors.red,
),
),
will be come
Flexible(
child: AnimatedSize(
vsync: this,
duration: Duration(milliseconds: 600),
child: Container(
height: imageHeight,
color: Colors.red,),
),
),
For this to work, your _PreviewScreenState has to use the SingleTickerProviderStateMixin mixin and your imageHeight logic will have to change from null to double.infinity for the filling the available space.
i.e you will have:
class _PreviewScreenState extends State<PreviewScreen> with SingleTickerProviderStateMixin{
//rest of your code
}
and
double imageHeight = selectedTab == 1 ? imageWidth : double.infinity;
Here is a DartPad demonstration: https://dartpad.dev/bf4f969f76ab3092d0b1960bfdbf7825

How to clip the BackdropFilter with smooth edges?

I wanted to apply a BackdropFilter over an image in Flutter. So, I used the following way to apply the filter as given in the Flutter docs.
import 'dart:ui';
import 'package:flutter/material.dart';
void main() {
runApp(
MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
body: MyApp(),
),
),
);
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Stack(
alignment: Alignment.bottomCenter,
children: <Widget>[
Container(
height: 500,
child: Image.network('https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcQlXYdeEKhFq34sh8-0ZKC1uqCcVGgOzdW_ZRAqCBkWxG-oeCB1'),
),
Positioned(
top: 300,
bottom: 0,
left: 0,
right: 0,
child: ClipRect(
child: BackdropFilter(
filter: ImageFilter.blur(sigmaX: 2, sigmaY: 10),
child: Container(
color: Colors.black.withOpacity(0),
),
),
),
),
]
),
),
);
}
}
It produced the following output:
Output of my code
I am getting a hard edge between the BackDropFilter and the Image. Although, I want a smooth edge between them.
How can I achieve something like
this?
I was able to achieve it but it is kind of way out as there is no way to directly implement it as of now.
Make this function for creating the effect.
This function will recieve a list of widgets that you want to blur.
List<Widget> buildBlurredImage(List<Widget> l) {
List<Widget> list = [];
list.addAll(l);
double sigmaX = 0;
double sigmaY = 0.1;
for (int i = 100; i < 350; i += 5) {
// 100 is the starting height of blur
// 350 is the ending height of blur
list.add(Positioned(
top: i.toDouble(),
bottom: 0,
left: 0,
right: 0,
child: ClipRect(
child: BackdropFilter(
filter: ImageFilter.blur(
sigmaX: sigmaX,
sigmaY: sigmaY,
),
child: Container(
color: Colors.black.withOpacity(0),
),
),
),
));
sigmaX += 0.1;
sigmaY += 0.1;
}
return list;
}
then inside your widget class inside stack use the function like this
#override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Stack(
alignment: Alignment.bottomCenter,
// children: <Widget>[],
children: buildBlurredImage([
Container(
height: 500,
child: Image.network(
'https://www.thewowstyle.com/wp-content/uploads/2015/02/Beautiful-Wallpapers-14.jpg',
fit: BoxFit.cover,
),
),
]),
),
),
);
}
Your final Widget class will look like this
class MyApp extends StatelessWidget {
List<Widget> buildBlurredImage(List<Widget> l) {
List<Widget> list = [];
list.addAll(l);
double sigmaX = 0;
double sigmaY = 0.1;
for (int i = 100; i < 350; i += 5) {
// 100 is the starting height of blur
// 350 is the ending height of blur
list.add(Positioned(
top: i.toDouble(),
bottom: 0,
left: 0,
right: 0,
child: ClipRect(
child: BackdropFilter(
filter: ImageFilter.blur(
sigmaX: sigmaX,
sigmaY: sigmaY,
),
child: Container(
color: Colors.black.withOpacity(0),
),
),
),
));
sigmaX += 0.1;
sigmaY += 0.1;
}
return list;
}
#override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Stack(
alignment: Alignment.bottomCenter,
// children: <Widget>[],
children: buildBlurredImage([
Container(
height: 500,
child: Image.network(
'https://www.thewowstyle.com/wp-content/uploads/2015/02/Beautiful-Wallpapers-14.jpg',
fit: BoxFit.cover,
),
),
]),
),
),
);
}
}
The best approach for me would be to used a ShaderMask above the BackdropFilter.
Unfortunately that stop working due to a change in the engine: link
I created a issue on flutter, and I hope they solve the bug soon.

How to animate a widget movement in Flutter?

I am new to animations in Flutter and did not figure out how to animate a widget movement between two states.
If I run the code below, the column's first child will go invisible and the RaisedButton will appear where the first widget was before.
How do I tell Flutter to animate this so that the RaisedButton will move upwards instead of just appearing there?
All solutions I found were way too complicated in my opinion...
bool visible = true;
Widget widget1, widget2;
Column(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Visibility(
visible: visible,
child: widget1,
),
RaisedButton(
child: widget2,
onPressed: () => setState(() => visible = !visible),
),
],
)
I hope this helps
double height = 200;
#override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
AnimatedContainer(
duration: Duration(seconds: 3),
height: height,
child: Container(height: 200, width: 200, color: Colors.blue),
),
RaisedButton(
child: Container(height: 200, width: 200, color: Colors.yellow),
onPressed: () => setState(() => height = height == 0 ? 200 : 0),
),
],
);
}
Here is the result:

How to recreate the Facebook Messenger gradient effect in Flutter

I'm building an application and I want to create an effect similar to this in a ListView.
https://css-tricks.com/recreating-the-facebook-messenger-gradient-effect-with-css/
If I would know the widget's position in the build method, I could calculate the gradient of the widget.
After the widget is rendered, I can get the position of the widget by a GlobalKey that is assigned to the widget in the build method. This approach isn't working in my case, because I need the position in order to render the widget with the correct gradient.
I used ColorFiltered Widget to make Facebook Messenger's gradient.
In a
Stack["gradient you want to apply", "ColorFiltered Widget which is parent of your ListView", ...]
put your Listview in ColorFiltered widget by child, pick filter, and you're done.
I used Firebase Cloud Firestore to make my messenger real-time so there's Streambuildter in my code.
Stack(
alignment: Alignment.center,
children: <Widget>[
Container(
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height,
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: [
Colors.pinkAccent,
Colors.deepPurpleAccent,
Colors.lightBlue,
],
),
),
),
Center(
child: Container(
alignment: Alignment.topCenter,
width: MediaQuery.of(context).size.width,
child: SingleChildScrollView(
controller: _scrollController,
reverse: true,
physics: ClampingScrollPhysics(),
child: StreamBuilder(
stream: Firestore.instance.collection('message').snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return Container(
padding: EdgeInsets.all(100),
color: Colors.transparent,
child: CircularProgressIndicator(
backgroundColor: Colors.white,
),
);
}
return Column(
children: _chatmulticolorbubbles(snapshot.data),
);
},
),
),
),
),
This below makes chatbubblelist.
List<Widget> _chatmulticolorbubbles(data) {
List<Widget> list = [];
list.add(_dumpspace(10.0));
//print(data.documents[0]['chat'].toString());
var _wasme;
list.add(_chatbubble(
data.documents[0]['chat'], data.documents[0]['who'], false));
_wasme = data.documents[0]['who'];
for (var i = 1; i < data.documents.length; i++) {
if (data.documents[i]['who'] == true)
_wasme
? list.add(_chatbubble(
data.documents[i]['chat'], data.documents[i]['who'], true))
: list.add(_chatbubble(
data.documents[i]['chat'], data.documents[i]['who'], false));
else
_wasme
? list.add(_chatbubble(
data.documents[i]['chat'], data.documents[i]['who'], true))
: list.add(_chatbubble(
data.documents[i]['chat'], data.documents[i]['who'], false));
}
list.add(_dumpspace(80.0));
return list;
}
This is my GitHub of this project
Messenger Gradient Github
I hope it helped you!!

Resources