Last active
August 28, 2023 13:04
-
-
Save rubywai/58ff528a66774fd246a16924d2793cf9 to your computer and use it in GitHub Desktop.
double border color.dart
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 'package:flutter/material.dart'; | |
class CustomDoubleBorder extends OutlinedBorder { | |
final double innerRadius; | |
final double outerRadius; | |
final BorderSide innerBorderSide; | |
final BorderSide outerBorderSide; | |
CustomDoubleBorder({ | |
this.innerRadius = 4, | |
this.outerRadius = 4, | |
this.innerBorderSide = const BorderSide(color: Colors.white, width: 3), | |
this.outerBorderSide = const BorderSide(color: Colors.red, width: 4), | |
}); | |
@override | |
CustomDoubleBorder copyWith({BorderSide? side}) { | |
return CustomDoubleBorder( | |
innerBorderSide: side ?? innerBorderSide, | |
outerBorderSide: side ?? outerBorderSide, | |
innerRadius: innerRadius, | |
outerRadius: outerRadius, | |
); | |
} | |
@override | |
EdgeInsetsGeometry get dimensions => const EdgeInsets.all(4); | |
@override | |
CustomDoubleBorder scale(double t) { | |
return CustomDoubleBorder( | |
innerRadius: innerRadius * t, | |
outerRadius: outerRadius * t, | |
innerBorderSide: innerBorderSide.scale(t), | |
outerBorderSide: outerBorderSide.scale(t), | |
); | |
} | |
@override | |
Path getInnerPath(Rect rect, {TextDirection? textDirection}) { | |
final innerPath = Path() | |
..addRRect(RRect.fromRectAndRadius( | |
rect.deflate(outerBorderSide.width + innerBorderSide.width), | |
Radius.circular(4))); | |
return innerPath; | |
} | |
@override | |
Path getOuterPath(Rect rect, {TextDirection? textDirection}) { | |
final outerPath = Path() | |
..addRRect(RRect.fromRectAndRadius( | |
rect.deflate(outerBorderSide.width), Radius.circular(4))); | |
return outerPath; | |
} | |
@override | |
void paint(Canvas canvas, Rect rect, {TextDirection? textDirection}) { | |
final inner = getInnerPath(rect, textDirection: textDirection); | |
final outer = getOuterPath(rect, textDirection: textDirection); | |
final innerPaint = innerBorderSide.toPaint(); | |
final outerPaint = outerBorderSide.toPaint(); | |
canvas.drawPath(outer, outerPaint); | |
canvas.drawPath(inner, innerPaint); | |
// Calculate the corner radius difference | |
} | |
} | |
class MyApp extends StatelessWidget { | |
const MyApp({Key? key}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
home: Scaffold( | |
body: Center( | |
child: ElevatedButton( | |
child: Text('Hello'), | |
onPressed: () {}, | |
style: TextButton.styleFrom(shape: CustomDoubleBorder( | |
innerBorderSide: BorderSide(color: Theme.of(context).colorScheme.onPrimary,width: 1), | |
outerBorderSide: BorderSide(color: Theme.of(context).colorScheme.primary,width: 2), | |
)), | |
), | |
), | |
), | |
); | |
} | |
} | |
void main() { | |
runApp(MyApp()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment