2

我有一个由一个或两个元素组成的行。根据后者,我想为第一个元素的宽度设置动画,使其要么完全展开,要么适应第二个元素。

我在行内使用 AnimatedContainers 来实现这一点,但没有动画。我认为这可能与 Flutter 在根据其父级的状态重新渲染后未将其识别为相同有关,因此我尝试使用唯一键。然而,这也不起作用。

class TabsBar extends StatelessWidget {
  TabsBar({this.currentIndex, this.onIndexChanged, Key key}) : super(key: key);
  final int currentIndex;
  final Function(int) onIndexChanged;

  @override
  Widget build(BuildContext context) {
    final tabsBloc = BlocProvider.of<TabsBarBloc>(context);

  return BlocBuilder<TabsBarBloc, TabsBarBlocState>(
    builder: (context, tabsBarState) {
  return SafeArea(
    child: Padding(
      padding: const EdgeInsets.symmetric(horizontal: standardSpacing),
      child: Row(
        children: [
          Expanded(
            child: AnimatedContainer(
              key: key,
              height: 60,
              duration: Duration(milliseconds: 200),
              //....//
          ),
          //HERE: IF INDEX IS == 2, I WANT THE CONTAINER ABOVE TO ANIMATE INTO ITS NEW SIZE WHICH IS LESS WIDE TO ACCOMMODATE FOR THE CONTAINER BELOW
          currentIndex == 2
              ? GestureDetector(
                  onTap: () {},
                  behavior: HitTestBehavior.translucent,
                  child: AnimatedContainer(
                    duration: Duration(milliseconds: 200),
                    padding: const EdgeInsets.only(left: standardSpacing),
                    height: 60,
                    child: Image.asset(
                      '${imagesPath}add_piece.png',
                    ),
                  ),
                )
              : HorizontalSpacing(0),
        ],
      ),
    ),
  );
});

我还尝试使用 Layout Builder 显式定义宽度,并使用 Animated Container 作为 Row 的父级。都没有奏效。

任何人都可以指出我可能会错过什么吗?

4

1 回答 1

2

我不完全理解 AnimatedContainer 的值是什么你试图改变,但它可能更容易只为该行的第二个容器设置动画并将其宽度更改为 0,以便它从屏幕上消失

class Home extends StatefulWidget {
  @override
  State<Home> createState() => HomeState();
}

class HomeState extends State<Home> {
  int currentIndex = 2; //or you could use a boolean type

  @override
  Widget build(BuildContext context) {
    return Column(mainAxisSize: MainAxisSize.min, children: [
      MyWidget(
        currentIndex: currentIndex,
      ),
      FlatButton(
          onPressed: () => setState(() {
                currentIndex = currentIndex == 1 ? 2 : 1;
              }),
          child: Text('Change'))
    ]);
  }
}

class MyWidget extends StatelessWidget {
  final int currentIndex;
  static const double standardSpacing = 8.0;

  MyWidget({this.currentIndex, Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Padding(
        padding: const EdgeInsets.symmetric(horizontal: standardSpacing),
        child: Row(
          children: [
            Expanded(
             child: Container( //Normal Container
               height: 60,
               color: Colors.blue)
            ),
            GestureDetector(
              onTap: () {},
              behavior: HitTestBehavior.translucent,
              child: AnimatedContainer(
                duration: Duration(milliseconds: 200),
                padding: const EdgeInsets.only(left: standardSpacing),
                height: 60,
                width: currentIndex == 2 ? 36 : 0, //when currentIndex is not 2, it's size its zero
                child: const Icon(Icons.save),
              ),
            )
          ],
        ),
      ),
    );
  }
}
于 2020-06-21T22:22:43.727 回答