skip to Main Content

enter image description hereCan i ask you some question about flutter list? I have a list ([Red,White,Yellow],[40,41,42],[Small,Big]), and I want to combine to DataTable DataRow DataCell (TextFormField):

Red,40,Small

White,40,Small

Yellow,40,Small

Red,41,Small

White,41,Small

Yellow,41,Small

Red,42,Small

White,42,Small

Yellow,42,Small

Red,40,Big

White,40,Big

Yellow,40,Big

Red,41,Big

White,41,Big

Yellow,41,Big

Red,42,Big

White,42,Big

Yellow,42,Big

2

Answers


  1. This is an example on how you use nested loop to get every combination of that three categories.

    final colors = ['Red', 'White', 'Yellow'];
    final sizes = [40, 41, 42];
    final types = ['Small', 'Big'];
    
    final records = <(String, int, String)>[];
    
    for (final color in colors) {
      for (final size in sizes) {
        for (final type in types) {
          records.add((color, size, type));
        }
      }
    }
    
    for (final record in records) {
      print(record);
    }
    

    Output:

    (Red, 40, Small)
    (Red, 40, Big)
    (Red, 41, Small)
    (Red, 41, Big)
    (Red, 42, Small)
    (Red, 42, Big)
    (White, 40, Small)
    (White, 40, Big)
    (White, 41, Small)
    (White, 41, Big)
    (White, 42, Small)
    (White, 42, Big)
    (Yellow, 40, Small)
    (Yellow, 40, Big)
    (Yellow, 41, Small)
    (Yellow, 41, Big)
    (Yellow, 42, Small)
    (Yellow, 42, Big)
    

    To access the color of the third element, for example, you can do this:

    print(records[2].$1) // prints 'Red'
    
    Login or Signup to reply.
    1. Define your data.
    List<String> data = [
     'Red,40,Small',
     'White,40,Small',
     'Yellow,40,Small',
     //... add all your data here
    ];
    
    1. Create a function to convert each string into a DataRow. Each DataRow will contain DataCell widgets for each value in the string. In your case, you want to use TextFormField widgets in the DataCell widgets. This allows the user to edit the values in the table.
    List<DataRow> _createRows() {
     return data.map((row) {
       var cells = row.split(',');
       return DataRow(cells: [
         DataCell(TextFormField(initialValue: cells[0])),
         DataCell(TextFormField(initialValue: cells[1])),
         DataCell(TextFormField(initialValue: cells[2])),
       ]);
     }).toList();
    }
    
    1. Create a function to create the columns for the DataTable.
    List<DataColumn> _createColumns() {
     return [
       DataColumn(label: Text('Color')),
       DataColumn(label: Text('Size')),
       DataColumn(label: Text('Category')),
     ];
    }
    
    1. Finally, create the DataTable in the build method of your widget.
    @override
    Widget build(BuildContext context) {
     return DataTable(
       columns: _createColumns(),
       rows: _createRows(),
     );
    }
    

    This will create a DataTable where each row is editable. The user can edit the values in the table using the TextFormField widgets.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search