Flutter custom animated dialog

前端 未结 4 591
轻奢々
轻奢々 2020-12-29 09:09

I\'m trying to animate a custom dialog box in dart so that when it pops up it create some animations. There is a library in Android that is having

4条回答
  •  心在旅途
    2020-12-29 09:24

    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.

    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 createState() => FunkyOverlayState();
    }
    
    class FunkyOverlayState extends State
        with SingleTickerProviderStateMixin {
      AnimationController controller;
      Animation 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!"),
                ),
              ),
            ),
          ),
        );
      }
    }
    

提交回复
热议问题