简体   繁体   English

如何在 Flutter 中使用 animation 更改行/列状态?

[英]How to change row/column states with animation in Flutter?

I am using rows and columns in my layout and load some data from the internet to display information inside my rows and columns.我在布局中使用行和列,并从 Internet 加载一些数据以在我的行和列中显示信息。

I want to design a dynamic loading page in such a way that every loaded data makes a particular widget visible with an animation (by moving the other widgets around smoothly).我想设计一个动态加载页面,使每个加载的数据都使一个特定的小部件在 animation 中可见(通过平滑地移动其他小部件)。

I am currently using if clauses inside my layouts and calling setState() when new data is retrieved.我目前在我的布局中使用 if 子句,并在检索新数据时调用setState()

Column(
  children: [
    SomeWidget(),
    if (data != null)
      DataWidget(),
    AnotherWidget(),
  ],
),

How can I insert widgets between other widgets in rows/columns with animation after some data is retrieved?检索到某些数据后,如何使用 animation 在行/列中的其他小部件之间插入小部件?

Flutter already has tons of useful widgets which you can use for this implementation. Flutter 已经有大量有用的小部件可用于此实现。 I believe that the AnimatedList widget solves your problem.我相信AnimatedList小部件可以解决您的问题。 I have added the widget of the week video and a basic example below.我在下面添加了本周视频的小部件和一个基本示例。

Widget of the Week - AnimatedList本周小部件 - AnimatedList

Example:例子:

import 'package:flutter/material.dart';

class PageOne extends StatefulWidget {
  @override
  _PageOneState createState() => _PageOneState();
}

class _PageOneState extends State<PageOne> {
  /// The global key to access the animated list.
  final _animatedListKey = GlobalKey<AnimatedListState>();

  List<String> _items = [];

  @override
  void initState() {
    // Set the items that should be display.
    _items = ['A', 'B', 'D', 'E', 'F'];
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Example')),
      body: AnimatedList(
        key: _animatedListKey,
        initialItemCount: _items.length,
        itemBuilder: (context, index, animation) {
          return SlideTransition(
            position: animation.drive(
              // Tween that slides from right to left.
              Tween(begin: Offset(1.0, 0.0), end: Offset(0.0, 0.0)),
            ),
            // Simply display the letter.
            child: ListTile(title: Text(_items[index])),
          );
        },
      ),
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.add),
        onPressed: () {
          // The item to insert.
          final _item = 'C';

          // Add, sort, and retrieve the index of the inserted item.
          List<String> _temp = _items..add(_item);
          _temp.sort();
          final _index = _temp.indexOf(_item);

          // Update the state and start the animated list animation.
          setState(() {
            _items.insert(_index, _item);
            _animatedListKey.currentState?.insertItem(_index);
          });
        },
      ),
    );
  }
}

you can always use AnimatedSwitcher to animate between widgets, just setState and change _widget:您总是可以使用 AnimatedSwitcher 在小部件之间设置动画,只需 setState 并更改 _widget:

AnimatedSwitcher(
  duration: Duration(milliseconds: 200),
  transitionBuilder: (Widget child, Animation<double> animation) {
    var tween=Tween<Offset>(begin: Offset(1, 0), end: Offset(0, 0))
     return SlideTransition(
       child: child,
       position: tween.animate(animation),
    );
  },
  child: _widget,
)

OR using flutter_staggered_animations with Column & Row showing animation或使用带有显示animation的列和行的flutter_staggered_animations

@override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SingleChildScrollView(
        child: AnimationLimiter(
          child: Column(
            children: AnimationConfiguration.toStaggeredList(
              duration: const Duration(milliseconds: 375),
              childAnimationBuilder: (widget) => SlideAnimation(
                horizontalOffset: 50.0,
                child: FadeInAnimation(
                  child: widget,
                ),
              ),
              children: YourColumnChildren(),
            ),
          ),
        ),
      ),
    );
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM