Flutter custom animated dialog

前端 未结 4 596
轻奢々
轻奢々 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:38

    Whenever you want to show Dialog with some Animation, the best way is to use showGeneralDialog()

    NOTE: ALL PARAMETERS MUST BE PROVIDED OTHERWISE SOME ERROR WILL OCCUR.

    showGeneralDialog(
                    barrierColor: Colors.black.withOpacity(0.5), //SHADOW EFFECT
                    transitionBuilder: (context, a1, a2, widget) {
                      return Center(
                        child: Container(
                          height: 100.0 * a1.value,  // USE PROVIDED ANIMATION
                          width: 100.0 * a1.value,
                          color: Colors.blue,
                        ),
                      );
                    },
                    transitionDuration: Duration(milliseconds: 200), // DURATION FOR ANIMATION
                    barrierDismissible: true,
                    barrierLabel: 'LABEL',
                    context: context,
                    pageBuilder: (context, animation1, animation2) {
                      return Text('PAGE BUILDER');
                    });
              }, child: Text('Show Dialog'),),
    

    If you need more customization, then extend PopupRoute and create your own _DialogRoute and showGeneralDialog()

    EDIT

    Edited answer of Niklas with functionality to close Overlay :)

    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: () {
                  OverlayEntry overlayEntry;
                  overlayEntry = OverlayEntry(builder: (c) {
                    return FunkyOverlay(onClose: () => overlayEntry.remove());
                  });
                  Overlay.of(context).insert(overlayEntry);
                },
                icon: Icon(Icons.message),
                label: Text("PopUp!")),
          ),
        );
      }
    }
    
    class FunkyOverlay extends StatefulWidget {
      final VoidCallback onClose;
    
      const FunkyOverlay({Key key, this.onClose}) : super(key: key);
    
      @override
      State createState() => FunkyOverlayState();
    }
    
    class FunkyOverlayState extends State
        with SingleTickerProviderStateMixin {
      AnimationController controller;
      Animation opacityAnimation;
      Animation scaleAnimatoin;
    
      @override
      void initState() {
        super.initState();
    
        controller =
            AnimationController(vsync: this, duration: Duration(milliseconds: 450));
        opacityAnimation = Tween(begin: 0.0, end: 0.4).animate(
            CurvedAnimation(parent: controller, curve: Curves.fastOutSlowIn));
        scaleAnimatoin =
            CurvedAnimation(parent: controller, curve: Curves.elasticInOut);
    
        controller.addListener(() {
          setState(() {});
        });
    
        controller.forward();
      }
    
      @override
      Widget build(BuildContext context) {
        return Material(
          color: Colors.black.withOpacity(opacityAnimation.value),
          child: Center(
            child: ScaleTransition(
              scale: scaleAnimatoin,
              child: Container(
                decoration: ShapeDecoration(
                    color: Colors.white,
                    shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(15.0))),
                child: Padding(
                  padding: const EdgeInsets.all(50.0),
                  child: OutlineButton(onPressed: widget.onClose, child: Text('Close!'),),
                ),
              ),
            ),
          ),
        );
      }
    }
    

提交回复
热议问题