Start slide-in animation on button press flutter

1.8k views Asked by At

How can I refactor this slide-in animation code to use the onLongPress on my button:

@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,
));

}

Right now the animation plays automatically repeatedly. I'd like for the list to slide in only after the button has been pressed for 3 seconds. This is my button code.

  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),
                ),

And this is the list I am displaying onLongPress:

  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,
                      ),
                    );
                  })),
        ),
1

There are 1 answers

4
faithomotoso On BEST ANSWER

At init change

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

to

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

and onLongPress add

_controller.forward();