Skip to content

Commit

Permalink
Improve BorderRadius extension (#195)
Browse files Browse the repository at this point in the history
- 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
Jupi007 committed Aug 31, 2022
1 parent 15e20f1 commit 4c9df54
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 27 deletions.
56 changes: 56 additions & 0 deletions lib/src/extensions/border_radius_extension.dart
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,
),
);
}
}
25 changes: 0 additions & 25 deletions lib/src/extensions/inner_border_radius.dart

This file was deleted.

2 changes: 1 addition & 1 deletion lib/src/utilities/yaru_selectable_container.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'package:flutter/material.dart';
import 'package:yaru_widgets/src/constants.dart';
import 'package:yaru_widgets/src/extensions/inner_border_radius.dart';
import 'package:yaru_widgets/src/extensions/border_radius_extension.dart';

class YaruSelectableContainer extends StatelessWidget {
/// Creates a Image Tile from the image path given in the path property.
Expand Down
2 changes: 1 addition & 1 deletion lib/yaru_widgets.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ export 'src/pages/rows/yaru_switch_row.dart';
export 'src/pages/yaru_tabbed_page.dart';
export 'src/pages/rows/yaru_toggle_buttons_row.dart';
export 'src/pages/layouts/yaru_wide_layout.dart';
export 'src/extensions/inner_border_radius.dart';
export 'src/extensions/border_radius_extension.dart';
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ dev_dependencies:
sdk: flutter

flutter_lints: ^1.0.0
test: ^1.21.4

flutter:
assets:
Expand Down
88 changes: 88 additions & 0 deletions test/extensions/inner_border_radius_test.dart
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);
}

0 comments on commit 4c9df54

Please sign in to comment.