0
0
Fluttermobile~20 mins

ClipRRect and ClipPath in Flutter - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Clip Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
What is the visual output of this ClipRRect widget?
Consider this Flutter widget code snippet:
ClipRRect(
  borderRadius: BorderRadius.circular(20),
  child: Container(
    width: 100,
    height: 100,
    color: Colors.blue,
  ),
)

What will the blue container look like on screen?
Flutter
ClipRRect(
  borderRadius: BorderRadius.circular(20),
  child: Container(
    width: 100,
    height: 100,
    color: Colors.blue,
  ),
)
AA blue circle with diameter 100 pixels.
BA blue square with rounded corners of radius 20 pixels, 100x100 pixels.
CA blue square with sharp corners, 100x100 pixels.
DA blue rectangle with no color visible.
Attempts:
2 left
💡 Hint
ClipRRect clips its child with rounded corners defined by borderRadius.
ui_behavior
intermediate
2:00remaining
What shape does this ClipPath create?
Given this Flutter code:
ClipPath(
  clipper: TriangleClipper(),
  child: Container(
    width: 100,
    height: 100,
    color: Colors.red,
  ),
)

class TriangleClipper extends CustomClipper {
  @override
  Path getClip(Size size) {
    Path path = Path();
    path.moveTo(size.width / 2, 0);
    path.lineTo(0, size.height);
    path.lineTo(size.width, size.height);
    path.close();
    return path;
  }

  @override
  bool shouldReclip(CustomClipper oldClipper) => false;
}

What shape will the red container be clipped into?
Flutter
ClipPath(
  clipper: TriangleClipper(),
  child: Container(
    width: 100,
    height: 100,
    color: Colors.red,
  ),
)

class TriangleClipper extends CustomClipper<Path> {
  @override
  Path getClip(Size size) {
    Path path = Path();
    path.moveTo(size.width / 2, 0);
    path.lineTo(0, size.height);
    path.lineTo(size.width, size.height);
    path.close();
    return path;
  }

  @override
  bool shouldReclip(CustomClipper<Path> oldClipper) => false;
}
AA red square with sharp corners.
BA red circle with diameter 100 pixels.
CA red triangle pointing upwards.
DA red rectangle with rounded corners.
Attempts:
2 left
💡 Hint
The custom clipper defines a path with three points forming a triangle.
lifecycle
advanced
2:00remaining
When does Flutter call shouldReclip in a CustomClipper?
In a CustomClipper used with ClipPath, when is the method shouldReclip called by Flutter?
ANever, it is only a placeholder method.
BEvery frame during animations regardless of changes.
COnly once when the widget is first built.
DWhen the clipper instance changes to decide if the clip needs updating.
Attempts:
2 left
💡 Hint
Think about when Flutter needs to know if it should redraw the clip shape.
📝 Syntax
advanced
2:00remaining
Which ClipRRect code snippet correctly applies a circular border radius?
Choose the correct Flutter code to clip a widget with a circular border radius of 50 pixels.
AClipRRect(borderRadius: BorderRadius.circular(50.0), child: widget)
BClipRRect(borderRadius: BorderRadius.circular('50'), child: widget)
CClipRRect(borderRadius: BorderRadius.all(Radius.circular(50)), child: widget)
DClipRRect(borderRadius: BorderRadius.circular(50), child: widget)
Attempts:
2 left
💡 Hint
The radius value must be a double, not a string.
🔧 Debug
expert
2:00remaining
Why does this ClipPath not clip the child widget as expected?
This Flutter code uses ClipPath with a custom clipper but the child widget is not clipped:
ClipPath(
  clipper: MyClipper(),
  child: Container(
    width: 100,
    height: 100,
    color: Colors.green,
  ),
)

class MyClipper extends CustomClipper {
  @override
  Path getClip(Size size) {
    return Path();
  }

  @override
  bool shouldReclip(CustomClipper oldClipper) => false;
}

What is the reason the clipping does not happen?
Flutter
ClipPath(
  clipper: MyClipper(),
  child: Container(
    width: 100,
    height: 100,
    color: Colors.green,
  ),
)

class MyClipper extends CustomClipper<Path> {
  @override
  Path getClip(Size size) {
    return Path();
  }

  @override
  bool shouldReclip(CustomClipper<Path> oldClipper) => false;
}
AThe getClip method returns an empty Path, so no clipping area is defined.
BClipPath requires a borderRadius property to clip.
CThe Container has no size, so clipping is ignored.
DThe shouldReclip method returns false, preventing clipping.
Attempts:
2 left
💡 Hint
Check what the getClip method returns as the clipping shape.