如何在颤抖中设计此自定义抽屉

时间:2020-03-24 16:50:25

标签: flutter

嘿,我现在尝试了几种方法来创建以下抽屉设计:

Drawer Design

当我使用OverflowBox和ClipOval进行比较时,这种设计是结果:

ClipOval(
  child: OverflowBox(
    maxHeight: double.infinity,
    maxWidth: double.infinity,
    child: DecoratedBox(
      decoration: BoxDecoration(
        color: AppColors.backgroundColor
      ),
      child: Container(
        height: AppConstants.screenHeight,
        width: AppConstants.screenWidth,
      ),
    ),
  ),
);

Current Design Result

如何获得以上设计。我知道必须使用溢出框和某种类型的Clipper来确保完成此操作,但是我却无法使其正常工作。

1 个答案:

答案 0 :(得分:1)

我会用一个透明的抽屉来做这个,在上面用CustomPainter在形状上绘画。通过使用主题和canvasColor: Colors.transparent 重要使抽屉透明:将抽屉的elevation设置为0,否则会看到透明抽屉的边缘。

Scaffold(
  appBar: AppBar(title: Text(title)),
  body: Center(child: Text('Custom Drawer Shape')),
  drawer: Theme(
    data: Theme.of(context).copyWith(
      canvasColor: Colors.transparent,  // set the Color of the drawer transparent; we'll paint above it with the shape
    ),
    child: Drawer(
      elevation: 0,   // set elevation to 0, otherwise you see the transparent drawer a little bit
      child: Container(
        padding: EdgeInsets.fromLTRB(0, 0, 0, 0),
        child: CustomPaint(
          painter: DrawerPainter(color: Colors.white), // this is your custom painter
          child: ListView(
            padding: EdgeInsets.fromLTRB(0, 20, 0, 0),
            children: <Widget>[
              // Add your menu e.g. with ListTile
            ],
          ),
        ),
      ),
    ),
  ),
);

// Class which draws the custom shape
class DrawerPainter extends CustomPainter {
  final Color color;
  DrawerPainter({this.color = Colors.black});

  @override
  void paint(Canvas canvas, Size size) {
    Paint paint = Paint()
      ..color = color
      ..strokeWidth = 3
      ..style = PaintingStyle.fill;
    canvas.drawPath(getShapePath(size.width, size.height), paint);
  }

  // This is the path of the shape we want to draw
  Path getShapePath(double x, double y) {
    return Path()
      ..moveTo(0, 0)
      ..lineTo(x / 2, 0)
      ..quadraticBezierTo(x, y / 2, x / 2, y)
      ..lineTo(0, y);
  }

  @override
  bool shouldRepaint(DrawerPainter oldDelegate) {
    return oldDelegate.color != color;
  }
}