颤动:为小部件设置z轴的动画

问题描述 投票:1回答:2

我想动画像这个gif的小部件。我试图设置高程动画,但没有按预期工作。还试过转换小部件。

flutter flutter-layout
2个回答
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
投票

这是一个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)),
        ),
      );
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.