Open In App

Flutter – ClipPath Widget

Last Updated : 30 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In Flutter, the ClipPath widget is used to clip its child widget using a custom path. This means you can define a specific shape or path, and the ClipPath widget will restrict the rendering of its child to be only within that shape or along that path. In this article, we are going to implement the ClipPath widget. A sample Image is given below to get an idea about what we are going to do in this article.

Screenshot_2023-10-08-17-01-16-641_comexamplevideoplayer-(2)

Basic Syntax of ClipPath Widget

ClipPath(
clipper: YourCustomClipper(), // A CustomClipper<Path> that defines the clipping path.
child: YourChildWidget(), // The child widget that you want to clip.
)

Required Tools

To build this app, you need the following items installed on your machine:

  • Visual Studio Code / Android Studio
  • Android Emulator / iOS Simulator / Physical Device device.
  • Flutter Installed
  • Flutter plugin for VS Code / Android Studio.

Step By Step Implementation

Step 1: Create a New Project in Android Studio

To set up Flutter Development on Android Studio please refer to Android Studio Setup for Flutter Development, and then create a new project in Android Studio please refer to Creating a Simple Application in Flutter.

Step 2: Import the Package

First of all import material.dart file.

import 'package:flutter/material.dart';

Step 3: Execute the main Method

Here the execution of our app starts.

Dart




void main() => runApp(MyApp());


Step 4: Create MyApp Class

In this class we are going to implement the MaterialApp , here we are also set the Theme of our App.

Dart




class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        primarySwatch: Colors.green, // Set the app's primary theme color
      ),
      debugShowCheckedModeBanner: false,
      home: ClipPathExample(),
    );
  }
}


Step 5: Create ClipPathExample Class

In this class we are going to Implement the ClipPath widget that help to create a custom path is defined using the MyClipper class as the clipper. The MyClipper class extends CustomClipper<Path> and defines a custom path that resembles a banner. It will create a Hexagon like structure .Comments are added for better understanding.

ClipPath(
// Define a custom path to clip the child
clipper: MyClipper(),
child: Container(
width: 200,
height: 200,
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [Colors.orange, Colors.white, Colors.green],
begin: Alignment.topLeft,
end: Alignment.bottomRight,
),
),
child: Center(
child: Text(
'ClipPath',
style: TextStyle(
color: const Color.fromARGB(255, 117, 95, 95),
fontSize: 24,
fontWeight: FontWeight.bold,
),
),
),
),
),

Dart




class ClipPathExample extends StatefulWidget {
  const ClipPathExample({Key? key}) : super(key: key);
  
  @override
  State<ClipPathExample> createState() => _ClipPathExampleState();
}
  
class _ClipPathExampleState extends State<ClipPathExample> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('ClipPath Example'),
      ),
      body: Center(
        child: ClipPath(
          // Define a custom path to clip the child
          clipper: MyClipper(),
          child: Container(
            width: 200,
            height: 200,
            decoration: BoxDecoration(
              gradient: LinearGradient(
                colors: [Colors.orange, Colors.white, Colors.green],
                begin: Alignment.topLeft,
                end: Alignment.bottomRight,
              ),
            ),
            child: Center(
              child: Text(
                'ClipPath',
                style: TextStyle(
                  color: const Color.fromARGB(255, 117, 95, 95),
                  fontSize: 24,
                  fontWeight: FontWeight.bold,
                ),
              ),
            ),
          ),
        ),
      ),
    );
  }
}


Step 6: Create MyClipper Class

The MyClipper class defines a custom path in the getClip method. This path specifies the shape that will be used to clip the child (Container).it create a hexagon like structure It also contains a method names as shouldReclip. The shouldReclip method of the MyClipper class returns false, indicating that the clipping path does not need to be recomputed.

Dart




class MyClipper extends CustomClipper<Path> {
  @override
  Path getClip(Size size) {
    // Define a custom path to create a stylish clipped shape
    var path = Path();
    path.moveTo(0, size.height * 0.5);
    path.lineTo(size.width * 0.2, 0);
    path.lineTo(size.width * 0.8, 0);
    path.lineTo(size.width, size.height * 0.5);
    path.lineTo(size.width * 0.8, size.height);
    path.lineTo(size.width * 0.2, size.height);
    path.close();
    return path;
  }
  
  @override
  bool shouldReclip(CustomClipper<Path> oldClipper) {
    return false;
  }
}


Here is the full Code of main.dart file

Dart




import 'package:flutter/material.dart';
  
void main() => runApp(MyApp());
  
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        primarySwatch: Colors.green, // Set the app's primary theme color
      ),
      debugShowCheckedModeBanner: false,
      home: ClipPathExample(),
    );
  }
}
  
class ClipPathExample extends StatefulWidget {
  const ClipPathExample({Key? key}) : super(key: key);
  
  @override
  State<ClipPathExample> createState() => _ClipPathExampleState();
}
  
class _ClipPathExampleState extends State<ClipPathExample> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('ClipPath Example'),
      ),
      body: Center(
        child: ClipPath(
          // Define a custom path to clip the child
          clipper: MyClipper(),
          child: Container(
            width: 200,
            height: 200,
            decoration: BoxDecoration(
              gradient: LinearGradient(
                colors: [Colors.orange, Colors.white, Colors.green],
                begin: Alignment.topLeft,
                end: Alignment.bottomRight,
              ),
            ),
            child: Center(
              child: Text(
                'ClipPath',
                style: TextStyle(
                  color: const Color.fromARGB(255, 117, 95, 95),
                  fontSize: 24,
                  fontWeight: FontWeight.bold,
                ),
              ),
            ),
          ),
        ),
      ),
    );
  }
}
  
class MyClipper extends CustomClipper<Path> {
  @override
  Path getClip(Size size) {
    // Define a custom path to create a stylish clipped shape
    var path = Path();
    path.moveTo(0, size.height * 0.5);
    path.lineTo(size.width * 0.2, 0);
    path.lineTo(size.width * 0.8, 0);
    path.lineTo(size.width, size.height * 0.5);
    path.lineTo(size.width * 0.8, size.height);
    path.lineTo(size.width * 0.2, size.height);
    path.close();
    return path;
  }
  
  @override
  bool shouldReclip(CustomClipper<Path> oldClipper) {
    return false;
  }
}


Output:

Screenshot_2023-10-08-17-01-16-641_comexamplevideoplayer-(2)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads