简体   繁体   English

如何使用 CarouselSlider 制作 customPaint

[英]How to make a customPaint with CarouselSlider

在此处输入图片说明

I'm trying to Achieve the Above Design :我正在努力实现上述设计:

what what I did is creating CarouselSlider with a custom painter with help of stack and position property我所做的是在堆栈和位置属性的帮助下使用自定义画家创建 CarouselSlider

what is not working for me is that can't laying any other image or text over my custom shape对我不起作用的是不能在我的自定义形状上放置任何其他图像或文本

                CarouselSlider.builder(
                itemCount: coverImage.length,
                itemBuilder: (context, index, realIdx) {
                  return CustomPaint(
                    size: Size(320, (320 * 0.5).toDouble()),
                    //You can Replace [WIDTH] with your desired width for Custom Paint and height will be calculated automatically
                    painter: RPSCustomPainter(),
                  );
                },

the original design :原始设计:

在此处输入图片说明

Is this the best way to do it ?这是最好的方法吗?

Use CarouselOptions to set size.使用CarouselOptions设置大小。

options: CarouselOptions(
          /// here controll the CardSize
          aspectRatio: 400 / 300, 
          height: 300,
          onPageChanged: (index, reason) {
            print(index.toString());
          },
        ),

Also, you can wrap with Row to use CustomPaint 's size: but I don't recommend to use here extra multChild-Widget .此外,您可以使用 Row 包装以使用CustomPaintsize:但我不建议在这里使用额外的 multChild-Widget 。

Here is the demo widget.这是演示小部件。


class BendTW extends StatelessWidget {
  const BendTW({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: CarouselSlider.builder(
        itemCount: 33,
        options: CarouselOptions(
          /// here controll the CardSize
          aspectRatio: 400 / 300,
          height: 300,

          onPageChanged: (index, reason) {
            print(index.toString());
          },
        ),
        itemBuilder: (context, index, realIdx) {
          return
              // Row(  //* Row not recommend
              //   children: [
              CustomPaint(
            size: const Size(50, 30), /* this size have no Effect on item without row
            painter: MyPaint(),
            child: Stack(
              children: [
                Align(
                  alignment: Alignment.topCenter,
                  child: Text("Item $index"),
                ),
                Align(
                  alignment: Alignment.bottomCenter,
                  child: Text("Item vB: $index"),
                ),
              ],
            ),
            //   ),
            // ],
          );
        },
      ),
    );
  }
}

class MyPaint extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    Paint paint = Paint()
      ..color = Colors.orange
      ..style = PaintingStyle.fill;

    canvas.drawRect(
        Rect.fromLTRB(10, 10, size.width - 10, size.height - 10), paint);
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return false;
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM