Created
October 28, 2022 17:08
-
-
Save aaronscherbing/010e8a96bae7cd84375c087cc06c11d3 to your computer and use it in GitHub Desktop.
Flutter: Animated Border Example
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
import 'dart:async'; | |
import 'package:flutter/material.dart'; | |
void main() => runApp(Test()); | |
class Test extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
home: Scaffold( | |
appBar: AppBar(title: const Text('Test Animation')), | |
body: Center(child: Stack(children: [ | |
Container(width: 50, height: 50, color: Colors.white), | |
const Positioned( | |
top: 0, right: 0, bottom: 0, left: 0, | |
child: IgnorePointer( child: AnimatedBorderComponent()), | |
) | |
])) | |
) | |
); | |
} | |
} | |
class AnimatedBorderComponent extends StatefulWidget { | |
const AnimatedBorderComponent({ super.key }); | |
@override | |
State<AnimatedBorderComponent> createState() { return _AnimatedBorderComponentState(); } | |
} | |
class _AnimatedBorderComponentState extends State<AnimatedBorderComponent> { | |
int index = 0; | |
List<Color> colors = [Colors.red, Colors.orange, Colors.yellow, Colors.green, Colors.blue, Colors.purple]; | |
Duration duration = const Duration(milliseconds: 250); | |
Timer? _timer; | |
@override | |
void initState() { | |
super.initState(); | |
_timer = Timer.periodic(duration, (timer) { | |
index = (index + 1) % colors.length; | |
if (mounted) { setState(() {}); } | |
}); | |
} | |
@override | |
void dispose() { | |
super.dispose(); | |
_timer?.cancel(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return AnimatedContainer( | |
duration: duration, | |
curve: Curves.easeInOut, | |
decoration: BoxDecoration( | |
border: Border.all(color: colors[index], style: BorderStyle.solid, width: 4), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment