Skip to content

Commit d3df795

Browse files
committed
add tests & README
1 parent db1892b commit d3df795

File tree

2 files changed

+117
-9
lines changed

2 files changed

+117
-9
lines changed

README.md

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,36 @@
1-
# availability
1+
# Availability
22

3-
A new Flutter package project.
3+
A simple if-else widget renderer in an object-oriented way. You can find it similar to Visibility, but with WidgetBuilder usage. With this, the child widget is not put into context until the condition is satisfied.
4+
5+
#code-for-fun
46

57
## Getting Started
68

7-
This project is a starting point for a Dart
8-
[package](https://flutter.dev/developing-packages/),
9-
a library module containing code that can be shared easily across
10-
multiple Flutter or Dart projects.
119

12-
For help getting started with Flutter, view our
13-
[online documentation](https://flutter.dev/docs), which offers tutorials,
14-
samples, guidance on mobile development, and a full API reference.
10+
### Usage:
11+
```dart
12+
Availability(
13+
available: <condition>,
14+
childBuilder: (BuildContext context) => <widget>,
15+
)
16+
```
17+
18+
or
19+
20+
```dart
21+
Availability(
22+
available: <condition>,
23+
childBuilder: (BuildContext context) => <widget>,
24+
elseBuilder: (BuildContext context) => <another_widget>,
25+
)
26+
```
27+
28+
*** Similar dart code without Availability:
29+
30+
```dart
31+
if (<condition>) {
32+
return <widget>;
33+
} else {
34+
return <another_widget>;
35+
}
36+
```

test/availability_test.dart

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import 'package:availability/availability.dart';
2+
import 'package:flutter/material.dart';
3+
import 'package:flutter_test/flutter_test.dart';
4+
5+
void main() {
6+
testWidgets(
7+
'The child widget is rendered correctly',
8+
(WidgetTester tester) async {
9+
await tester.pumpWidget(
10+
_testableWidget(
11+
Availability(
12+
available: true,
13+
childBuilder: (_) => Text('qwerty'),
14+
),
15+
),
16+
);
17+
18+
await tester.pumpAndSettle();
19+
20+
expect(find.text('qwerty'), findsOneWidget);
21+
expect(find.byType(SizedBox), findsNothing);
22+
},
23+
);
24+
25+
testWidgets(
26+
'The child widget is rendered correctly with elseBuilder defined',
27+
(WidgetTester tester) async {
28+
await tester.pumpWidget(
29+
_testableWidget(
30+
Availability(
31+
available: true,
32+
childBuilder: (_) => Text('qwerty'),
33+
elseBuilder: (_) => Text('azerty'),
34+
),
35+
),
36+
);
37+
38+
await tester.pumpAndSettle();
39+
40+
expect(find.text('qwerty'), findsOneWidget);
41+
expect(find.text('azerty'), findsNothing);
42+
},
43+
);
44+
45+
testWidgets(
46+
'The child widget is not rendered and shrinked SizedBox is rendered',
47+
(WidgetTester tester) async {
48+
await tester.pumpWidget(
49+
_testableWidget(
50+
Availability(
51+
available: false,
52+
childBuilder: (_) => Text('qwerty'),
53+
),
54+
),
55+
);
56+
57+
await tester.pumpAndSettle();
58+
59+
expect(find.text('qwerty'), findsNothing);
60+
expect(find.byType(SizedBox), findsOneWidget);
61+
},
62+
);
63+
64+
testWidgets(
65+
'The else builder is triggered correctly',
66+
(WidgetTester tester) async {
67+
await tester.pumpWidget(
68+
_testableWidget(
69+
Availability(
70+
available: false,
71+
childBuilder: (_) => Text('qwerty'),
72+
elseBuilder: (_) => Text('azerty'),
73+
),
74+
),
75+
);
76+
77+
await tester.pumpAndSettle();
78+
79+
expect(find.text('qwerty'), findsNothing);
80+
expect(find.text('azerty'), findsOneWidget);
81+
expect(find.byType(SizedBox), findsNothing);
82+
},
83+
);
84+
}
85+
86+
Widget _testableWidget(Widget widget) => MaterialApp(home: widget);

0 commit comments

Comments
 (0)