flutterdartflutter-clippath

How can I add certain ClipPath to every page in flutter by writting the code, one time?


ClipPath(
        child: Container(
          height: 200,
          decoration: const BoxDecoration(
            gradient: LinearGradient(
              colors: [Color(0xFF7CB342), Color(0xFFDCEDC8)],
            ),
          ),
          child: Center(child: Text('mainPage')),
        ),
      ),

I want to apply this code to every pages that will open in my app, How can i do it?


Solution

  • You can create a widget like this:

    class ClipWidget extends StatelessWidget {
      final Widget? childWidget;
    
      const ClipWidget({Key? key, this.childWidget}) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return ClipPath(
          child: Container(
            height: 200,
            decoration: const BoxDecoration(
              gradient: LinearGradient(
                colors: [Color(0xFF7CB342), Color(0xFFDCEDC8)],
              ),
            ),
            child: childWidget),
          );
      }
    }
    

    and use it like:

    return ClipWidget(childWidget:const Center(child: Text('mainPage')));