Open In App

ClipRRect Widget in Flutter

The ClipRRect widget in flutter is used to clips its child using a rounded rectangle.  It associates with the Clippers family. The main use of clippers is to clip out any portion of the widget as required. It behaves similar to that of ClipRect and is used to Clip a Rectangle portion of the child widget but with rounded corners

Constructor:

Syntax:
ClipRRect(
{Key key,
BorderRadius borderRadius: BorderRadius.zero,
CustomClipper<RRect> clipper, 
Clip clipBehavior: Clip.antiAlias, 
Widget child})

Properties:

Methods:

@override
RenderClipRect createRenderObject (
BuildContext context
)
override
 @override
void debugFillProperties (
DiagnosticPropertiesBuilder properties
)
override
@override
void didUnmountRenderObject (
covariant RenderClipRect renderObject
)
override

Example:



Here we will clip the below image with a rounded corner rectangle:






import 'package:flutter/material.dart';
 
void main() {
  runApp(MyApp());
}
 
class MyApp extends StatelessWidget {
  // This widget is
  //the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'ClipOval',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePAGE(),
      debugShowCheckedModeBanner: false,
    );
  }
}
 
class MyHomePAGE extends StatefulWidget {
  @override
  _MyHomePAGEState createState() => _MyHomePAGEState();
}
 
class _MyHomePAGEState extends State<MyHomePAGE> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('GeeksforGeeks'),
        backgroundColor: Colors.green,
      ),
      body: Center(
          child: ClipRRect(
            borderRadius:
            BorderRadius.circular(10),
              child: Image.network('https://picsum.photos/250?image=9'),
            ),
          )
      );
  }
}
 
class MyClip extends CustomClipper<Rect> {
  Rect getClip(Size size) {
    return Rect.fromLTWH(0, 0, 100, 100);
  }
 
  bool shouldReclip(oldClipper) {
    return false;
  }
}

Output:

Explanation:

  1. First initialize the main app as a stateless widget.
  2. Second design the main widget as you desire.
  3. Build the Appbar with the scaffold widget.
  4. Now use the ClipRect widget inside the body of the scaffold widget and place it in the middle using the center widget.

Article Tags :