Home > Blockchain >  I have a data table where I need to add datarows based on text input
I have a data table where I need to add datarows based on text input

Time:10-21

I have written a hard-coded 50 rows. Now I need to optimize the code.

As per the expected output, the first row should have a text input that will accept only int values and say that I enter 10, then 10 rows should be created dynamically.

https://github.com/AafreenKhan040896/skillsTable/blob/main/lib/main.dart

this is the widget that I am trying to create.

CodePudding user response:

I have optimized a little your SkillTable class:

class SkillsTable extends StatelessWidget {
  const SkillsTable({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    // ignore: prefer_typing_uninitialized_variables
    var value;
    return Scaffold(
        appBar:
            AppBar(centerTitle: true, title: const Text('Skills & Experince')),
        body: Row(
          children: [
            Container(
              width: (MediaQuery.of(context).size.width / 2) - 30,
              margin: const EdgeInsets.all(15.0),
              padding: const EdgeInsets.all(3.0),
              decoration:
                  BoxDecoration(border: Border.all(color: Colors.black)),
              child: const Text("Hello"),
            ),
            Container(
              width: (MediaQuery.of(context).size.width / 2) - 30,
              margin: const EdgeInsets.all(15.0),
              padding: const EdgeInsets.all(3.0),
              child: SingleChildScrollView(
                scrollDirection: Axis.vertical,
                child: SingleChildScrollView(
                    scrollDirection: Axis.horizontal,
                    child: DataTable(
                      showBottomBorder: true,
                      dividerThickness: 2.0,
                      headingRowColor: MaterialStateColor.resolveWith(
                          (states) => Colors.cyanAccent),
                      headingRowHeight: 70,
                      dataRowHeight: 50,
                      columnSpacing:
                          (MediaQuery.of(context).size.width / 67) * 3,
                      columns: [
                        _getDataColumn('Skills'),
                        _getDataColumn('Months'),
                        _getDataColumn('SelfScore'),
                        _getDataColumn('RightScore'),
                        _getDataColumn('Notes'),
                      ],
                      rows: _getListRows(50, value),
                    ),
                  ),
                ),
              ),
            
          ],
        ));
  }

  DataColumn _getDataColumn(String name) {
    return DataColumn(
                          label: Text(
                        name,
                        style: TextStyle(fontWeight: FontWeight.bold),
                      ));
  }

  DataRow _dataRowEmpty() {
    return DataRow(cells: <DataCell>[
                        DataCell(Text('')),
                        DataCell(Text('')),
                        DataCell(Text('')),
                        DataCell(Text('')),
                        DataCell(Text('')),
                      ]);
  }

  DataRow _dataRow(value) {
    return DataRow(cells: <DataCell>[
                        const DataCell(MyDropDownWidget()),
                        DataCell(
                            TextFormField(
                              initialValue: '$value',
                              keyboardType: TextInputType.number,
                              inputFormatters: <TextInputFormatter>[
                                FilteringTextInputFormatter.digitsOnly
                              ],
                            ),
                            showEditIcon: true),
                        DataCell(
                            TextFormField(
                              initialValue: '$value',
                              keyboardType: TextInputType.number,
                              inputFormatters: <TextInputFormatter>[
                                FilteringTextInputFormatter.digitsOnly
                              ],
                            ),
                            showEditIcon: true),
                        const DataCell(Text('')),
                        DataCell(
                            TextFormField(
                              initialValue: '$value',
                              keyboardType: TextInputType.multiline,
                              maxLength: 100,
                            ),
                            showEditIcon: true),
                      ]);
  }

  List<DataRow> _getListRows(int rows, dynamic value) {
    List<DataRow> list = [];
    list.add(_dataRowEmpty());
    for (int i = 0; i < rows; i  ){
      list.add(_dataRow(value));
    }
    return list;
  }
}
  • Related