如何实现widget在垂直(高度)和水平(宽度)方向上的扩展

2024-03-30

下面的代码列出了一个图表,我需要在其中实现图表在垂直(高度)和水平(宽度)方向上的扩展。建议的方法(例如https://docs.flutter.io/flutter/widgets/Row-class.html https://docs.flutter.io/flutter/widgets/Row-class.html)是使用Expanded in Row or Column.

我试图扩展的图表小部件扩展CustomPaint,没有孩子,一切都是用CustomPainter在画布上,在CustomPainter.paint(canvas, size).

这段代码

return new Scaffold(
  appBar: new AppBar(
    title: new Text(widget.title),
  ),
  body: new Center(    
    child: new Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
        new Text(
          'vvvvvvvv:',
        ),
        new RaisedButton(
          color: Colors.green,
          onPressed: _chartStateChanger,
        ),
        new Text(
          'vvvvvvvv:',
        ),    
        new Expanded( // Expanded in Column, no expansion vertically 
          child: new Row(
            children: [
              new Text('>>>'),    
              new Expanded(// Expanded in Row, expands horizontally
                child: new Chart(  // extends CustomPaint
                  // size: chartLogicalSize,
                  painter: new ChartPainter( // extends CustomPainter
                    chartData: _chartData,
                    chartOptions: _chartOptions,
                  ),
                ),
              ),
              new Text('<<<'),
            ],
          ),  // row
        ),
        new Text('^^^^^^:'),
        new RaisedButton(
          color: Colors.green,
          onPressed: _chartStateChanger,
        ),
      ],
    ),
  ),
);

result looks like this: (code of ChartPainter is not shown for brevity) result of the above code

在 - 的里面ChartPainter.paint(canvas, size)有一个print()打印尺寸。

print(" ### 大小:paint(): 传递的大小 = ${size}");

上面的paint->print的结果是:

I/flutter ( 4187): ### 大小:paint(): 传递的大小 = Size(340.0, 0.0)

打印和图像显示,行级别的宽度扩展被传递到CustomPainter.print(canvas, size)(宽度= 340.0),但是列上的高度扩展没有传递给自定义画家打印(高度= 0.0)。尽管结果显示该行确实获得了扩展的高度,但如果没有在行内部传递给CustomPainter- 收到 0 高度。

我需要改变什么才能实现高度扩展?

Thanks


这是针对您所看到的问题的简化测试用例。解决方案是给你的Row a crossAxisAlignment of CrossAxisAlignment.stretch。否则它会尝试确定您的固有高度CustomPaint这是零,因为它没有孩子。

import 'package:flutter/material.dart';

// from https://stackoverflow.com/questions/45875334/how-to-achieve-expansion-of-a-widget-in-both-vertical-height-and-horizontal-w

class MyCustomPainter extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    // NOT using crossAxisAlignment: CrossAxisAlignment.stretch => width = 222.0, height=0.0
    // using crossAxisAlignment: CrossAxisAlignment.stretch     => width = 222.0, height=560.0
    print("width = ${size.width}, height=${size.height}");
    canvas.drawRect(Offset.zero & size, new Paint()..color = Colors.blue);
  }

  @override
  bool shouldRepaint(MyCustomPainter other) => false;
}
void main() {
  runApp(new MaterialApp(
    home: new Scaffold(
        body: new Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            new Text('Above Paint'),
            // Expanded - because we are in Column, expand the
            //            contained row's height
            new Expanded(
              child: new Row(
                // The crossAxisAlignment is needed to give content height > 0
                //   - we are in a Row, so crossAxis is Column, so this enforces
                //     to "stretch height".
                crossAxisAlignment: CrossAxisAlignment.stretch,
                children: <Widget>[
                  new Text('Left of Paint'),
                  // Expanded - because we are in Row, expand the
                  //           contained Painter's width
                  new Expanded(
                    child: new CustomPaint(
                      painter: new MyCustomPainter(),
                    ),
                  ),
                  new Text('Right of Paint'),
                ],
              ),
            ),
            new Text('Below Paint'),
          ],
        )
    ),
  ));
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何实现widget在垂直(高度)和水平(宽度)方向上的扩展 的相关文章

随机推荐