Skip to content

border beam #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 46 additions & 44 deletions lib/fx_7_border_beam/border_beam.dart
Original file line number Diff line number Diff line change
@@ -1,19 +1,9 @@
import 'package:flutter/material.dart';
import 'dart:math' as math;
import 'dart:ui' as ui;

class BorderBeam extends StatefulWidget {
final Widget child;
final double duration;
final double borderWidth;
final Color colorFrom;
final Color colorTo;
final Color staticBorderColor;
final BorderRadius borderRadius;
final EdgeInsetsGeometry padding;

const BorderBeam({
Key? key,
super.key,
required this.child,
this.duration = 15,
this.borderWidth = 1.5,
Expand All @@ -22,16 +12,31 @@ class BorderBeam extends StatefulWidget {
this.staticBorderColor = const Color(0xFFCCCCCC),
this.borderRadius = const BorderRadius.all(Radius.circular(12)),
this.padding = EdgeInsets.zero,
}) : super(key: key);
});

final BorderRadius borderRadius;
final double borderWidth;
final Widget child;
final Color colorFrom;
final Color colorTo;
final double duration;
final EdgeInsetsGeometry padding;
final Color staticBorderColor;

@override
_BorderBeamState createState() => _BorderBeamState();
}

class _BorderBeamState extends State<BorderBeam>
with SingleTickerProviderStateMixin {
late AnimationController _controller;
late Animation<double> _animation;
late AnimationController _controller;

@override
void dispose() {
_controller.dispose();
super.dispose();
}

@override
void initState() {
Expand All @@ -44,44 +49,34 @@ class _BorderBeamState extends State<BorderBeam>
_controller.repeat();
}

@override
void dispose() {
_controller.dispose();
super.dispose();
}

@override
Widget build(BuildContext context) {
return AnimatedBuilder(
animation: _animation,
builder: (context, child) {
return CustomPaint(
painter: BorderBeamPainter(
progress: _animation.value,
borderWidth: widget.borderWidth,
colorFrom: widget.colorFrom,
colorTo: widget.colorTo,
staticBorderColor: widget.staticBorderColor,
borderRadius: widget.borderRadius,
),
child: Padding(
padding: widget.padding,
child: widget.child,
),
);
},
return Padding(
padding: EdgeInsets.all(widget.borderWidth / 2),
child: AnimatedBuilder(
animation: _animation,
builder: (context, child) {
return CustomPaint(
painter: BorderBeamPainter(
progress: _animation.value,
borderWidth: widget.borderWidth,
colorFrom: widget.colorFrom,
colorTo: widget.colorTo,
staticBorderColor: widget.staticBorderColor,
borderRadius: widget.borderRadius,
),
child: Padding(
padding: widget.padding,
child: widget.child,
),
);
},
),
);
}
}

class BorderBeamPainter extends CustomPainter {
final double progress;
final double borderWidth;
final Color colorFrom;
final Color colorTo;
final Color staticBorderColor;
final BorderRadius borderRadius;

BorderBeamPainter({
required this.progress,
required this.borderWidth,
Expand All @@ -91,6 +86,13 @@ class BorderBeamPainter extends CustomPainter {
required this.borderRadius,
});

final BorderRadius borderRadius;
final double borderWidth;
final Color colorFrom;
final Color colorTo;
final double progress;
final Color staticBorderColor;

@override
void paint(Canvas canvas, Size size) {
final rect = Rect.fromLTWH(0, 0, size.width, size.height);
Expand Down