Skip to content
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

edited .separatedBy() to return a list by default. #4

Merged
merged 6 commits into from
May 25, 2023
Merged
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.1.1

- `.separatedBy()` now returns a list by default.

## 1.1.0

- Rename `horizontal` to `paddingHorizontal` and so on
Expand Down
11 changes: 7 additions & 4 deletions lib/src/miscellaneous_ext.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,18 @@ extension DevTools on Object {
/// Allows to insert a separator between the items of the iterable.
extension SeparatedIterable on Iterable<Widget> {
/// Allows to insert a [separator] between the items of the iterable.
Iterable<Widget> separatedBy(Widget separator) sync* {
List<Widget> separatedBy(Widget separator) {
final result = <Widget>[];
final iterator = this.iterator;
if (iterator.moveNext()) {
yield iterator.current;
result.add(iterator.current);
while (iterator.moveNext()) {
yield separator;
yield iterator.current;
result
..add(separator)
..add(iterator.current);
}
}
return result;
}
}

Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: awesome_flutter_extensions
description: >
A Flutter Extension to remove boilerplate when accessing
ancestor context properties
version: 1.1.0
version: 1.1.1
homepage: https://bestofcode.dev
repository: https://github.com/nank1ro/awesome_flutter_extensions

Expand Down
58 changes: 58 additions & 0 deletions test/misellaneous_ext_test.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,30 @@
import 'package:awesome_flutter_extensions/src/miscellaneous_ext.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
// Dart's equality operator (==) tests whether two references are to the same
// object, not if they have the same value. The Text widgets in the expected
// list and in the actual list are different instances, so they are not equal
// even if their properties are the same. The way to solve this issue is to
// compare the data of the widgets, not the widgets themselves. You can do
// this by creating a helper function that checks if two widgets have the same
// type and data. If they do, then they are equal.

bool areWidgetsEqual(Widget widget1, Widget widget2) {
if (widget1.runtimeType != widget2.runtimeType) {
return false;
}

if (widget1 is Text && widget2 is Text) {
return (widget1.data == widget2.data);
} else if (widget1 is SizedBox && widget2 is SizedBox) {
return (widget1.height == widget2.height);
}

return false;
}

group('misellaneous_ext_test -', () {
test('num to Duration', () {
final dur1 = 1000.milliseconds;
Expand Down Expand Up @@ -29,5 +52,40 @@ void main() {
final dur = 1.seconds;
expect(dur.future(), isA<Future<dynamic>>());
});

test(
'SeparatedIterable.separatedBy inserts separator',
() {
// Create a list of widgets
final widgets = <Widget>[
const Text('1'),
const Text('2'),
const Text('3')
];

//Create a separator widget
const Widget separator = SizedBox(height: 8);

// Separate the widgets by the separator
final separatedList = widgets.separatedBy(separator);

final expectedResult = <Widget>[
const Text('1'),
const SizedBox(height: 8),
const Text('2'),
const SizedBox(height: 8),
const Text('3'),
];

expect(separatedList.length, expectedResult.length);

for (var i = 0; i < separatedList.length; i++) {
expect(
areWidgetsEqual(separatedList[i], expectedResult[i]),
isTrue,
);
}
},
);
});
}