我怎样才能像这样在 Flutter 中为对话框设置动画

问题描述

我正在尝试为 Flutter 中的警报对话框设置动画,以便在它弹出时显示如下动画。

如何在 alertDialog 中实现 Pokemon Go 的以下外观和行为?

我真的很想在我的应用中加入这个动画。

感谢您的回答!

PokemonGoGif

解决方法

试试这个,修改任何变量以满足您的要求:

import 'package:flutter/material.dart';

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

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

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> with TickerProviderStateMixin {
  AnimationController _controller;
  Animation<Offset> _animation;
  double _width = 20;
  double _height = 200;
  Color _color = Colors.transparent;

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

    _controller = AnimationController(
      duration: const Duration(seconds: 1),vsync: this,);
    _animation = Tween<Offset>(
      begin: const Offset(0.0,1.0),end: const Offset(0.0,-2.0),).animate(CurvedAnimation(
      parent: _controller,curve: Curves.easeInCubic,));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.black,appBar: AppBar(
        actions: [
          IconButton(
            icon: Icon(Icons.send),onPressed: () {
              setState(() {
                _color = Colors.white;
              });
              _controller.forward().then((_) {
                _width = 200;
                setState(() {});
              });
            },),],body: Stack(
        children: [
          Align(
            alignment: Alignment.bottomCenter,child: SlideTransition(
              position: _animation,child: AnimatedContainer(
                width: _width,height: _height,decoration: BoxDecoration(
                  color: _color,borderRadius: BorderRadius.circular(10),duration: Duration(seconds: 1),curve: Curves.fastOutSlowIn,);
  }
}