-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve BorderRadius extension (#195)
- Rename file to border_radius_extension - Add outer method - Fix typo in comment - Fix negative radius in inner method when padding too large - Add unit tests
- Loading branch information
Showing
6 changed files
with
147 additions
and
27 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import 'package:flutter/widgets.dart'; | ||
|
||
extension BorderRadiusExtension on BorderRadius { | ||
/// Compute an inner border radius from a given padding | ||
BorderRadius inner(EdgeInsets padding) { | ||
final topLeftX = topLeft.x - padding.left / 2; | ||
final topLeftY = topLeft.y - padding.top / 2; | ||
final topRightX = topRight.x - padding.right / 2; | ||
final topRightY = topRight.y - padding.top / 2; | ||
final bottomLeftX = bottomLeft.x - padding.left / 2; | ||
final bottomLeftY = bottomLeft.y - padding.bottom / 2; | ||
final bottomRightX = bottomRight.x - padding.right / 2; | ||
final bottomRightY = bottomRight.y - padding.bottom / 2; | ||
|
||
return BorderRadius.only( | ||
topLeft: Radius.elliptical( | ||
topLeftX < 0 ? 0 : topLeftX, | ||
topLeftY < 0 ? 0 : topLeftY, | ||
), | ||
topRight: Radius.elliptical( | ||
topRightX < 0 ? 0 : topRightX, | ||
topRightY < 0 ? 0 : topRightY, | ||
), | ||
bottomRight: Radius.elliptical( | ||
bottomRightX < 0 ? 0 : bottomRightX, | ||
bottomRightY < 0 ? 0 : bottomRightY, | ||
), | ||
bottomLeft: Radius.elliptical( | ||
bottomLeftX < 0 ? 0 : bottomLeftX, | ||
bottomLeftY < 0 ? 0 : bottomLeftY, | ||
), | ||
); | ||
} | ||
|
||
/// Compute an outer border radius from a given margin | ||
BorderRadius outer(EdgeInsets margin) { | ||
return BorderRadius.only( | ||
topLeft: Radius.elliptical( | ||
topLeft.x + margin.left / 2, | ||
topLeft.y + margin.top / 2, | ||
), | ||
topRight: Radius.elliptical( | ||
topRight.x + margin.right / 2, | ||
topRight.y + margin.top / 2, | ||
), | ||
bottomRight: Radius.elliptical( | ||
bottomRight.x + margin.right / 2, | ||
bottomRight.y + margin.bottom / 2, | ||
), | ||
bottomLeft: Radius.elliptical( | ||
bottomLeft.x + margin.left / 2, | ||
bottomLeft.y + margin.bottom / 2, | ||
), | ||
); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ dev_dependencies: | |
sdk: flutter | ||
|
||
flutter_lints: ^1.0.0 | ||
test: ^1.21.4 | ||
|
||
flutter: | ||
assets: | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:test/test.dart'; | ||
import 'package:yaru_widgets/src/extensions/border_radius_extension.dart'; | ||
|
||
void main() { | ||
group('- BorderRadiusExtension.inner() test -', () { | ||
test('With no padding', () { | ||
testBorderRadiusInner( | ||
BorderRadius.circular(10), | ||
const EdgeInsets.all(0), | ||
BorderRadius.circular(10), | ||
); | ||
}); | ||
|
||
test('With same side padding', () { | ||
testBorderRadiusInner( | ||
BorderRadius.circular(10), | ||
const EdgeInsets.all(4), | ||
BorderRadius.circular(8), | ||
); | ||
}); | ||
|
||
test('With large padding', () { | ||
testBorderRadiusInner( | ||
BorderRadius.circular(10), | ||
const EdgeInsets.all(40), | ||
BorderRadius.zero, | ||
); | ||
}); | ||
|
||
test('With different side padding', () { | ||
testBorderRadiusInner( | ||
BorderRadius.circular(10), | ||
const EdgeInsets.fromLTRB(2, 4, 6, 8), | ||
const BorderRadius.only( | ||
topLeft: Radius.elliptical(9, 8), | ||
topRight: Radius.elliptical(7, 8), | ||
bottomRight: Radius.elliptical(7, 6), | ||
bottomLeft: Radius.elliptical(9, 6), | ||
), | ||
); | ||
}); | ||
|
||
test('With different corner radius', () { | ||
testBorderRadiusInner( | ||
const BorderRadius.only( | ||
topLeft: Radius.elliptical(2, 4), | ||
topRight: Radius.elliptical(6, 8), | ||
bottomLeft: Radius.elliptical(10, 12), | ||
bottomRight: Radius.elliptical(14, 16), | ||
), | ||
const EdgeInsets.all(2), | ||
const BorderRadius.only( | ||
topLeft: Radius.elliptical(1, 3), | ||
topRight: Radius.elliptical(5, 7), | ||
bottomLeft: Radius.elliptical(9, 11), | ||
bottomRight: Radius.elliptical(13, 15), | ||
), | ||
); | ||
}); | ||
|
||
test('With different corner radius and side padding', () { | ||
testBorderRadiusInner( | ||
const BorderRadius.only( | ||
topLeft: Radius.elliptical(10, 12), | ||
topRight: Radius.elliptical(14, 16), | ||
bottomLeft: Radius.elliptical(18, 20), | ||
bottomRight: Radius.elliptical(22, 24), | ||
), | ||
const EdgeInsets.fromLTRB(2, 4, 6, 8), | ||
const BorderRadius.only( | ||
topLeft: Radius.elliptical(9, 10), | ||
topRight: Radius.elliptical(11, 14), | ||
bottomLeft: Radius.elliptical(17, 16), | ||
bottomRight: Radius.elliptical(19, 20), | ||
), | ||
); | ||
}); | ||
}); | ||
} | ||
|
||
void testBorderRadiusInner( | ||
BorderRadius initial, | ||
EdgeInsets padding, | ||
BorderRadius expected, | ||
) { | ||
expect(initial.inner(padding), expected); | ||
} |