Last active
July 30, 2024 10:58
-
-
Save IvanGael/d6d7ef0d15beb28bf0863fc63481689c to your computer and use it in GitHub Desktop.
Animated gradient border implementation on card in flutter
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
// ignore_for_file: library_private_types_in_public_api | |
import 'dart:math'; | |
import 'package:flutter/material.dart'; | |
class GradientBorderCard extends StatefulWidget { | |
const GradientBorderCard({super.key}); | |
@override | |
_GradientBorderCardState createState() => _GradientBorderCardState(); | |
} | |
class _GradientBorderCardState extends State<GradientBorderCard> with SingleTickerProviderStateMixin { | |
late AnimationController _controller; | |
@override | |
void initState() { | |
super.initState(); | |
_controller = AnimationController( | |
duration: const Duration(seconds: 2), | |
vsync: this, | |
)..repeat(); | |
} | |
@override | |
void dispose() { | |
_controller.dispose(); | |
super.dispose(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Center( | |
child: AnimatedBuilder( | |
animation: _controller, | |
builder: (context, child) { | |
return Container( | |
width: 300, | |
height: 200, | |
decoration: BoxDecoration( | |
gradient: LinearGradient( | |
begin: Alignment.topLeft, | |
end: Alignment.bottomRight, | |
colors: [ | |
Colors.greenAccent, | |
Colors.deepPurpleAccent, | |
Theme.of(context).cardColor, | |
Theme.of(context).cardColor, | |
], | |
stops: const [ | |
0.0, | |
0.33, | |
0.66, | |
1.0, | |
], | |
transform: GradientRotation(_controller.value * 2 * pi), | |
), | |
borderRadius: BorderRadius.circular(15), | |
), | |
child: Padding( | |
padding: const EdgeInsets.all(1), | |
child: Card( | |
elevation: 4, | |
color: Colors.black, | |
shape: RoundedRectangleBorder( | |
borderRadius: BorderRadius.circular(12), | |
), | |
child: const Center( | |
child: Text( | |
'⚡', | |
style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold), | |
), | |
), | |
), | |
), | |
); | |
}, | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment