Animate a Custom Dialog

Flutter custom animated dialog

To create dialog boxes you can use the Overlay or Dialog classes. If you want to add animations like in the given framework you can use the AnimationController like in the following example. The CurvedAnimation class is used to create the bouncing effect on the animation.

Update: In general it is better to show dialogs with the showDialog function, because the closing and gesture are handled by Flutter. I have updated the example, it is now running with showDialog and you are able to close the dialog by tapping on the background.

Bouncing Dialog Animation

You can copy & paste the following code into a new project and adjust it. It is runnable on it's own.

import 'package:flutter/material.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(title: 'Flutter Demo', theme: ThemeData(), home: Page());
}
}

class Page extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: RaisedButton.icon(
onPressed: () {
showDialog(
context: context,
builder: (_) => FunkyOverlay(),
);
},
icon: Icon(Icons.message),
label: Text("PopUp!")),
),
);
}
}

class FunkyOverlay extends StatefulWidget {
@override
State<StatefulWidget> createState() => FunkyOverlayState();
}

class FunkyOverlayState extends State<FunkyOverlay>
with SingleTickerProviderStateMixin {
AnimationController controller;
Animation<double> scaleAnimation;

@override
void initState() {
super.initState();

controller =
AnimationController(vsync: this, duration: Duration(milliseconds: 450));
scaleAnimation =
CurvedAnimation(parent: controller, curve: Curves.elasticInOut);

controller.addListener(() {
setState(() {});
});

controller.forward();
}

@override
Widget build(BuildContext context) {
return Center(
child: Material(
color: Colors.transparent,
child: ScaleTransition(
scale: scaleAnimation,
child: Container(
decoration: ShapeDecoration(
color: Colors.white,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15.0))),
child: Padding(
padding: const EdgeInsets.all(50.0),
child: Text("Well hello there!"),
),
),
),
),
);
}
}

Animation not working in custom Dialog

Problem is with the interpolator value. Used accelarate interpolator with 5000 duration which is too long for it. Setting the value of duration to 1000 makes the code work perfectly!

Animation not working the in custom dialog

Try this:

slide_down_animation.xml

<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="@android:integer/config_mediumAnimTime"
android:fromYDelta="-100%p"
android:interpolator="@android:anim/accelerate_interpolator"
android:toYDelta="0%p" />
</set>

and

slide_up_animation.xml

<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="@android:integer/config_mediumAnimTime"
android:fromYDelta="0%p"
android:interpolator="@android:anim/accelerate_interpolator"
android:toYDelta="-100%p" />
</set>

EDIT:

Apart from that, you can also try setting your style this way:

getWindow().setWindowAnimations(R.style.DialogAnimation);

(NOT R.style.DialogSlideAnim)

Add load animation to custom dialog in kotlin

style.xml

<style name="yourCustomDialog" parent="@android:style/Theme.Dialog">
<item name="android:windowAnimationStyle">@style/yourCustomDialogAnimation</item>
</style>

<style name="yourCustomDialogAnimation">
<item name="android:windowEnterAnimation">@anim/slide_down</item>
<item name="android:windowExitAnimation">@anim/slide_up</item>
</style>

</resources>

How to use it in kotlin code

val dialogBuilder = AlertDialog.Builder(activity,R.style.yourCustomDialog)

How to animate a dialog to slide from the bottom of a screen to the middle without bounce?

I figured out how to do this. Instead of using Dialog, it is better to use DialogFragment. For whatever reason, my Dialog implementation is correct, it just does not work with animations. Here is my implementation:

public class NetworkDialog extends DialogFragment {

@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(STYLE_NO_FRAME, android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
}

@Override
public void onActivityCreated(Bundle arg0) {
super.onActivityCreated(arg0);
getDialog().getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
}

@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
final View v = inflater.inflate(R.layout.dialog_network, container, false);
v.setAlpha(0.5f);
final ImageButton ibRefresh = (ImageButton) v.findViewById(R.id.ib_refresh);
ibRefresh.setClickable(true);

ibRefresh.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MapActivity.rotateImageView(ibRefresh);
}
});

return v;
}

@Override
public void onResume() {
super.onResume();
}

@Override
public void onPause() {
super.onPause();
}
}

And then I call the dialog with

NetworkDialog dialogFragment = new NetworkDialog();
dialogFragment.show(getFragmentManager(), "ProgressDialog");


Related Topics



Leave a reply



Submit