[Flutter] [widget] Table widget


insert image description here

foreword

Table widget, in fact, is rarely used, and when necessary, check the widget


1. What is Table?

Form widget, but not as powerful as excel.

project Value
computer $1600
cell phone $12
catheter $1

2. Use steps

project Value
Table a form
TableRow one line
TableCell a cell

1. Table basic use

The code is as follows (example):

    return Scaffold(
      appBar: AppBar(
        // Here we take the value from the MyHomePage object that was created by
        // the App.build method, and use it to set our appbar title.
        title: Text(widget.title),
      ),
      body: Table(
        children: [
          TableRow(children: [
            TableCell(
              child: Text(' i am one '),
            ),
            TableCell(
              child: Text(' i am one '),
            ),
            TableCell(
              child: Text(' i am one '),
            ),
            TableCell(
              child: Text(' i am one '),
            )
          ]),
          TableRow(children: [
            TableCell(
              child: Text(' i am one '),
            ),
            TableCell(
              child: Text(' i am one '),
            ),
            TableCell(
              child: Text(' i am one '),
            ),
            TableCell(
              child: Text(' i am one '),
            )
          ])
        ],
      ),

insert image description here

2. Width

Several forms of width setting

  • FlexColumnWidth
 //columnWidths 单元格的宽,map 哪列 :宽度
  columnWidths: {
    
    
    0: FlexColumnWidth(20),
    1: FlexColumnWidth(10),
    2: FlexColumnWidth(10),
    3: FlexColumnWidth(10)
  },

As shown in the picture:
insert image description here

  • IntrinsicColumnWidth: Take the widest cell as the width of the column
   columnWidths: {
    
    
     0: IntrinsicColumnWidth(),
     1: FlexColumnWidth(10),
     2: FlexColumnWidth(20),
     3: FlexColumnWidth(10)
   },

As shown in the picture:
insert image description here

  • MaxColumnWidth: Use the widest cell as the width of the column, set the maximum value, if it exceeds this maximum value, it will also take the maximum value
        columnWidths: const {
    
    
          0: MaxColumnWidth(FlexColumnWidth(20), FlexColumnWidth(200)),
          1: FlexColumnWidth(10),
          2: FlexColumnWidth(10),
          3: FlexColumnWidth(10)
        },

3. Set the border

The code is as follows (example):

    return Scaffold(
      appBar: AppBar(
        // Here we take the value from the MyHomePage object that was created by
        // the App.build method, and use it to set our appbar title.
        title: Text(widget.title),
      ),
      body: Table(
        border: TableBorder(
          //添加上行左右的网格线
            top: BorderSide(color: Colors.red),
            left: BorderSide(color: Colors.red),
            bottom: BorderSide(color: Colors.red),
            right: BorderSide(color: Colors.red),
            //水平线
            horizontalInside: BorderSide(color: Colors.red),
            //垂直方向
            verticalInside: BorderSide(color: Colors.red)),
        //columnWidths 单元格的宽,map 哪列 :宽度
        columnWidths: {
    
    
          0: FlexColumnWidth(20),
          1: FlexColumnWidth(10),
          2: FlexColumnWidth(10),
          3: FlexColumnWidth(10)
        },
        children: [
          TableRow(children: [
            TableCell(
              child: Text(' i am one '),
            ),
            TableCell(
              child: Text(' i am one '),
            ),
            TableCell(
              child: Text(' i am one '),
            ),
            TableCell(
              child: Text(' i am one '),
            )
          ]),
          TableRow(children: [
            TableCell(
              child: Text(' i am one '),
            ),
            TableCell(
              child: Text(' i am one '),
            ),
            TableCell(
              child: Text(' i am one '),
            ),
            TableCell(
              child: Text(' i am one '),
            )
          ])
        ],
      ),
// This trailing comma makes auto-formatting nicer for build methods.
    );

The data requested by the url network used here.

4. TableCell set cell format widget and other settings

  • The content of the unit can be set as a widget
  TableCell(
       child: Card(child: Text(' i am onedfasdfadsfasd ')),
     ),

insert image description here

  • Set the alignment in the vertical direction:
    TableCell(
         verticalAlignment: TableCellVerticalAlignment.middle,
        child: Text(' i am onedfasdfadsfasd '),
      ),

insert image description here

  • set background color
 TableRow(
              decoration: BoxDecoration(
                  color: Colors.green,
                  borderRadius: BorderRadius.all(Radius.circular(15))),
              children: [
                TableCell(
                  verticalAlignment: TableCellVerticalAlignment.bottom,
                  child: Text(' i am onedfasdfadsfasd '),
                ),

insert image description here


Summarize

Welcome to pay attention, leave a message, consult and communicate!
insert image description here

Guess you like

Origin blog.csdn.net/weixin_43444734/article/details/127957579
Recommended