Skip to content

Commit 8e32070

Browse files
authored
fix(code style): use formatter and examples (#2)
To please the flutter pub score, use flutter format instead of flutter stylizer and create a small example. Signed-off-by: Christoph Bühler <christoph@smartive.ch>
1 parent ed9aa59 commit 8e32070

File tree

7 files changed

+161
-76
lines changed

7 files changed

+161
-76
lines changed

.flutter-stylizer.yml

Lines changed: 0 additions & 13 deletions
This file was deleted.

.github/workflows/test.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ jobs:
1818
with:
1919
channel: 'stable'
2020
- run: flutter pub get
21+
- run: dart format --fix --set-exit-if-changed .
2122
- run: flutter analyze
2223
- run: flutter test --coverage
2324
- name: Setup LCOV

example/example.dart

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:generic_dropdown_widget/generic_dropdown_widget.dart';
3+
4+
class Dropdown extends StatelessWidget {
5+
const Dropdown({super.key});
6+
7+
@override
8+
Widget build(BuildContext context) => GenericDropdown(
9+
openOnRender: false,
10+
closeOnOutsideTap: true,
11+
toggleBuilder: (context, isOpen) => Container(
12+
height: 50,
13+
width: 50,
14+
color: isOpen ? Colors.green : Colors.red,
15+
),
16+
contentBuilder: (context, repaint, close) => Container(
17+
height: 100,
18+
width: 100,
19+
color: Colors.blue,
20+
),
21+
);
22+
}

lib/src/generic_dropdown.dart

Lines changed: 68 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ enum DropdownDirection {
4444
downRight,
4545
}
4646

47-
typedef ContentBuilder = Widget Function(BuildContext context, VoidCallback repaint, VoidCallback close);
47+
typedef ContentBuilder = Widget Function(
48+
BuildContext context, VoidCallback repaint, VoidCallback close);
4849
typedef ToggleBuilder = Widget Function(BuildContext context, bool isOpen);
4950

5051
/// A generic dropdown widget that enables arbitrary content
@@ -130,7 +131,10 @@ class _GenericDropdownState extends State<GenericDropdown> {
130131
}
131132

132133
RenderBox? _ancestor(BuildContext context) =>
133-
GenericDropdownConfigProvider.of(context)?.rootScreenKey?.currentContext?.findRenderObject() as RenderBox?;
134+
GenericDropdownConfigProvider.of(context)
135+
?.rootScreenKey
136+
?.currentContext
137+
?.findRenderObject() as RenderBox?;
134138

135139
void _close() {
136140
_overlayEntry?.remove();
@@ -142,7 +146,8 @@ class _GenericDropdownState extends State<GenericDropdown> {
142146
final renderBox = context.findRenderObject() as RenderBox;
143147

144148
final size = renderBox.size;
145-
final togglePosition = renderBox.localToGlobal(Offset.zero, ancestor: _ancestor(context));
149+
final togglePosition =
150+
renderBox.localToGlobal(Offset.zero, ancestor: _ancestor(context));
146151

147152
final screenSize = _screenSize(context);
148153

@@ -166,61 +171,93 @@ class _GenericDropdownState extends State<GenericDropdown> {
166171
double? top, left, bottom, right;
167172

168173
// Anchor TOP LEFT
169-
if (widget.anchor == DropdownAnchor.topLeft && widget.direction == DropdownDirection.upLeft) {
174+
if (widget.anchor == DropdownAnchor.topLeft &&
175+
widget.direction == DropdownDirection.upLeft) {
170176
bottom = screenSize.height - togglePosition.dy + widget.offset.dy;
171177
right = screenSize.width - togglePosition.dx + widget.offset.dx;
172-
} else if (widget.anchor == DropdownAnchor.topLeft && widget.direction == DropdownDirection.upRight) {
178+
} else if (widget.anchor == DropdownAnchor.topLeft &&
179+
widget.direction == DropdownDirection.upRight) {
173180
bottom = screenSize.height - togglePosition.dy + widget.offset.dy;
174181
left = togglePosition.dx + widget.offset.dx;
175-
} else if (widget.anchor == DropdownAnchor.topLeft && widget.direction == DropdownDirection.downLeft) {
182+
} else if (widget.anchor == DropdownAnchor.topLeft &&
183+
widget.direction == DropdownDirection.downLeft) {
176184
top = togglePosition.dy + widget.offset.dy;
177185
right = screenSize.width - togglePosition.dx + widget.offset.dx;
178-
} else if (widget.anchor == DropdownAnchor.topLeft && widget.direction == DropdownDirection.downRight) {
186+
} else if (widget.anchor == DropdownAnchor.topLeft &&
187+
widget.direction == DropdownDirection.downRight) {
179188
top = togglePosition.dy + widget.offset.dy;
180189
left = togglePosition.dx + widget.offset.dx;
181190
}
182191

183192
// Anchor TOP RIGHT
184-
if (widget.anchor == DropdownAnchor.topRight && widget.direction == DropdownDirection.upLeft) {
193+
if (widget.anchor == DropdownAnchor.topRight &&
194+
widget.direction == DropdownDirection.upLeft) {
185195
bottom = screenSize.height - togglePosition.dy + widget.offset.dy;
186-
right = screenSize.width - togglePosition.dx + widget.offset.dx - size.width;
187-
} else if (widget.anchor == DropdownAnchor.topRight && widget.direction == DropdownDirection.upRight) {
196+
right =
197+
screenSize.width - togglePosition.dx + widget.offset.dx - size.width;
198+
} else if (widget.anchor == DropdownAnchor.topRight &&
199+
widget.direction == DropdownDirection.upRight) {
188200
bottom = screenSize.height - togglePosition.dy + widget.offset.dy;
189201
left = togglePosition.dx + widget.offset.dx + size.width;
190-
} else if (widget.anchor == DropdownAnchor.topRight && widget.direction == DropdownDirection.downLeft) {
202+
} else if (widget.anchor == DropdownAnchor.topRight &&
203+
widget.direction == DropdownDirection.downLeft) {
191204
top = togglePosition.dy + widget.offset.dy;
192-
right = screenSize.width - togglePosition.dx + widget.offset.dx - size.width;
193-
} else if (widget.anchor == DropdownAnchor.topRight && widget.direction == DropdownDirection.downRight) {
205+
right =
206+
screenSize.width - togglePosition.dx + widget.offset.dx - size.width;
207+
} else if (widget.anchor == DropdownAnchor.topRight &&
208+
widget.direction == DropdownDirection.downRight) {
194209
top = togglePosition.dy + widget.offset.dy;
195210
left = togglePosition.dx + widget.offset.dx + size.width;
196211
}
197212

198213
// Anchor BOTTOM LEFT
199-
if (widget.anchor == DropdownAnchor.bottomLeft && widget.direction == DropdownDirection.upLeft) {
200-
bottom = screenSize.height - togglePosition.dy + widget.offset.dy - size.height;
214+
if (widget.anchor == DropdownAnchor.bottomLeft &&
215+
widget.direction == DropdownDirection.upLeft) {
216+
bottom = screenSize.height -
217+
togglePosition.dy +
218+
widget.offset.dy -
219+
size.height;
201220
right = screenSize.width - togglePosition.dx + widget.offset.dx;
202-
} else if (widget.anchor == DropdownAnchor.bottomLeft && widget.direction == DropdownDirection.upRight) {
203-
bottom = screenSize.height - togglePosition.dy + widget.offset.dy - size.height;
221+
} else if (widget.anchor == DropdownAnchor.bottomLeft &&
222+
widget.direction == DropdownDirection.upRight) {
223+
bottom = screenSize.height -
224+
togglePosition.dy +
225+
widget.offset.dy -
226+
size.height;
204227
left = togglePosition.dx + widget.offset.dx;
205-
} else if (widget.anchor == DropdownAnchor.bottomLeft && widget.direction == DropdownDirection.downLeft) {
228+
} else if (widget.anchor == DropdownAnchor.bottomLeft &&
229+
widget.direction == DropdownDirection.downLeft) {
206230
top = togglePosition.dy + widget.offset.dy + size.height;
207231
right = screenSize.width - togglePosition.dx + widget.offset.dx;
208-
} else if (widget.anchor == DropdownAnchor.bottomLeft && widget.direction == DropdownDirection.downRight) {
232+
} else if (widget.anchor == DropdownAnchor.bottomLeft &&
233+
widget.direction == DropdownDirection.downRight) {
209234
top = togglePosition.dy + widget.offset.dy + size.height;
210235
left = togglePosition.dx + widget.offset.dx;
211236
}
212237

213238
// Anchor BOTTOM RIGHT
214-
if (widget.anchor == DropdownAnchor.bottomRight && widget.direction == DropdownDirection.upLeft) {
215-
bottom = screenSize.height - togglePosition.dy + widget.offset.dy - size.height;
216-
right = screenSize.width - togglePosition.dx + widget.offset.dx - size.width;
217-
} else if (widget.anchor == DropdownAnchor.bottomRight && widget.direction == DropdownDirection.upRight) {
218-
bottom = screenSize.height - togglePosition.dy + widget.offset.dy - size.height;
239+
if (widget.anchor == DropdownAnchor.bottomRight &&
240+
widget.direction == DropdownDirection.upLeft) {
241+
bottom = screenSize.height -
242+
togglePosition.dy +
243+
widget.offset.dy -
244+
size.height;
245+
right =
246+
screenSize.width - togglePosition.dx + widget.offset.dx - size.width;
247+
} else if (widget.anchor == DropdownAnchor.bottomRight &&
248+
widget.direction == DropdownDirection.upRight) {
249+
bottom = screenSize.height -
250+
togglePosition.dy +
251+
widget.offset.dy -
252+
size.height;
219253
left = togglePosition.dx + widget.offset.dx + size.width;
220-
} else if (widget.anchor == DropdownAnchor.bottomRight && widget.direction == DropdownDirection.downLeft) {
254+
} else if (widget.anchor == DropdownAnchor.bottomRight &&
255+
widget.direction == DropdownDirection.downLeft) {
221256
top = togglePosition.dy + widget.offset.dy + size.height;
222-
right = screenSize.width - togglePosition.dx + widget.offset.dx - size.width;
223-
} else if (widget.anchor == DropdownAnchor.bottomRight && widget.direction == DropdownDirection.downRight) {
257+
right =
258+
screenSize.width - togglePosition.dx + widget.offset.dx - size.width;
259+
} else if (widget.anchor == DropdownAnchor.bottomRight &&
260+
widget.direction == DropdownDirection.downRight) {
224261
top = togglePosition.dy + widget.offset.dy + size.height;
225262
left = togglePosition.dx + widget.offset.dx + size.width;
226263
}
@@ -249,8 +286,8 @@ class _GenericDropdownState extends State<GenericDropdown> {
249286
// content.
250287
},
251288
child: StatefulBuilder(
252-
builder: (context, setState) =>
253-
widget.contentBuilder.call(context, () => setState(() {}), _close)),
289+
builder: (context, setState) => widget.contentBuilder
290+
.call(context, () => setState(() {}), _close)),
254291
),
255292
),
256293
],
@@ -265,7 +302,8 @@ class _GenericDropdownState extends State<GenericDropdown> {
265302
setState(() => _isOpen = true);
266303
}
267304

268-
Size _screenSize(BuildContext context) => _ancestor(context)?.size ?? MediaQuery.of(context).size;
305+
Size _screenSize(BuildContext context) =>
306+
_ancestor(context)?.size ?? MediaQuery.of(context).size;
269307

270308
@override
271309
Widget build(BuildContext context) => Row(

lib/src/generic_dropdown_config_provider.dart

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ class GenericDropdownConfigProvider extends InheritedWidget {
1818
/// of the content overlay to the toggle.
1919
final GlobalKey? rootScreenKey;
2020

21-
const GenericDropdownConfigProvider({super.key, this.rootScreenKey, required super.child});
21+
const GenericDropdownConfigProvider(
22+
{super.key, this.rootScreenKey, required super.child});
2223

2324
@override
24-
bool updateShouldNotify(GenericDropdownConfigProvider oldWidget) => rootScreenKey != oldWidget.rootScreenKey;
25+
bool updateShouldNotify(GenericDropdownConfigProvider oldWidget) =>
26+
rootScreenKey != oldWidget.rootScreenKey;
2527

26-
static GenericDropdownConfigProvider? of(BuildContext context) =>
27-
context.dependOnInheritedWidgetOfExactType<GenericDropdownConfigProvider>();
28+
static GenericDropdownConfigProvider? of(BuildContext context) => context
29+
.dependOnInheritedWidgetOfExactType<GenericDropdownConfigProvider>();
2830
}

storybook/lib/main.dart

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ void main() {
2020
home: Scaffold(
2121
body: SafeArea(
2222
child: GenericDropdownConfigProvider(
23-
rootScreenKey: rootKey, child: Center(key: UniqueKey(), child: child))))),
23+
rootScreenKey: rootKey,
24+
child: Center(key: UniqueKey(), child: child))))),
2425
// initialStory: 'Generic Dropdown',
2526
stories: [
2627
_dropdown(),
@@ -73,26 +74,37 @@ Story _dropdown() => Story(
7374
toggleBuilder: (context, isOpen) => Container(
7475
height: 120,
7576
width: 120,
76-
color: isOpen ? Colors.amber.withOpacity(.25) : Colors.blue.withOpacity(.25),
77+
color: isOpen
78+
? Colors.amber.withOpacity(.25)
79+
: Colors.blue.withOpacity(.25),
7780
child: Text('Toggle (${isOpen ? 'Open' : 'Closed'})'),
7881
),
7982
offset: Offset(
80-
context.knobs.sliderInt(label: 'X Offset', initial: 0, min: -100, max: 100).toDouble(),
81-
context.knobs.sliderInt(label: 'Y Offset', initial: 0, min: -100, max: 100).toDouble(),
83+
context.knobs
84+
.sliderInt(label: 'X Offset', initial: 0, min: -100, max: 100)
85+
.toDouble(),
86+
context.knobs
87+
.sliderInt(label: 'Y Offset', initial: 0, min: -100, max: 100)
88+
.toDouble(),
8289
),
8390
anchor: context.knobs.options(
8491
label: 'Anchor',
8592
description: 'The anchor for the content dropdown.',
8693
initial: DropdownAnchor.bottomLeft,
87-
options: DropdownAnchor.values.map((v) => Option(label: v.name, value: v)).toList()),
94+
options: DropdownAnchor.values
95+
.map((v) => Option(label: v.name, value: v))
96+
.toList()),
8897
direction: context.knobs.options(
8998
label: 'Direction',
9099
description: 'The direction where the dropdown should open to.',
91100
initial: DropdownDirection.downRight,
92-
options: DropdownDirection.values.map((v) => Option(label: v.name, value: v)).toList()),
101+
options: DropdownDirection.values
102+
.map((v) => Option(label: v.name, value: v))
103+
.toList()),
93104
closeOnOutsideTap: context.knobs.boolean(
94105
label: 'Close On Outside Tap',
95-
description: 'Whether the content is closed on an outside tap or only if the content calls close().',
106+
description:
107+
'Whether the content is closed on an outside tap or only if the content calls close().',
96108
initial: true),
97109
));
98110
});
@@ -142,26 +154,37 @@ Story _openDropdown() => Story(
142154
toggleBuilder: (context, isOpen) => Container(
143155
height: 120,
144156
width: 120,
145-
color: isOpen ? Colors.amber.withOpacity(.25) : Colors.blue.withOpacity(.25),
157+
color: isOpen
158+
? Colors.amber.withOpacity(.25)
159+
: Colors.blue.withOpacity(.25),
146160
child: Text('Toggle (${isOpen ? 'Open' : 'Closed'})'),
147161
),
148162
offset: Offset(
149-
context.knobs.sliderInt(label: 'X Offset', initial: 0, min: -100, max: 100).toDouble(),
150-
context.knobs.sliderInt(label: 'Y Offset', initial: 0, min: -100, max: 100).toDouble(),
163+
context.knobs
164+
.sliderInt(label: 'X Offset', initial: 0, min: -100, max: 100)
165+
.toDouble(),
166+
context.knobs
167+
.sliderInt(label: 'Y Offset', initial: 0, min: -100, max: 100)
168+
.toDouble(),
151169
),
152170
anchor: context.knobs.options(
153171
label: 'Anchor',
154172
description: 'The anchor for the content dropdown.',
155173
initial: DropdownAnchor.bottomLeft,
156-
options: DropdownAnchor.values.map((v) => Option(label: v.name, value: v)).toList()),
174+
options: DropdownAnchor.values
175+
.map((v) => Option(label: v.name, value: v))
176+
.toList()),
157177
direction: context.knobs.options(
158178
label: 'Direction',
159179
description: 'The direction where the dropdown should open to.',
160180
initial: DropdownDirection.downRight,
161-
options: DropdownDirection.values.map((v) => Option(label: v.name, value: v)).toList()),
181+
options: DropdownDirection.values
182+
.map((v) => Option(label: v.name, value: v))
183+
.toList()),
162184
closeOnOutsideTap: context.knobs.boolean(
163185
label: 'Close On Outside Tap',
164-
description: 'Whether the content is closed on an outside tap or only if the content calls close().',
186+
description:
187+
'Whether the content is closed on an outside tap or only if the content calls close().',
165188
initial: true),
166189
));
167190
});

0 commit comments

Comments
 (0)