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

我试图扩展的图表小部件扩展了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,
        ),
      ],
    ),
  ),
);

结果如下:(为简洁起见,未显示ChartPainter代码)

ChartPainter.paint(canvas, size)的内部有一个print()打印尺寸.

打印(###大小:paint():通过的大小=${Size});

The result from the paint->print above is:

I/Ffltter(4187):#Size:Paint():Passed Size=Size(340.0,0.0)

打印和图像一起显示,行级别的宽度扩展被传递到CustomPainter.print(canvas, size)(宽度=340.0),但列的高度扩展没有传递到自定义绘制打印(高度=0.0).虽然结果显示该行确实获得了它的扩展高度,但如果没有在该行内部传递到CustomPainter-0高度,则会收到.

我需要改变什么才能实现身高的增加呢?

谢谢

推荐答案

以下是针对您看到的问题的简化测试用例.解决办法是给你的Row分打crossAxisAlignment分(满分CrossAxisAlignment.stretch分).否则,它将try 确定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'),
          ],
        )
    ),
  ));
}

Flutter相关问答推荐

Flutter:带有表单的SpeechToTextprovider

Riverpod生成器和生成util类

如何防止alert 对话框在收到通知时出现在某个flutter页面中

如何更新文本字段的基础上的选定下拉菜单?

如何等待抖动S定时器.定期取消?

SupabaseFlutter 集成问题:无法更新行

你能go 掉Flutter 小部件测试中的样板吗?

Flutter OpenID Connect无效参数:redirect_uri using Keycloak

如何在Flutter 中共享选定文本

上传的图像不能在FireBase中预览.S档案类型为空白,摆动

Flutter:在 pubspec.yaml 中找不到assets资源

Flutter Dismissible 不会在 startToEnd 滑动时被关闭

模块是使用不兼容的 kotlin 版本编译的.其元数据的二进制版本是1.8.0,预期版本是1.6

仅在获取(读取)、Provider 时,Firestore 中的数据为空

无法在 ListView (Flutter) 中限制 Card 的最大宽度

无状态小部件被奇怪地重建

有任何方法可以在应用程序移动背景恢复时显示 appopenAd

Flutter 本地通知在最新版本中不起作用

解密 Modulr 安全令牌

未处理的异常:FileSystemException:无法打开文件,路径 = 'assets/res.zip'(操作系统错误:没有这样的文件或目录,errno = 2)