gpt4 book ai didi

嵌套行/列中的 Flutter 扩展图表

转载 作者:IT王子 更新时间:2023-10-29 06:52:15 24 4
gpt4 key购买 nike

我正在尝试从 charts_flutter package 获取图表展开以填充可用空间,当它位于列内的行内时。我花了几个小时试图做到这一点,但我一定遗漏了一些东西!这在视觉上是我想要实现的目标:

enter image description here

这是一个最小的工作示例。我一定已经尝试了一百种扩展、灵活、主轴尺寸、交叉轴对齐等组合,但我似乎无法找到正确的组合。目前底部溢出无限像素,对象在布局期间被赋予无限大小。有 this open issue ,不知道有没有关系,也没有答案。我只需要帮助处理 runApp 中的一些代码:

import 'package:charts_flutter/flutter.dart' as charts;
import 'package:flutter/material.dart';

void main() => runApp(

// HOW DO I LAY THIS OUT?
Column(
children: <Widget>[
Container(height: 200, color: Colors.orange),
Row(
textDirection: TextDirection.ltr,
children: <Widget>[
Container(width: 200,color: Colors.orange),
Expanded(child: SimpleTimeSeriesChart.withSampleData())
],
)
],
)
);

class SimpleTimeSeriesChart extends StatelessWidget {
final List<charts.Series> seriesList;
final bool animate;

SimpleTimeSeriesChart(this.seriesList, {this.animate});

factory SimpleTimeSeriesChart.withSampleData() {
return new SimpleTimeSeriesChart(
_createSampleData(),
animate: false,
);
}

@override
Widget build(BuildContext context) {
return new charts.TimeSeriesChart(
seriesList,
animate: animate,
dateTimeFactory: const charts.LocalDateTimeFactory(),
);
}

/// Create one series with sample hard coded data.
static List<charts.Series<TimeSeriesSales, DateTime>> _createSampleData() {
final data = [
new TimeSeriesSales(new DateTime(2017, 9, 19), 5),
new TimeSeriesSales(new DateTime(2017, 9, 26), 25),
new TimeSeriesSales(new DateTime(2017, 10, 3), 100),
new TimeSeriesSales(new DateTime(2017, 10, 10), 75),
];

return [
new charts.Series<TimeSeriesSales, DateTime>(
id: 'Sales',
colorFn: (_, __) => charts.MaterialPalette.blue.shadeDefault,
domainFn: (TimeSeriesSales sales, _) => sales.time,
measureFn: (TimeSeriesSales sales, _) => sales.sales,
data: data,
)
];
}
}

class TimeSeriesSales {
final DateTime time;
final int sales;

TimeSeriesSales(this.time, this.sales);
}

最佳答案

**在第 8 步查看原因 - 向下滚动👇 **

问题

I'm trying to get a chart from the charts_flutter package to expand to fill available space

错误证据

object was given an infinite size during layout

解决方案

用扩展包住行

Column(children: <Widget>[Expanded(child: Row())])

截图

enter image description here

注意事项:

  • 😎,感谢提供最少的代码

完整的工作代码

import 'package:charts_flutter/flutter.dart' as charts;
import 'package:flutter/material.dart';

void main() => runApp(

// HOW DO I LAY THIS OUT?
Column(
children: <Widget>[
Container(height: 200, color: Colors.orange),
Expanded(
child: Row(
textDirection: TextDirection.ltr,
children: <Widget>[
Container(width: 200, color: Colors.green),
Expanded(child: SimpleTimeSeriesChart.withSampleData()),
],
),
)
],
));

class SimpleTimeSeriesChart extends StatelessWidget {
final List<charts.Series> seriesList;
final bool animate;

SimpleTimeSeriesChart(this.seriesList, {this.animate});

factory SimpleTimeSeriesChart.withSampleData() {
return new SimpleTimeSeriesChart(
_createSampleData(),
animate: false,
);
}

@override
Widget build(BuildContext context) {
return new charts.TimeSeriesChart(
seriesList,
animate: animate,
dateTimeFactory: const charts.LocalDateTimeFactory(),
);
}

/// Create one series with sample hard coded data.
static List<charts.Series<TimeSeriesSales, DateTime>> _createSampleData() {
final data = [
new TimeSeriesSales(new DateTime(2017, 9, 19), 5),
new TimeSeriesSales(new DateTime(2017, 9, 26), 25),
new TimeSeriesSales(new DateTime(2017, 10, 3), 100),
new TimeSeriesSales(new DateTime(2017, 10, 10), 75),
];

return [
new charts.Series<TimeSeriesSales, DateTime>(
id: 'Sales',
colorFn: (_, __) => charts.MaterialPalette.blue.shadeDefault,
domainFn: (TimeSeriesSales sales, _) => sales.time,
measureFn: (TimeSeriesSales sales, _) => sales.sales,
data: data,
)
];
}
}

class TimeSeriesSales {
final DateTime time;
final int sales;

TimeSeriesSales(this.time, this.sales);
}

额外:如何读取错误(最后附上完整的错误转储)

第一步:定位起点

I/flutter ( 4228): ══╡ EXCEPTION CAUGHT BY RENDERING LIBRARY ╞═════════════════════════════════════════════════════════

第二步:定位结束

I/flutter ( 4228): ════════════════════════════════════════════════════════════════════════════════════════════════════

第三步:读取错误

用你的眼睛扫描错误你会看到一些像

这样的短语
I/flutter ( 4228):     constraints: BoxConstraints(w=320.0, h=480.0)

I/flutter ( 4228): The following assertion was thrown during performLayout():

I/flutter ( 4228): Summary 1: RenderCustomMultiChildLayoutBox object was given an infinite size during layout.

第 4 步:确定什么是类型

这是一个框约束错误

第 5 步:如何知道是哪个小部件引发了错误

接近尾声它会给你来源:

I/flutter ( 4228):   creator: CustomMultiChildLayout ← TimeSeriesChart ← SimpleTimeSeriesChart ← Expanded ← Row ← Column
I/flutter ( 4228): ← [root]
  • 所以它在 TimeSeriesChart 里面

第 6 步:缺少什么

接近尾声它会给你缺少的约束

I/flutter ( 4228):   constraints: BoxConstraints(w=120.0, 0.0<=h<=Infinity)
  • 注意:0.0<=h<=Infinity

  • 高度不能是无穷大

第七步:

  • SizedBox 扭曲源代码以确保它是什么引发了错误,看看它是否解决了问题

  • SizedBox

    A box with a specified size.

    If given a child, this widget forces its child to have a specific width and/or height (assuming values are permitted by this widget's parent). If either the width or height is null, this widget will size itself to match the child's size in that dimension.

第 8 步:

  1. TimeSeriesChart(child) 向 Raw(parent) 询问高度

  2. Raw(child) 向 Column(parent) 询问高度

  3. 专栏不知道

  4. 为什么 Column 不知道,因为默认行为是它会让 children 自己决定自己的高度

  5. 但我们可以告诉 Column children 不知道,所以将他们扩展到可用空间

错误转储

I/flutter ( 4228): ══╡ EXCEPTION CAUGHT BY RENDERING LIBRARY ╞═════════════════════════════════════════════════════════
I/flutter ( 4228): The following assertion was thrown during performLayout():
I/flutter ( 4228): FlutterError contained multiple error summaries.
I/flutter ( 4228): All FlutterError objects should have only a single short (one line) summary description of the
I/flutter ( 4228): problem that was detected.
I/flutter ( 4228): Malformed FlutterError:
I/flutter ( 4228): RenderCustomMultiChildLayoutBox object was given an infinite size during layout.
I/flutter ( 4228): This probably means that it is a render object that tries to be as big as possible, but it was put
I/flutter ( 4228): inside another render object that allows its children to pick their own size.
I/flutter ( 4228): RenderCustomMultiChildLayoutBox object was given an infinite size during layout.
I/flutter ( 4228): This probably means that it is a render object that tries to be as big as possible, but it was put
I/flutter ( 4228): inside another render object that allows its children to pick their own size.
I/flutter ( 4228): The nearest ancestor providing an unbounded height constraint is: RenderFlex#d29e7 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE:
I/flutter ( 4228): creator: Column ← [root]
I/flutter ( 4228): parentData: <none>
I/flutter ( 4228): constraints: BoxConstraints(w=320.0, h=480.0)
I/flutter ( 4228): size: MISSING
I/flutter ( 4228): direction: vertical
I/flutter ( 4228): mainAxisAlignment: start
I/flutter ( 4228): mainAxisSize: max
I/flutter ( 4228): crossAxisAlignment: center
I/flutter ( 4228): verticalDirection: down
I/flutter ( 4228): The constraints that applied to the RenderCustomMultiChildLayoutBox were:
I/flutter ( 4228): BoxConstraints(w=120.0, 0.0<=h<=Infinity)
I/flutter ( 4228): The exact size it was given was:
I/flutter ( 4228): Size(120.0, Infinity)
I/flutter ( 4228): See https://flutter.dev/docs/development/ui/layout/box-constraints for more information.
I/flutter ( 4228):
I/flutter ( 4228): The malformed error has 2 summaries.
I/flutter ( 4228): Summary 1: RenderCustomMultiChildLayoutBox object was given an infinite size during layout.
I/flutter ( 4228): Summary 2: RenderCustomMultiChildLayoutBox object was given an infinite size during layout.
I/flutter ( 4228):
I/flutter ( 4228): This error should still help you solve your problem, however please also report this malformed error
I/flutter ( 4228): in the framework by filing a bug on GitHub:
I/flutter ( 4228): https://github.com/flutter/flutter/issues/new?template=BUG.md
I/flutter ( 4228):
I/flutter ( 4228): When the exception was thrown, this was the stack:
I/flutter ( 4228): #0 new FlutterError.fromParts.<anonymous closure> (package:flutter/src/foundation/assertions.dart:540:9)
I/flutter ( 4228): #1 new FlutterError.fromParts (package:flutter/src/foundation/assertions.dart:543:6)
I/flutter ( 4228): #2 RenderBox.debugAssertDoesMeetConstraints.<anonymous closure> (package:flutter/src/rendering/box.dart:1966:28)
I/flutter ( 4228): #3 RenderBox.debugAssertDoesMeetConstraints (package:flutter/src/rendering/box.dart:2029:6)
I/flutter ( 4228): #4 RenderBox.size=.<anonymous closure> (package:flutter/src/rendering/box.dart:1740:7)
I/flutter ( 4228): #5 RenderBox.size= (package:flutter/src/rendering/box.dart:1742:6)
I/flutter ( 4228): #6 RenderCustomMultiChildLayoutBox.performLayout (package:flutter/src/rendering/custom_layout.dart:355:5)
I/flutter ( 4228): #7 RenderObject.layout (package:flutter/src/rendering/object.dart:1619:7)
I/flutter ( 4228): #8 RenderFlex.performLayout (package:flutter/src/rendering/flex.dart:805:17)
I/flutter ( 4228): #9 RenderObject.layout (package:flutter/src/rendering/object.dart:1619:7)
I/flutter ( 4228): #10 RenderFlex.performLayout (package:flutter/src/rendering/flex.dart:743:15)
I/flutter ( 4228): #11 RenderObject.layout (package:flutter/src/rendering/object.dart:1619:7)
I/flutter ( 4228): #12 RenderView.performLayout (package:flutter/src/rendering/view.dart:151:13)
I/flutter ( 4228): #13 RenderObject._layoutWithoutResize (package:flutter/src/rendering/object.dart:1496:7)
I/flutter ( 4228): #14 PipelineOwner.flushLayout (package:flutter/src/rendering/object.dart:765:18)
I/flutter ( 4228): #15 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding.drawFrame (package:flutter/src/rendering/binding.dart:346:19)
I/flutter ( 4228): #16 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding&WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:701:13)
I/flutter ( 4228): #17 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding&PaintingBinding&SemanticsBinding&RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:285:5)I/flutter ( 4228): #18 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1016:15)
I/flutter ( 4228): #19 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:958:9)
I/flutter ( 4228): #20 _WidgetsFlutterBinding&BindingBase&GestureBinding&ServicesBinding&SchedulerBinding.scheduleWarmUpFrame.<anonymous closure> (package:flutter/src/scheduler/binding.dart:784:7)
I/flutter ( 4228): #29 _Timer._runTimers (dart:isolate-patch/timer_impl.dart:382:19)
I/flutter ( 4228): #30 _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:416:5)
I/flutter ( 4228): #31 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:172:12)
I/flutter ( 4228): (elided 8 frames from package dart:async and package dart:async-patch)
I/flutter ( 4228):
I/flutter ( 4228): The following RenderObject was being processed when the exception was fired: RenderCustomMultiChildLayoutBox#e4ae0 relayoutBoundary=up2 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE:
I/flutter ( 4228): creator: CustomMultiChildLayout ← TimeSeriesChart ← SimpleTimeSeriesChart ← Expanded ← Row ← Column
I/flutter ( 4228): ← [root]
I/flutter ( 4228): parentData: offset=Offset(0.0, 0.0); flex=1; fit=FlexFit.tight (can use size)
I/flutter ( 4228): constraints: BoxConstraints(w=120.0, 0.0<=h<=Infinity)
I/flutter ( 4228): size: Size(120.0, Infinity)
I/flutter ( 4228): This RenderObject had the following descendants (showing up to depth 5):
I/flutter ( 4228): child 1: RenderSemanticsGestureHandler#2034c NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
I/flutter ( 4228): child: RenderPointerListener#aef29 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
I/flutter ( 4228): child: ChartContainerRenderObject<DateTime>#b28fc NEEDS-LAYOUT NEEDS-PAINT
I/flutter ( 4228): ════════════════════════════════════════════════════════════════════════════════════════════════════

关于第 8 步的更多内容

  • 现在我们知道错误来自 TimeSeriesChart 小部件,因为它需要高度
  • 但如果它没有高度,它会询问它的父级,行

  • 但是行没有固定的高度,它会询问 child

  • 但是 child 没有固定的高度,它会询问行父

  • 但是父级没有给它的子级(行)一个固定的高度

关于嵌套行/列中的 Flutter 扩展图表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57655660/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com