Reputation: 19444
I need to animate widget and I used AnimatedPositioned. but not animated. just change position without animate
Full Code
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,
)
],
),
),
],
),
)
),
Upvotes: 0
Views: 6377
Reputation: 27147
You can animate using only left or right property of animated container.
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,
),
],
),
),
Upvotes: 1
Reputation: 165
You just need to set a distance not equal to null from the right and left sides all the time so the container doesn't Jump, I modified the code a little (try it):
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,
)
],
),
),
],
),
)),
],
),
Upvotes: 4