Flutter:动画化小部件的z轴

时间:2019-04-05 23:26:10

标签: flutter flutter-layout

我想为这样的gif动画化小部件。我曾尝试为高程设置动画,但没有按预期工作。还尝试了转换小部件。

enter image description here

2 个答案:

答案 0 :(得分:1)

如果您需要代码,我举个例子:

    class AnimatedSample extends StatefulWidget {
      @override
      _AnimatedSampleState createState() => _AnimatedSampleState();
    }

    class _AnimatedSampleState extends State<AnimatedSample>
        with SingleTickerProviderStateMixin {
      AnimationController _controller;

      @override
      void initState() {
        super.initState();
        _controller = AnimationController(
            vsync: this,
            lowerBound: 0.8,
            upperBound: 1.2,
            duration: Duration(seconds: 1));

        _controller.repeat(reverse: true);
      }

      @override
      void dispose() {
        super.dispose();
        _controller.dispose();
      }

      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Center(
            child: Container(
              margin: EdgeInsets.all(10),
              height: 100,
              color: Colors.grey[400],
              child: Row(children: [
                SizedBox(
                  width: 20,
                ),
                AnimatedBuilder(
                  animation: _controller,
                  child: Icon(
                    Icons.notifications,
                    size: 35,
                    color: Colors.white,
                  ),
                  builder: (context, widget) =>
                      Transform.scale(scale: _controller.value, child: widget),
                ),
                SizedBox(
                  width: 20,
                ),
                Expanded(
                  child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      crossAxisAlignment: CrossAxisAlignment.stretch,
                      children: [
                        Text(
                          "Alert title",
                          style: TextStyle(
                              color: Colors.white,
                              fontSize: 23,
                              fontWeight: FontWeight.w600),
                        ),
                        Text(
                          "Alert text...",
                          style: TextStyle(color: Colors.white, fontSize: 17),
                        ),
                      ]),
                )
              ]),
            ),
          ),
        );
      }
    }

答案 1 :(得分:1)

这是一个ScaleTransition,您无法设置z轴的动画。

class _HomePageState extends State<HomePage> with SingleTickerProviderStateMixin {
    AnimationController _controller;

    @override
    void initState() {
      super.initState();
      _controller = AnimationController(
        vsync: this,
        lowerBound: 0.8,
        upperBound: 1,
        duration: Duration(milliseconds: 500),
      )..repeat(reverse: true);
    }

    @override
    Widget build(BuildContext context) {
      return Scaffold(
        appBar: AppBar(title: Text("Animation")),
        body: Center(
          child: ScaleTransition(scale: _controller, child: FlutterLogo(size: 200)),
        ),
      );
    }
}