animatedPositioned не анимируется на Flutter - PullRequest
0 голосов
/ 18 февраля 2020

Мне нужно анимировать виджет, и я использовал AnimatedPositioned. но не анимированные. просто измените положение без анимации

Полный код

Column(
      children: <Widget>[
        Container(
          height: 50,
            width: width,
            color: Palette.white,
          child: Padding(
            padding: const EdgeInsets.only(left:20.0),
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Container(
                  width: 200,
                  height: 40,
                  decoration: BoxDecoration(
                    color: Palette.lightSilver,
                    borderRadius: BorderRadius.all(Radius.circular(20))
                  ),
                  child: Stack(
                    children: <Widget>[
                      AnimatedPositioned(
                        right:check? 0:null,
                          left:check?null: 0,
                          child: InkWell(
                            onTap: (){
                              setState(() =>
                                check = !check
                              );
                            },
                            child: Container(
                              width: 110,
                              height: 40,
                              decoration: BoxDecoration(
                                  color: Palette.darkSilver2,
                                  borderRadius: BorderRadius.all(Radius.circular(20))
                              ),
                            ),
                          ),
                        duration: const Duration(milliseconds: 50000),
                        curve: Curves.bounceOut,
                      )
                    ],
                  ),
                ),
              ],
            ),
          )
        ),

Ответы [ 2 ]

1 голос
/ 19 февраля 2020

Вы можете анимировать, используя только левое или правое свойство анимированного контейнера.

Container(
                  width: 200,
                  height: 60,
                  decoration: BoxDecoration(
                      color: Colors.red,
                      borderRadius: BorderRadius.all(Radius.circular(20))),
                  child: Stack(
                    children: <Widget>[
                      AnimatedPositioned(
                        left: check
                            ? 90
                            : 0, // here 90 is (200(above container)-110(container which is animating))
                        child: InkWell(
                          onTap: () {
                            setState(() {
                              check = !check;
                            });
                          },
                          child: Container(
                            width: 110,
                            height: 40,
                            decoration: BoxDecoration(
                                color: Colors.teal,
                                borderRadius:
                                    BorderRadius.all(Radius.circular(20))),
                          ),
                        ),
                        duration: const Duration(seconds: 1),
                        curve: Curves.bounceOut,
                      ),
                    ],
                  ),
                ),
0 голосов
/ 19 февраля 2020

Вам просто нужно установить расстояние, не равное нулю, со стороны справа и слева , чтобы контейнер не Прыжок , I немного изменил код (попробуйте):

Column(
      children: <Widget>[
        Container(
            height: 50,
            width: 400,
            color: Colors.green,
            child: Padding(
              padding: const EdgeInsets.only(left: 20.0),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Container(
                    width: 300,
                    height: 40,
                    decoration: BoxDecoration(
                        color: Colors.grey,
                        borderRadius:
                            BorderRadius.all(Radius.circular(20))),
                    child: Stack(
                      children: <Widget>[
                        AnimatedPositioned(
                          right: check ? 0 : 200,
                          left: check ? 200 : 0,
                          onEnd: () {
                            print('done');
                          },
                          child: InkWell(
                            onTap: () {
                              setState(() => check = !check);
                            },
                            child: Container(
                              width: 50,
                              height: 40,
                              decoration: BoxDecoration(
                                  color: Colors.black54,
                                  borderRadius: BorderRadius.all(
                                      Radius.circular(20))),
                            ),
                          ),
                          duration: const Duration(milliseconds: 5000),
                          curve: Curves.bounceOut,
                        )
                      ],
                    ),
                  ),
                ],
              ),
            )),
      ],
    ),
...