In this code i try to make a simple increasing number with SlideTransition
but it doesn’t work correctly, i could only use ScaleTransition
successful. that means i want to increase number with SlideTransition
Gif example of what i want to create:
please see second
part as animation
class TestScreen extends StatefulWidget {
const TestScreen({super.key});
@override
State<TestScreen> createState() => _TestScreenState();
}
class _TestScreenState extends State<TestScreen> with SingleTickerProviderStateMixin {
int num = 0;
late final AnimationController _animationController = AnimationController(
duration: Duration(milliseconds: 750),
vsync: this)
..forward();
static final offset1 = Tween<Offset>(begin: Offset.zero, end: Offset(0, -1));
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
body: Container(
width: double.infinity,
height: double.infinity,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
AnimatedSwitcher(
duration: Duration(milliseconds: 1000),
transitionBuilder: (child, animation) => SlideTransition(
position: (offset1).animate(animation),
child: child,
),
child: Text(
key: ValueKey(num),
'$num',
style: TextStyle(color: Colors.black, fontSize: 64.0),
),
),
ElevatedButton(
onPressed: () {
setState(() {
num++;
});
},
child: Text('increment'),
),
],
),
),
);
}
}
2
Answers
I’ve used a
StreamBuilder
to make a timer and assumed three situations to make change in position of theText
In order to make these three conditions I’ve used two
Boolean
variables and toggled their values.I know this looks so complicated. But this works,
Sorry that I didn’t optimised the code.
Also note that the animation is only done for seconds.