Skip to content

Instantly share code, notes, and snippets.

@artem-zaitsev
Last active February 13, 2024 15:42
Show Gist options
  • Save artem-zaitsev/10de38f2cfd41ee61657e98e3a98d00f to your computer and use it in GitHub Desktop.
Save artem-zaitsev/10de38f2cfd41ee61657e98e3a98d00f to your computer and use it in GitHub Desktop.
Anim Example with Tween
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.light().copyWith(
scaffoldBackgroundColor: Colors.white,
),
debugShowCheckedModeBanner: false,
home: const RateScreen(),
);
}
}
class RateScreen extends StatefulWidget {
const RateScreen({super.key});
@override
State<RateScreen> createState() => _RateScreenState();
}
class _RateScreenState extends State<RateScreen> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
children: [
Positioned(
top: 250,
left: 0,
right: 0,
bottom: 0,
child: Container(
decoration: const BoxDecoration(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(16),
topRight: Radius.circular(16),
),
color: Colors.black87,
),
padding: const EdgeInsets.symmetric(horizontal: 16),
child: Column(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.end,
children: [
SizedBox(
height: 100,
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(16),
color: Colors.white54,
),
),
),
const SizedBox(height: 10),
SizedBox(
height: 25,
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(16),
color: Colors.white54,
),
),
),
const SizedBox(height: 50),
const _AnimatedButton(),
const SizedBox(height: 24),
],
),
),
)
],
),
);
}
}
class _AnimatedButton extends StatefulWidget {
const _AnimatedButton();
@override
State<_AnimatedButton> createState() => _AnimatedButtonState();
}
class _AnimatedButtonState extends State<_AnimatedButton>
with SingleTickerProviderStateMixin<_AnimatedButton> {
late final _controller = AnimationController(
vsync: this,
duration: Durations.medium4,
value: 0,
)..repeat(reverse: true);
late final _offset = Tween<Offset>(
begin: const Offset(-5, 0),
end: const Offset(5, 0),
).animate(
_controller,
);
@override
void initState() {
super.initState();
_controller.addListener(_update);
}
@override
Widget build(BuildContext context) {
return Transform.translate(
offset: _offset.value,
child: ElevatedButton(
style: ElevatedButton.styleFrom(
backgroundColor: Colors.amber,
),
onPressed: () {},
child: const Text('Оценить заказ'),
),
);
}
@override
void dispose() {
_controller.removeListener(_update);
_controller.dispose();
super.dispose();
}
void _update() {
setState(() {});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment