Запустить слайд-анимацию при нажатии кнопки флаттер

Как я могу реорганизовать этот код анимации слайдов, чтобы использовать onLongPress на моей кнопке:

@override void initState() {
super.initState();
_controller = AnimationController(
  duration: const Duration(seconds: 2),
  vsync: this,
)..repeat(reverse: true);
_offsetAnimation = Tween<Offset>(
  begin: Offset.zero,
  end: const Offset(1.5, 0.0),
).animate(CurvedAnimation(
  parent: _controller,
  curve: Curves.elasticIn,
));

}

Сейчас анимация воспроизводится автоматически повторно. Я бы хотел, чтобы список скользил только после того, как кнопка была нажата в течение 3 секунд. Это мой код кнопки.

  child: RaisedButton(
                  onPressed: (null),
                  autofocus: false,
                  clipBehavior: Clip.none,
                  color: Colors.red,
                  onLongPress: () => {
                    setState(() {
                      isEmerg = !isEmerg;
                    }),
                    // Navigator.pushReplacement(
                    //     context,
                    //     MaterialPageRoute(
                    //         builder: (context) => AnimationTest()))
                  },
                  child: Text(
                    'Hold for 3 Secs',
                    style: TextStyle(
                        fontWeight: FontWeight.w400,
                        color: Colors.white,
                        fontSize: 24.0,
                        fontFamily: 'Circular'),
                  ),
                  shape: CircleBorder(),
                  padding: EdgeInsets.all(70),
                ),

И это список, который я показываю на LongPress:

  SlideTransition(
          position: _offsetAnimation,
          child: Container(
              child: ListView.builder(
                  itemCount: messages.length,
                  itemBuilder: (context, index) {
                    return Card(
                      child: ListTile(
                        onTap: () {},
                        title: Text(messages[index].emergencyType),
                        subtitle: Text(messages[index].description),
                        leading: Icon(Icons.message_outlined),
                        trailing: Text('Send'),
                        tileColor: Colors.white,
                      ),
                    );
                  })),
        ),

person William Chidube    schedule 25.11.2020    source источник


Ответы (1)


При инициализации изменения

_controller = AnimationController(
  duration: const Duration(seconds: 2),
  vsync: this,
)..repeat(reverse: true);

to

 _controller = AnimationController(
  duration: const Duration(seconds: 2),
  vsync: this,
); 

и onLongPress добавить

_controller.forward();
person faithomotoso    schedule 25.11.2020
comment
Оказывается, анимация вообще остановилась. Список постоянно находится в поле зрения. - person William Chidube; 25.11.2020
comment
Вы хотите, чтобы это повторялось при длительном нажатии? - person faithomotoso; 26.11.2020
comment
Нет, я просто хочу, чтобы он воспроизвелся один раз, но он не воспроизводится вообще. Список просто сидит там без анимации — например, когда загружается экран, список загружается вместе с ним. - person William Chidube; 26.11.2020
comment
Что происходит, когда вы звоните _controller.forward() - person faithomotoso; 26.11.2020