简体   繁体   English

如何在顶部容器上打孔?

[英]How to make holes into top container?

I am trying to build a dial plate in Flutter.我正在尝试在 Flutter 中构建一个拨号盘。 The bottom plate is just a stateless plate with numbers on top of it.底板只是一个无国籍的板,上面有数字。 The top plate should be Stateful and have a few holes in it so that you can see the numbers underneath it, just like a real dial plate.顶板应该是有状态的,上面有几个孔,这样你就可以看到它下面的数字,就像一个真正的拨号盘。

Some tips on how to accomplish this would be appreciated.关于如何实现这一点的一些提示将不胜感激。 I don't want complete solutions, because I want to do it by myself, but rather some helpful tips that makes it a little bit easier.我不想要完整的解决方案,因为我想自己做,而是一些有用的提示,可以让它更容易一些。 I've been searching a couple of days but haven't found anything that helps me.我已经搜索了几天,但没有找到任何对我有帮助的东西。 Please no answers which are too complicated for me as I am an absolute beginner, now 3 months into programming and flutter.请不要回答对我来说太复杂的答案,因为我是一个绝对的初学者,现在 3 个月的编程和颤振。

You can use Path.combine mixed with ' difference ' operation to cut holes in top container.您可以使用Path.combine与“ 差异”操作混合在顶部容器中切孔。

Path combine (路径组合(
PathOperation operation, PathOperation 操作,
Path path1,路径 path1,
Path path2路径 path2
) )

Combines the two paths according to the manner specified by the given operation.根据给定操作指定的方式组合两个路径。

The resulting path will be constructed from non-overlapping contours.生成的路径将由不重叠的轮廓构建。 The curve order is reduced where possible so that cubics may be turned into quadratics, and quadratics maybe turned into lines.在可能的情况下减少曲线阶数,以便三次可以变成二次方,二次方可以变成线。

=========== ============

difference → const PathOperation差异→ const PathOperation

Subtract the second path from the first path.从第一条路径中减去第二条路径。

For example, if the two paths are overlapping circles of equal diameter but differing centers, the result would be a crescent portion of the first circle that was not overlapped by the second circle.例如,如果两条路径是直径相等但中心不同的重叠圆,则结果将是第一个圆的新月形部分没有被第二个圆重叠。

Please see the code below.请看下面的代码。

import 'package:flutter/material.dart';

final Color darkBlue = Color.fromARGB(255, 18, 32, 47);

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue),
      debugShowCheckedModeBanner: false,
      home: CutHoleDemo(),
    );
  }
}

class CutHoleDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Cut Hole in Top Container Demo.'),
      ),
      body: Center(
        child: Container(
          width: 300,
          height: 300,
          color: Colors.blueGrey,
          child: Stack(
            children: [
              Center(
                child: Text(
                  "Bottom\nContainer\nText",
                  style: TextStyle(color: Colors.white),
                  textAlign: TextAlign.center,
                ),
              ),
              CustomPaint(
                painter: CustomContainerWithHole(),
                child: Center(
                  child: Container(
                    height: 200,
                    width: 200,
                    color: Colors.transparent,
                    child: Align(
                      alignment: Alignment.bottomCenter,
                      child: Text(
                        "Top Container Text",
                        style: TextStyle(color: Colors.black),
                      ),
                    ),
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

class CustomContainerWithHole extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    final paint = Paint();
    paint.color = Colors.grey;
    canvas.drawPath(
      Path.combine(
        PathOperation.difference,
        Path()
          ..addRRect(RRect.fromLTRBR(50, 50, 250, 250, Radius.circular(10))),
        Path()
          ..addOval(Rect.fromCircle(center: Offset(150, 150), radius: 50))
          ..close(),
      ),
      paint,
    );
  }

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

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

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