build method
Describes the part of the user interface represented by this widget.
The framework calls this method when this widget is inserted into the tree in a given BuildContext and when the dependencies of this widget change (e.g., an InheritedWidget referenced by this widget changes). This method can potentially be called in every frame and should not have any side effects beyond building a widget.
The framework replaces the subtree below this widget with the widget returned by this method, either by updating the existing subtree or by removing the subtree and inflating a new subtree, depending on whether the widget returned by this method can update the root of the existing subtree, as determined by calling Widget.canUpdate.
Typically implementations return a newly created constellation of widgets that are configured with information from this widget's constructor and from the given BuildContext.
The given BuildContext contains information about the location in the tree at which this widget is being built. For example, the context provides the set of inherited widgets for this location in the tree. A given widget might be built with multiple different BuildContext arguments over time if the widget is moved around the tree or if the widget is inserted into the tree in multiple places at once.
The implementation of this method must only depend on:
- the fields of the widget, which themselves must not change over time, and
- any ambient state obtained from the
contextusing BuildContext.dependOnInheritedWidgetOfExactType.
If a widget's build method is to depend on anything else, use a StatefulWidget instead.
See also:
- StatelessWidget, which contains the discussion on performance considerations.
Implementation
@override
Widget build(BuildContext context) {
// If glass is disabled, just use normal Container
if (!glassEnabled) {
return Container(
alignment: alignment,
padding: padding,
color: color,
decoration: decoration,
foregroundDecoration: foregroundDecoration,
width: width,
height: height,
constraints: constraints,
margin: margin,
transform: transform,
transformAlignment: transformAlignment,
clipBehavior: clipBehavior,
child: child,
);
}
// Create base container with decoration
// Wrap in SizedBox to ensure fixed size when width/height are specified
Widget baseContainer = Container(
width: width,
height: height,
decoration:
decoration ?? (color != null ? BoxDecoration(color: color) : null),
);
// If width or height specified, enforce with tight constraints
if (width != null || height != null) {
baseContainer = ConstrainedBox(
constraints: BoxConstraints.tightFor(
width: width,
height: height,
),
child: baseContainer,
);
}
// Apply glass shader effect
Widget glassContainer = ShaderBuilder(
(context, shader, _) {
return AnimatedSampler(
(image, size, canvas) {
// Set uniforms
shader.setFloatUniforms((u) {
u.setSize(size);
});
shader.setImageSampler(0, image);
// Draw the shader covering full size
canvas.drawRect(
Offset.zero & size,
Paint()..shader = shader,
);
},
child: baseContainer,
);
},
assetKey: ShaderUtils.getShaderAssetPath('liquid_glass_shader.frag'),
);
// Stack: glass background + child on top
Widget result = Stack(
fit: StackFit.loose,
children: [
// Glass effect background
glassContainer,
// Child content on top - positioned to fill
if (child != null)
Positioned.fill(
child: Container(
alignment: alignment,
padding: padding,
child: child,
),
),
],
);
// If width/height specified, wrap with SizedBox to enforce exact size
// UnconstrainedBox breaks parent constraints so Stack doesn't expand
if (width != null || height != null) {
result = UnconstrainedBox(
child: SizedBox(
width: width,
height: height,
child: result,
),
);
}
// Apply clipping FIRST if decoration has border radius
// This ensures the shader output is properly clipped
if (decoration != null && decoration is BoxDecoration) {
final boxDecoration = decoration as BoxDecoration;
if (boxDecoration.borderRadius != null) {
result = ClipRRect(
borderRadius: boxDecoration.borderRadius as BorderRadius,
clipBehavior: clipBehavior,
child: result,
);
}
}
// Apply constraints if specified
if (constraints != null) {
result = ConstrainedBox(
constraints: constraints!,
child: result,
);
}
// Apply foreground decoration if specified
if (foregroundDecoration != null) {
result = DecoratedBox(
position: DecorationPosition.foreground,
decoration: foregroundDecoration!,
child: result,
);
}
// Apply transform if specified
if (transform != null) {
result = Transform(
transform: transform!,
alignment: transformAlignment,
child: result,
);
}
// Apply margin if specified
if (margin != null) {
result = Padding(
padding: margin!,
child: result,
);
}
return result;
}