3

I want to make a screen like this in flutter :

enter image description here

Can anyone suggest how can i make containers like this in flutter and design like this, Thanks in advance.

Karan Mehta
  • 1,442
  • 13
  • 32
  • I found a similar answered question to yours.Maybe this will help https://stackoverflow.com/questions/56170150/how-to-achieve-a-custom-shaped-container-in-flutter – Jordan Kotiadis Jul 21 '20 at 06:55

1 Answers1

2

You can give your containers a custom shape by using the ClipPath class.

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: ClipPath(
            child: Container(color: Colors.red),
            clipper: MyCustomClipper(),
          ),
        ),
      ));
  }
}

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

  @override
  bool shouldReclip(CustomClipper<Path> oldClipper) => false;
}
nvoigt
  • 75,013
  • 26
  • 93
  • 142
  • 1
    `ClipPath` is not a best solution for giving a `Container` a custom shape - use `ShapeDecoration` for that, [here](https://stackoverflow.com/a/57943257/2252830) you have some sample code – pskink Jul 21 '20 at 07:19
  • @pskink can you please create an example like that for me please? – Karan Mehta Jul 21 '20 at 07:31
  • @pskink I am new to flutter, can you explain *why* the ShapeDecoration is better in this case? – nvoigt Jul 21 '20 at 07:58
  • you can apply shadows for example, perform test hitting but also when implementing `lerp*` methods you can have "morphing" shapes like here: https://github.com/flutter/flutter/blob/c969b8af7b/packages/flutter/lib/src/painting/rounded_rectangle_border.dart#L55, this "morphing" can be applied to other `Deoration`s as well - check `CornerDecorationTest` from https://gist.github.com/pskink/da43c327b75eec05d903fa1b4d0c4d3e#file-decorations-dart-L7 - simply click inside that `CornerDecorationTest` and watch the magic ;-) – pskink Jul 21 '20 at 08:07
  • @pskink Oh, okay, so it's better in general if you want more features than the OP. I had assumed that when you said "better" you meant better in this specific case, maybe more effective redrawing or less complicated or something like that. – nvoigt Jul 21 '20 at 11:30
  • both solutions are implemented the same way: you have to construct shape's `Path` - that's all, but when using `ShapeBorder` you don't need additional `ClipPath` widget – pskink Jul 21 '20 at 12:45
  • I'm not able to understand at all what you've done. @pskink – Karan Mehta Jul 23 '20 at 04:17
  • @KeranMehta here you have a working code: https://stackoverflow.com/a/57943257/2252830 – pskink Jul 23 '20 at 04:34