【问题标题】:How to do a background grid color [closed]如何做背景网格颜色[关闭]
【发布时间】:2020-08-07 07:22:07
【问题描述】:

我正在创建一个应用程序,我想做一个像这张图片一样的背景:

我怎样才能以最简单的方式做到这一点?

【问题讨论】:

  • 到目前为止你尝试了什么?和动画有什么关系?我们不会在这个网站上做你的工作,我们帮助你做你自己的工作。因此,您需要告诉我们到目前为止您做了什么,以及您遇到的确切问题是什么,以便我们提供帮助。
  • 我正在寻找最好的方法来做到这一点!我尝试使用 gridview,但我正在寻找一种更简单的方法
  • 没有一种最好的方法来做到这一点。计算中的任何事物都没有适合每个人的最佳方法。网格可能需要更少的资源和更多的编码工作。预先绘制的图片可能会使用更多资源,扩展性稍差,但更容易实现。如果我们不知道您为什么认为您的尝试不够好,那么没有人可以帮助您。这就是为什么我们需要了解您做了什么以及您想改进什么。
  • 我投票结束这个问题,因为 SO 不是代码编写服务

标签: flutter flutter-layout flutter-animation


【解决方案1】:

这是我绘制网格线的简单方法

  _drawGridLines({double space = 30, Color color = Colors.red, Widget child}) {
    return LayoutBuilder(
      builder: (BuildContext context, BoxConstraints constraints) {
        double width = constraints.maxWidth;
        double height = constraints.maxHeight;
        var h = Container(width: 2, height: height, color: color);
        var v = Container(width: width, height: 2, color: color);
        return Stack(children: <Widget>[
          ...List.generate((width / space).round(), (index) => Positioned(left: index * space, child: h)),
          ...List.generate((height / space).round(), (index) => Positioned(top: index * space, child: v)),
          if(child != null) child,
        ]);
      },
    );
  }

用法

class Test extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Scaffold(body: _drawGridLines()),
    );
  }
}

【讨论】:

    【解决方案2】:

    它使用 CustomPaint

    举例

    class _MyHomePageState extends State<MyHomePage> {
      int _counter = 0;
    
      void _incrementCounter() {
        getHttp();
    //
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text(widget.title),
          ),
          body: CustomPaint(
            painter: BacgroundPaint(),
            child: Center(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Text(
                    'You have pushed the button this many times:',
                  ),
                  Text(
                    '$_counter',
                    style: Theme.of(context).textTheme.headline4,
                  ),
                ],
              ),
            ),
          ),
        );
      }
    }
    
    class BacgroundPaint extends CustomPainter {
      @override
      void paint(Canvas canvas, Size size) {
        final height = size.height;
        final width = size.width;
        final paint = Paint();
    
        Path mainBackground = Path();
        mainBackground.addRect(Rect.fromLTRB(0, 0, width, height));
        paint.color = Colors.teal;
    
        final heightLine = height ~/ 20; // your Horizontal line
        final widthLine = (width ~/ 10); // your Vertical line
    
        for(int i = 1 ; i < height ; i++){
          if(i % heightLine == 0){
             Path linePath = Path();
             linePath.addRect(Rect.fromLTRB(0, i.toDouble(), width, (i+2).toDouble()));
             canvas.drawPath(linePath, paint);
          }
        }
        for(int i = 1 ; i < width ; i++){
          if(i % widthLine == 0){
            Path linePath = Path();
            linePath.addRect(Rect.fromLTRB(i.toDouble(), 0 , (i+2).toDouble(), height));
            canvas.drawPath(linePath, paint);
          }
        }
      }
    
      @override
      bool shouldRepaint(CustomPainter oldDelegate) {
        return oldDelegate != this;
      }
    }
    

    【讨论】:

      【解决方案3】:

      您可以通过在您的材料应用程序中创建一个容器来做到这一点,如下所示:

      Widget build(BuildContext context) {
      return MaterialApp(
        home: Container(
          decoration: BoxDecoration(
              image: DecorationImage(
                  image: AssetImage("path/image.format"), fit: BoxFit.cover)),
          child: Scaffold(
            backgroundColor: Colors.transparent,
            
            ),
          ),
        ),
      );
      }
      

      无论您在此布局上绘制什么,都应该具有透明的背景颜色,否则它将不可见。希望它有效!

      【讨论】:

      猜你喜欢
      • 2018-02-21
      • 1970-01-01
      • 2017-01-05
      • 2015-09-10
      • 1970-01-01
      • 2020-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多