简体   繁体   中英

How to create table like ui in flutter

I am new in flutter just want to know using which widget i can create ui like given in below image.

在此处输入图像描述

Thanks,

Flutter has a Table class for this (but you can also do it using simple Row + Column combo).

Here's the link to the Table docs: Flutter Table

Here's a simple example to get you started:

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 has a DataTable class that can be used like this.


在此处输入图像描述

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\$')),
         ])
    ])

For advanced configurations, you can check these packages which will help you extend some features like pagination, selection etc.

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

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

Use Table widget !
That widget allows you to build a table. Code : Table(children : <TableRow>[])

Example :

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'),
    ]),
  ]
),

Output: 在此处输入图片说明

Note: TableRow is a horizontal group of cells in a Table.
Add many TableRow that you want to

You can learn more about Table by watching this official video or by visiting flutter.dev

You can use DataTable widget. This sample shows how to display a DataTable with three columns: name, age, and role. enter image description here

All the above solutions work well for static data. But if you are looking at using dynamic data then, the solution below is for you.

  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,
                        ),
                      ),
                    ),
                  ]);
            },
          )
        ],
      )

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM