Skip to content

Instantly share code, notes, and snippets.

@callmephil
Created February 26, 2025 18:13
Show Gist options
  • Save callmephil/83df032d4273ca21a44298fb94d4fd66 to your computer and use it in GitHub Desktop.
Save callmephil/83df032d4273ca21a44298fb94d4fd66 to your computer and use it in GitHub Desktop.
AnimatedBuilder + ShaderMask and LinearGradient
import 'package:flutter/material.dart';
import 'dart:ui';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
backgroundColor: Colors.black,
body: const Center(
child: AnimatedGradientBorder(),
),
),
);
}
}
class AnimatedGradientBorder extends StatefulWidget {
const AnimatedGradientBorder({super.key});
@override
_AnimatedGradientBorderState createState() => _AnimatedGradientBorderState();
}
class _AnimatedGradientBorderState extends State<AnimatedGradientBorder>
with SingleTickerProviderStateMixin {
late AnimationController _controller;
late Animation<double> _animation;
@override
void initState() {
super.initState();
_controller = AnimationController(
vsync: this,
duration: const Duration(seconds: 3),
)..repeat(reverse: true);
_animation = Tween<double>(begin: 0, end: 1).animate(_controller);
}
@override
Widget build(BuildContext context) {
return AnimatedBuilder(
animation: _animation,
builder: (context, child) {
return ShaderMask(
shaderCallback: (bounds) {
return LinearGradient(
colors: [
Color.lerp(Colors.purple, Colors.blue, _animation.value)!,
Color.lerp(Colors.blue, Colors.green, _animation.value)!,
Color.lerp(Colors.green, Colors.purple, _animation.value)!,
],
stops: [0.0, 0.5, 1.0],
begin: Alignment.topLeft,
end: Alignment.bottomRight,
).createShader(bounds);
},
child: Container(
width: 200,
height: 200,
decoration: BoxDecoration(
color: Colors.black,
border: Border.all(
width: 5,
color: Colors.white, // Fallback border color
),
borderRadius: BorderRadius.circular(20),
),
),
);
},
);
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment