【问题标题】:Flutter: how can I implement an animated Container from left to right?Flutter:如何实现从左到右的动画容器?
【发布时间】:2021-09-01 10:02:34
【问题描述】:
  • 我实现了一个房间聊天屏幕,在其中一个动画容器从左到右移动并在完成后重新开始,有人可以告诉我如何实现它

【问题讨论】:

    标签: flutter dart animation containers chat


    【解决方案1】:

    你需要循环动画容器,那么你可以尝试一些Marquee的包,比如fast_marquee

    Marquee(
      text: 'Some sample text that takes some space.',
      style: TextStyle(color: Colors.blue, fontWeight: FontWeight.bold),
      velocity: 100,
      blankSpace: 10,
      startPadding: 10,
      reverse: true,
      bounce: true,
      startAfter: const Duration(seconds: 2),
      pauseAfterRound: const Duration(seconds: 1),
      numberOfRounds: 5,
      showFadingOnlyWhenScrolling: false,
      fadingEdgeStartFraction: 0.05,
      fadingEdgeEndFraction: 0.05,
      curve: Curves.easeInOut,
    )
    

    试试AnimatedPositioned:

    import 'package:flutter/material.dart';
    
    void main() => runApp(const MyApp());
    
    /// This is the main application widget.
    class MyApp extends StatelessWidget {
      const MyApp({Key? key}) : super(key: key);
    
      static const String _title = 'Flutter Code Sample';
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: _title,
          home: Scaffold(
            appBar: AppBar(title: const Text(_title)),
            body: const Center(
              child: MyStatefulWidget(),
            ),
          ),
        );
      }
    }
    
    /// This is the stateful widget that the main application instantiates.
    class MyStatefulWidget extends StatefulWidget {
      const MyStatefulWidget({Key? key}) : super(key: key);
    
      @override
      State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
    }
    
    /// This is the private State class that goes with MyStatefulWidget.
    class _MyStatefulWidgetState extends State<MyStatefulWidget> {
      bool selected = false;
    
      @override
      Widget build(BuildContext context) {
        return SizedBox(
          width: 200,
          height: 350,
          child: Stack(
            children: <Widget>[
              AnimatedPositioned(
                width: selected ? 200.0 : 50.0,
                height: selected ? 50.0 : 200.0,
                top: selected ? 50.0 : 150.0,
                duration: const Duration(seconds: 2),
                curve: Curves.fastOutSlowIn,
                child: GestureDetector(
                  onTap: () {
                    setState(() {
                      selected = !selected;
                    });
                  },
                  child: Container(
                    color: Colors.blue,
                    child: const Center(child: Text('Tap me')),
                  ),
                ),
              ),
            ],
          ),
        );
      }
    }
    

    【讨论】:

    • 这是一个可移动的容器,从左到右移动,然后从左到右重新开始
    猜你喜欢
    • 1970-01-01
    • 2015-07-01
    • 1970-01-01
    • 2016-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-04
    相关资源
    最近更新 更多