Skip to content

Instantly share code, notes, and snippets.

@aaronscherbing
Created October 28, 2022 17:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aaronscherbing/010e8a96bae7cd84375c087cc06c11d3 to your computer and use it in GitHub Desktop.
Save aaronscherbing/010e8a96bae7cd84375c087cc06c11d3 to your computer and use it in GitHub Desktop.
Flutter: Animated Border Example
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