Skip to content

Commit 361701a

Browse files
committed
feat: Prioritize MediaQuery.of(context) in ScreenUtilPlusInit and refactor AdaptiveContainer breakpoint map creation.
1 parent 0e31232 commit 361701a

5 files changed

Lines changed: 117 additions & 27 deletions

File tree

lib/src/core/screen_util_plus.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ class ScreenUtilPlus {
101101
/// );
102102
/// },
103103
/// )
104-
/// ```
104+
/// ```
105105
static Future<void> ensureScreenSize([
106106
ui.FlutterView? window,
107107
Duration duration = const Duration(milliseconds: 10),

lib/src/core/screen_util_plus_init.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,10 @@ class _ScreenUtilPlusInitState extends State<ScreenUtilPlusInit>
151151
}
152152

153153
MediaQueryData? _getMediaQueryData() {
154+
final MediaQueryData? mediaQuery = MediaQuery.maybeOf(context);
155+
if (mediaQuery != null) {
156+
return mediaQuery;
157+
}
154158
final FlutterView? view = View.maybeOf(context);
155159
return view != null ? MediaQueryData.fromView(view) : null;
156160
}

lib/src/widgets/adaptive_container.dart

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -238,32 +238,27 @@ class SimpleAdaptiveContainer extends StatelessWidget {
238238
@override
239239
Widget build(BuildContext context) {
240240
final adaptive = AdaptiveContainer(
241-
width: {
242-
if (widthXs != null) Breakpoint.xs: widthXs!,
243-
if (widthSm != null) Breakpoint.sm: widthSm!,
244-
if (widthMd != null) Breakpoint.md: widthMd!,
245-
if (widthLg != null) Breakpoint.lg: widthLg!,
246-
if (widthXl != null) Breakpoint.xl: widthXl!,
247-
},
248-
height: {
249-
if (heightXs != null) Breakpoint.xs: heightXs!,
250-
if (heightSm != null) Breakpoint.sm: heightSm!,
251-
if (heightMd != null) Breakpoint.md: heightMd!,
252-
if (heightLg != null) Breakpoint.lg: heightLg!,
253-
if (heightXl != null) Breakpoint.xl: heightXl!,
254-
},
255-
padding: {
256-
if (paddingXs != null)
257-
Breakpoint.xs: EdgeInsets.all(paddingXs!.toDouble()),
258-
if (paddingSm != null)
259-
Breakpoint.sm: EdgeInsets.all(paddingSm!.toDouble()),
260-
if (paddingMd != null)
261-
Breakpoint.md: EdgeInsets.all(paddingMd!.toDouble()),
262-
if (paddingLg != null)
263-
Breakpoint.lg: EdgeInsets.all(paddingLg!.toDouble()),
264-
if (paddingXl != null)
265-
Breakpoint.xl: EdgeInsets.all(paddingXl!.toDouble()),
266-
},
241+
width: _createBreakpointMap(
242+
xs: widthXs,
243+
sm: widthSm,
244+
md: widthMd,
245+
lg: widthLg,
246+
xl: widthXl,
247+
),
248+
height: _createBreakpointMap(
249+
xs: heightXs,
250+
sm: heightSm,
251+
md: heightMd,
252+
lg: heightLg,
253+
xl: heightXl,
254+
),
255+
padding: _createBreakpointMap(
256+
xs: paddingXs != null ? EdgeInsets.all(paddingXs!.toDouble()) : null,
257+
sm: paddingSm != null ? EdgeInsets.all(paddingSm!.toDouble()) : null,
258+
md: paddingMd != null ? EdgeInsets.all(paddingMd!.toDouble()) : null,
259+
lg: paddingLg != null ? EdgeInsets.all(paddingLg!.toDouble()) : null,
260+
xl: paddingXl != null ? EdgeInsets.all(paddingXl!.toDouble()) : null,
261+
),
267262
color: color,
268263
decoration: decoration,
269264
breakpoints: breakpoints,
@@ -272,4 +267,23 @@ class SimpleAdaptiveContainer extends StatelessWidget {
272267

273268
return adaptive;
274269
}
270+
271+
Map<Breakpoint, T>? _createBreakpointMap<T>({
272+
T? xs,
273+
T? sm,
274+
T? md,
275+
T? lg,
276+
T? xl,
277+
}) {
278+
if (xs == null && sm == null && md == null && lg == null && xl == null) {
279+
return null;
280+
}
281+
return {
282+
if (xs != null) Breakpoint.xs: xs,
283+
if (sm != null) Breakpoint.sm: sm,
284+
if (md != null) Breakpoint.md: md,
285+
if (lg != null) Breakpoint.lg: lg,
286+
if (xl != null) Breakpoint.xl: xl,
287+
};
288+
}
275289
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:flutter_screenutil_plus/flutter_screenutil_plus.dart';
3+
import 'package:flutter_test/flutter_test.dart';
4+
5+
void main() {
6+
group('ScreenUtilPlusInit', () {
7+
testWidgets('uses parent MediaQuery data if available', (tester) async {
8+
const parentSize = Size(500, 1000);
9+
const parentData = MediaQueryData(
10+
size: parentSize,
11+
devicePixelRatio: 2.0,
12+
);
13+
14+
await tester.pumpWidget(
15+
MediaQuery(
16+
data: parentData,
17+
// ScreenUtilPlusInit inside MediaQuery
18+
child: ScreenUtilPlusInit(
19+
child: Builder(
20+
builder: (context) {
21+
// Verify ScreenUtil picked up the size
22+
expect(ScreenUtilPlus().screenWidth, parentSize.width);
23+
return const SizedBox();
24+
},
25+
),
26+
),
27+
),
28+
);
29+
});
30+
31+
testWidgets('falls back to View data if no parent MediaQuery', (
32+
tester,
33+
) async {
34+
tester.view.physicalSize = const Size(1200, 1600);
35+
tester.view.devicePixelRatio = 2.0;
36+
addTearDown(tester.view.reset);
37+
38+
// ScreenUtilPlusInit at root (no parent MediaQuery)
39+
await tester.pumpWidget(
40+
ScreenUtilPlusInit(
41+
child: Builder(
42+
builder: (context) {
43+
// Should use view size (physical / pixelRatio) = 600x800
44+
expect(ScreenUtilPlus().screenWidth, 600);
45+
expect(ScreenUtilPlus().screenHeight, 800);
46+
return const SizedBox();
47+
},
48+
),
49+
),
50+
);
51+
});
52+
});
53+
}

test/widgets/adaptive_container_test.dart

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,25 @@ void main() {
622622
expect(container.constraints, null);
623623
expect(container.padding, null);
624624
expect(container.margin, null);
625+
expect(container.margin, null);
626+
});
627+
628+
testWidgets('handles all nulls internally', (tester) async {
629+
// This verifies that the internal helper returns null when all inputs are null,
630+
// resulting in a standard Container with no AdaptiveContainer behavior overhead.
631+
await tester.pumpWidget(
632+
const MaterialApp(
633+
home: MediaQuery(
634+
data: MediaQueryData(size: Size(800, 600)),
635+
child: SimpleAdaptiveContainer(child: Text('Test')),
636+
),
637+
),
638+
);
639+
640+
final Container container = tester.widget(find.byType(Container));
641+
// No extra constraints or padding should be applied
642+
expect(container.constraints, null);
643+
expect(container.padding, null);
625644
});
626645
});
627646
}

0 commit comments

Comments
 (0)