Skip to content

Commit b02ec0a

Browse files
committed
Initialization Background Update
- Use background on initialization if set.
1 parent e646302 commit b02ec0a

File tree

2 files changed

+54
-49
lines changed

2 files changed

+54
-49
lines changed

lib/responsive_wrapper.dart

Lines changed: 42 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,14 @@ class ResponsiveWrapper extends StatefulWidget {
118118
/// is not resized.
119119
/// Can be used to set a background image, pattern,
120120
/// or solid fill.
121+
/// Overrides [backgroundColor] if a widget is set.
121122
final Widget? background;
122123

123124
/// First frame initialization default background color.
124125
/// Because layout initialization is delayed by 1 frame,
125126
/// a solid background color is displayed instead.
126-
/// By default uses `background` widget if it is set,
127-
/// otherwise uses white color.
127+
/// Is overridden by [background] if set.
128+
/// Defaults to a white background.
128129
final Color? backgroundColor;
129130
final MediaQueryData? mediaQueryData;
130131
final bool shrinkWrap;
@@ -659,39 +660,41 @@ class _ResponsiveWrapperState extends State<ResponsiveWrapper>
659660
// Platform initialization requires context.
660661
setPlatform();
661662

662-
return (screenWidth ==
663-
0) // Initialization check. Window measurements not available until postFrameCallback.
664-
? buildBackgroundColorWidget(widget
665-
.backgroundColor) // First frame with empty background.
666-
: InheritedResponsiveWrapper(
667-
data: ResponsiveWrapperData.fromResponsiveWrapper(this),
668-
child: Stack(
669-
alignment: widget.alignment,
670-
children: [
671-
widget.background ?? SizedBox.shrink(),
672-
MediaQuery(
673-
data: calculateMediaQueryData(),
674-
child: SizedBox(
675-
width: screenWidth,
676-
child: FittedBox(
677-
fit: BoxFit.fitWidth,
678-
alignment: Alignment.topCenter,
679-
child: Container(
680-
width: scaledWidth,
681-
height: (widget.shrinkWrap == true &&
682-
widget.mediaQueryData == null)
683-
? null
684-
: scaledHeight,
685-
// Shrink wrap height if no MediaQueryData is passed.
686-
alignment: Alignment.center,
687-
child: widget.child,
688-
),
689-
),
690-
),
663+
// Initialization check. Window measurements not available until postFrameCallback.
664+
// Return first frame with empty background.
665+
if (screenWidth == 0)
666+
return buildBackground(
667+
background: widget.background, color: widget.backgroundColor);
668+
669+
return InheritedResponsiveWrapper(
670+
data: ResponsiveWrapperData.fromResponsiveWrapper(this),
671+
child: Stack(
672+
alignment: widget.alignment,
673+
children: [
674+
widget.background ?? SizedBox.shrink(),
675+
MediaQuery(
676+
data: calculateMediaQueryData(),
677+
child: SizedBox(
678+
width: screenWidth,
679+
child: FittedBox(
680+
fit: BoxFit.fitWidth,
681+
alignment: Alignment.topCenter,
682+
child: Container(
683+
width: scaledWidth,
684+
height: (widget.shrinkWrap == true &&
685+
widget.mediaQueryData == null)
686+
? null
687+
: scaledHeight,
688+
// Shrink wrap height if no MediaQueryData is passed.
689+
alignment: Alignment.center,
690+
child: widget.child,
691691
),
692-
],
692+
),
693693
),
694-
);
694+
),
695+
],
696+
),
697+
);
695698
}
696699

697700
/// Return updated [MediaQueryData] values.
@@ -718,11 +721,13 @@ class _ResponsiveWrapperState extends State<ResponsiveWrapper>
718721
padding: scaledPadding);
719722
}
720723

721-
/// Builds a container with [color].
724+
/// Builds a container with widget [background] or [color].
722725
/// Defaults to a white background.
723-
Widget buildBackgroundColorWidget(Color? color) {
724-
if (color == null) return widget.background ?? Container(color: Color(0xFFFFFFFF));
725-
return Container(color: color);
726+
Widget buildBackground({Widget? background, Color? color}) {
727+
if (background != null) return background;
728+
if (color != null) return Container(color: color);
729+
730+
return Container(color: Colors.white);
726731
}
727732
}
728733

test/responsive_wrapper_test.dart

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,6 @@ void main() {
277277
Widget widget = MaterialApp(
278278
builder: (context, widget) => ResponsiveWrapper.builder(
279279
widget,
280-
background: Container(color: Colors.blue), // won't be used
281280
backgroundColor: Colors.amber,
282281
),
283282
home: Container(),
@@ -291,37 +290,38 @@ void main() {
291290
expect(find.byWidgetPredicate(widgetPredicate), findsOneWidget);
292291
});
293292

294-
testWidgets('Background Color Null Fallback', (WidgetTester tester) async {
293+
testWidgets('Background Color Null', (WidgetTester tester) async {
295294
// 0 width to simulate screen loading.
296295
setScreenSize(tester, Size(0, 1200));
297296
Widget widget = MaterialApp(
298-
builder: (context, widget) => ResponsiveWrapper.builder(
299-
widget,
300-
background: Container(color: Colors.blue),
301-
),
297+
builder: (context, widget) => ResponsiveWrapper.builder(widget),
302298
home: Container(),
303299
);
304300
// Pump once to trigger one frame build.
305301
await tester.pumpWidget(widget);
306302
// Expect only a container with default white color.
307303
WidgetPredicate widgetPredicate = (Widget widget) =>
308-
widget is Container && widget.color == Colors.blue;
304+
widget is Container && widget.color == Colors.white;
309305
// Confirm defaults.
310306
expect(find.byWidgetPredicate(widgetPredicate), findsOneWidget);
311307
});
312308

313-
testWidgets('Background Color Null', (WidgetTester tester) async {
309+
testWidgets('Background Widget', (WidgetTester tester) async {
314310
// 0 width to simulate screen loading.
315311
setScreenSize(tester, Size(0, 1200));
316312
Widget widget = MaterialApp(
317-
builder: (context, widget) => ResponsiveWrapper.builder(widget),
313+
builder: (context, widget) => ResponsiveWrapper.builder(
314+
widget,
315+
background: Container(color: Colors.blue),
316+
backgroundColor: Colors.amber, // Overriden by background widget.
317+
),
318318
home: Container(),
319319
);
320320
// Pump once to trigger one frame build.
321321
await tester.pumpWidget(widget);
322-
// Expect only a container with default white color.
323-
WidgetPredicate widgetPredicate = (Widget widget) =>
324-
widget is Container && widget.color == Colors.white;
322+
// Expect background to be a blue container.
323+
WidgetPredicate widgetPredicate =
324+
(Widget widget) => widget is Container && widget.color == Colors.blue;
325325
// Confirm defaults.
326326
expect(find.byWidgetPredicate(widgetPredicate), findsOneWidget);
327327
});

0 commit comments

Comments
 (0)