Created
June 1, 2022 03:19
-
-
Save gucci-ninja/4698166233ab078a2ff979b45a4673bb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:rive/rive.dart'; | |
void main() { | |
runApp(const MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
const MyApp({Key? key}) : super(key: key); | |
// This widget is the root of your application. | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: const BasketBallAnimation(), | |
); | |
} | |
} | |
class BasketBallAnimation extends StatefulWidget { | |
const BasketBallAnimation({Key? key}) : super(key: key); | |
@override | |
State<BasketBallAnimation> createState() => _BasketBallAnimationState(); | |
} | |
class _BasketBallAnimationState extends State<BasketBallAnimation> { | |
late RiveAnimationController _controller; | |
bool get isPlaying => _controller.isActive; | |
void _toggleAnimation() { | |
setState(() { | |
_controller.isActive = !_controller.isActive; | |
}); | |
} | |
@override | |
void initState() { | |
super.initState(); | |
_controller = SimpleAnimation('idle'); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
body: Center( | |
child: RiveAnimation.asset('animations/basketball.riv', | |
controllers: [_controller])), | |
floatingActionButton: FloatingActionButton( | |
onPressed: _toggleAnimation, | |
tooltip: isPlaying ? 'Pause' : 'Play', | |
child: Icon( | |
isPlaying ? Icons.pause : Icons.play_arrow, | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment