Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
341 views
in Technique[技术] by (71.8m points)

dynamic - Flutter-动态创建表(Flutter - Creating tables dynamically)

I'm trying to dynamically create tables using Dart and Flutter.

(我正在尝试使用Dart和Flutter动态创建表。)

Something like this The number of table rows will change depending on the the JSON file passed in.

(事情是这样的表中的行数将根据传入的JSON文件更改。)

I've read through and done all of the Flutter tutorials I can get my hands on and read through the Documentation on the Table and ListBuilder classes, but none of them quite accomplish what I'm trying to do, because the examples either only dynamically create single ListItems or all the data and/or Widgets are hard-coded.

(我已经阅读并完成了所有Flutter教程,可以动手操作并阅读Table和ListBuilder类上的文档,但是它们都不能完全完成我想做的事情,因为这些示例只是动态地创建单个ListItem或所有数据和/或小部件都经过硬编码。)

I've also tried doing this by doing: Table dynamicTable = new Table();

(我也尝试过这样做: Table dynamicTable = new Table();)

then dynamically adding children Widgets with

(然后动态添加子控件)

dynamicTable.add(TableRow(
children: [
    Text("test1"),
    Text("test2"),
    Text("test3"),
]
));

But I get an error saying "Cannot add to an unmodifiable list".

(但是我收到一个错误消息,说“无法添加到不可修改的列表”。)

Any tips on how to accomplish this would be greatly appreciated.

(任何有关如何完成此操作的技巧将不胜感激。)

  ask by kaden_shaihd translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It's pretty easy, actually!

(实际上,这很容易!)

All you have to do is, make a list of TableRows, and put that in the children parameter of your table.

(您所要做的就是列出TableRows,并将其放入表的children参数中。)

For example

(例如)

List<TableRow> tableRows = [];
// dynamically make TableRows and add them to the list

And then you can just do this:

(然后,您可以执行以下操作:)

Table(
   children: tableRows,
   // other stuff
)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...