Bird
Raised Fist0
Fluttermobile~20 mins

ClipRRect and ClipPath in Flutter - Mini App: Build & Ship

Choose your learning style10 modes available
Build: Clip Demo Screen
This screen shows how to use ClipRRect and ClipPath to clip widgets with rounded corners and custom shapes.
Target UI
-------------------------
| Clip Demo Screen       |
|-----------------------|
| [Rounded Image]        |
|                       |
| [Custom Shape Box]     |
|                       |
|                       |
-------------------------
Display an image clipped with ClipRRect with 20 radius corners.
Below the image, show a colored box clipped with ClipPath in a triangle shape.
Use a custom clipper class for the ClipPath.
Center both clipped widgets vertically with some spacing.
Starter Code
Flutter
import 'package:flutter/material.dart';

class ClipDemoScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Clip Demo Screen')),
      body: Center(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            // TODO: Add ClipRRect with image here
            SizedBox(height: 40),
            // TODO: Add ClipPath with custom shape here
          ],
        ),
      ),
    );
  }
}

// TODO: Define custom clipper class here
Task 1
Task 2
Task 3
Solution
Flutter
import 'package:flutter/material.dart';

class ClipDemoScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Clip Demo Screen')),
      body: Center(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            ClipRRect(
              borderRadius: BorderRadius.circular(20),
              child: Image.network(
                'https://flutter.github.io/assets-for-api-docs/assets/widgets/owl.jpg',
                width: 150,
                height: 150,
                fit: BoxFit.cover,
              ),
            ),
            SizedBox(height: 40),
            ClipPath(
              clipper: TriangleClipper(),
              child: Container(
                width: 150,
                height: 150,
                color: Colors.blueAccent,
              ),
            ),
          ],
        ),
      ),
    );
  }
}

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

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

We use ClipRRect to clip the image with rounded corners by setting borderRadius to 20 pixels. This makes the image corners smooth and rounded.

Below it, we use ClipPath with a custom clipper TriangleClipper that clips a container into a triangle shape. The getClip method defines the triangle path by connecting the top center point to bottom left and bottom right corners.

Both clipped widgets are centered vertically with spacing using SizedBox.

Final Result
Completed Screen
-------------------------
| Clip Demo Screen       |
|-----------------------|
|   _______             |
|  /       \            |
| |  Image  |           |
|  \_______/            |
|                       |
|      /\               |
|     /  \              |
|    /____\             |
|                       |
-------------------------
User sees an image with rounded corners at top.
Below the image is a blue triangle-shaped box.
No interactive elements on this screen.
Stretch Goal
Add a button that toggles the triangle color between blue and green when tapped.
💡 Hint
Use a StatefulWidget and setState to change the color variable on button press.

Practice

(1/5)
1. What is the main purpose of the ClipRRect widget in Flutter?
easy
A. To add padding around a widget
B. To clip a widget with a custom path shape
C. To animate a widget's size
D. To clip a widget with rounded rectangle corners

Solution

  1. Step 1: Understand ClipRRect usage

    The ClipRRect widget clips its child using a rounded rectangle shape.
  2. Step 2: Compare with other options

    ClipPath clips with custom shapes, padding adds space, animation changes size.
  3. Final Answer:

    To clip a widget with rounded rectangle corners -> Option D
  4. Quick Check:

    ClipRRect = Rounded rectangle clip [OK]
Hint: ClipRRect always clips with rounded rectangles [OK]
Common Mistakes:
  • Confusing ClipRRect with ClipPath
  • Thinking ClipRRect adds padding
  • Assuming ClipRRect animates widgets
2. Which of the following is the correct syntax to clip a widget with rounded corners using ClipRRect?
easy
A. ClipRRect(borderRadius: BorderRadius.circular(10), child: Text('Hello'))
B. ClipRRect(radius: 10, child: Text('Hello'))
C. ClipRRect(cornerRadius: 10, child: Text('Hello'))
D. ClipRRect(borderRadius: 10, child: Text('Hello'))

Solution

  1. Step 1: Recall ClipRRect property names

    The correct property for rounded corners is borderRadius with a BorderRadius.circular() value.
  2. Step 2: Check syntax correctness

    ClipRRect(borderRadius: BorderRadius.circular(10), child: Text('Hello')) uses correct property and value type; others use wrong property names or types.
  3. Final Answer:

    ClipRRect(borderRadius: BorderRadius.circular(10), child: Text('Hello')) -> Option A
  4. Quick Check:

    borderRadius with BorderRadius.circular() is correct [OK]
Hint: Use borderRadius: BorderRadius.circular() for ClipRRect [OK]
Common Mistakes:
  • Using radius or cornerRadius instead of borderRadius
  • Passing integer directly instead of BorderRadius.circular()
  • Omitting the child widget
3. What will be the visual result of this Flutter code snippet?
ClipRRect(
  borderRadius: BorderRadius.circular(20),
  child: Container(
    width: 100,
    height: 100,
    color: Colors.blue,
  ),
)
medium
A. A blue square with rounded corners of radius 20
B. A blue square with sharp corners
C. A blue circle with radius 20
D. A blue rectangle with no clipping

Solution

  1. Step 1: Analyze ClipRRect effect

    The ClipRRect clips the child container with rounded corners of radius 20.
  2. Step 2: Understand container shape

    The container is a 100x100 square colored blue, so clipping rounds its corners.
  3. Final Answer:

    A blue square with rounded corners of radius 20 -> Option A
  4. Quick Check:

    ClipRRect rounds corners = blue square with rounded corners [OK]
Hint: ClipRRect rounds corners, does not make circles [OK]
Common Mistakes:
  • Thinking ClipRRect creates circles
  • Ignoring borderRadius effect
  • Assuming no clipping happens
4. Identify the error in this Flutter code using ClipPath:
ClipPath(
  clipper: CustomClipper(),
  child: Container(color: Colors.red, width: 100, height: 100),
)
medium
A. Container cannot be a child of ClipPath
B. clipper property should be a Widget, not a CustomClipper
C. CustomClipper is an abstract class and cannot be used directly
D. Colors.red is not a valid color

Solution

  1. Step 1: Understand CustomClipper usage

    CustomClipper is an abstract class; you must create a subclass implementing its methods.
  2. Step 2: Check clipper property type

    The clipper property requires an instance of a subclass of CustomClipper, not the abstract class itself.
  3. Final Answer:

    CustomClipper is an abstract class and cannot be used directly -> Option C
  4. Quick Check:

    CustomClipper must be subclassed before use [OK]
Hint: CustomClipper must be subclassed, not used directly [OK]
Common Mistakes:
  • Using CustomClipper directly without subclassing
  • Confusing clipper property type
  • Thinking Container can't be child
5. You want to clip a widget into a triangle shape using Flutter. Which approach correctly uses ClipPath to achieve this?
hard
A. Use ClipRRect with borderRadius set to 0
B. Use ClipPath with a CustomClipper that defines a triangle path
C. Use ClipOval with a rectangular child
D. Use Container with padding and background color

Solution

  1. Step 1: Understand ClipPath flexibility

    ClipPath allows clipping with any shape by defining a custom path in a subclass of CustomClipper.
  2. Step 2: Identify correct method for triangle shape

    To clip a triangle, you must create a CustomClipper that draws a triangle path and pass it to ClipPath.
  3. Step 3: Eliminate incorrect options

    ClipRRect only clips rounded rectangles, ClipOval clips ovals/circles, and Container does not clip shapes.
  4. Final Answer:

    Use ClipPath with a CustomClipper that defines a triangle path -> Option B
  5. Quick Check:

    ClipPath + CustomClipper = any shape clip [OK]
Hint: ClipPath + CustomClipper = clip any shape [OK]
Common Mistakes:
  • Trying to clip triangle with ClipRRect
  • Using ClipOval for non-oval shapes
  • Confusing padding with clipping
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard