簡體   English   中英

如何在 flutter 中創建類似 ui 的表

[英]How to create table like ui in flutter

我是 flutter 的新手,只想知道使用哪個小部件我可以創建如下圖所示的用戶界面。

在此處輸入圖像描述

謝謝,

Flutter 為此提供了一個Table類(但您也可以使用簡單的 Row + Column 組合來實現)。

這是Table文檔的鏈接: Flutter Table

這是一個讓您入門的簡單示例:

Container(
        color: Colors.white,
        padding: EdgeInsets.all(20.0),
        child: Table(
          border: TableBorder.all(color: Colors.black),
          children: [
            TableRow(children: [
              Text('Cell 1'),
              Text('Cell 2'),
              Text('Cell 3'),
            ]),
            TableRow(children: [
              Text('Cell 4'),
              Text('Cell 5'),
              Text('Cell 6'),
            ])
          ],
        ),
      )

Flutter有一個DataTable class可以這樣用


在此處輸入圖像描述

DataTable(
 columns: [
          DataColumn(
            label: Text('ID'),
          ),
          DataColumn(
            label: Text('Name'),
          ),
          DataColumn(
            label: Text('Code'),
          ),
          DataColumn(
            label: Text('Quantity'),
          ),
          DataColumn(
            label: Text('Amount'),
          ),
        ], 
  rows: [
            
         DataRow(cells: [
            DataCell(Text('1')),
            DataCell(Text('Arshik')),
            DataCell(Text('5644645')),
            DataCell(Text('3')),
            DataCell(Text('265\$')),
         ])
    ])

對於高級配置,您可以檢查這些包,這將幫助您擴展一些功能,如分頁、選擇等。

https://pub.dev/packages/data.tables

https://pub.dev/packages/data.table_2

使用表格小部件
該小部件允許您構建表格。 代碼: Table(children : <TableRow>[])

例子 :

Table(
  border: TableBorder.all(), // Allows to add a border decoration around your table
  children: [ 
    TableRow(children :[
      Text('Year'),
      Text('Lang'),
      Text('Author'),
    ]),
    TableRow(children :[
      Text('2011',),
      Text('Dart'),
      Text('Lars Bak'),
    ]),
    TableRow(children :[
      Text('1996'),
      Text('Java'),
      Text('James Gosling'),
    ]),
  ]
),

輸出: 在此處輸入圖片說明

注意: TableRow是表格中的一組水平單元格。
添加許多您想要的TableRow

您可以通過觀看此官方視頻或訪問flutter.dev來了解有關Table的更多信息

您可以使用DataTable小部件。 此示例顯示如何顯示具有三列的 DataTable:名稱、年齡和角色。 在此處輸入圖片說明

以上所有解決方案都適用於 static 數據。 但是,如果您正在考慮使用動態數據,那么下面的解決方案適合您。

  Table(
        border: TableBorder(
            horizontalInside:
                BorderSide(color: scaffoldBgColor, width: 10.0)),
        children: [
   //This table row is for the table header which is static
          TableRow(children: [

            Center(
              child: Padding(
                padding: const EdgeInsets.symmetric(vertical: 10),
                child: Text(
                  "INDEX",
                  style: TextStyle(
                      fontWeight: FontWeight.bold, color: Colors.black87),
                ),
              ),
            ),
            Center(
              child: Padding(
                padding: const EdgeInsets.symmetric(vertical: 10),
                child: Text(
                  "NAME",
                  style: TextStyle(
                      fontWeight: FontWeight.bold, color: Colors.black87),
                ),
              ),
            ),
            Center(
              child: Padding(
                padding: const EdgeInsets.symmetric(vertical: 10),
                child: Text(
                  "ACTION",
                  style: TextStyle(
                      fontWeight: FontWeight.bold, color: Colors.black87),
                ),
              ),
            ),
          ]),
      // Using the spread operator to add the remaining table rows which have dynamic data
      // Be sure to use .asMap().entries.map if you want to access their indexes and objectName.map() if you have no interest in the items index.

          ...students.asMap().entries.map(
            (student) {
              return TableRow(
                  decoration: BoxDecoration(color: tertiary),
                  children: [

                    Center(
                      child: Padding(
                        padding: const EdgeInsets.symmetric(vertical: 20),
                        child: Text(
                          student.value.id.toString(),
                        ),
                      ),
                    ),
                    Center(
                      child: Padding(
                        padding: const EdgeInsets.symmetric(vertical: 20),
                        child: Text(
                          '${student.value.firstName} ${student.value.surName}',
                        ),
                      ),
                    ),
                    Center(
                      child: Padding(
                        padding: const EdgeInsets.symmetric(vertical: 10),
                        child: Checkbox(
                          materialTapTargetSize:
                              MaterialTapTargetSize.shrinkWrap,
                          onChanged: (val) {},
                          value: false,
                        ),
                      ),
                    ),
                  ]);
            },
          )
        ],
      )

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM