skip to Main Content

the table is editable and it contain of => code/codename/price/kilograms

code codename price kilograms

I tried to create a data model but it was hard to connect it to the table

2

Answers


  1. enter image description here

    import 'package:flutter/material.dart';
    
    void main() {
      runApp(const MyApp());
    }
    
    class MyApp extends StatelessWidget {
      const MyApp({super.key});
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Demo',
          theme: ThemeData(
            colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepOrange),
          ),
          home: const MyHomePage(),
        );
      }
    }
    
    class MyHomePage extends StatelessWidget {
      const MyHomePage({super.key});
    
      @override
      Widget build(BuildContext context) {
        List<MyModel> data = [
          MyModel(
            code: "code1",
            codeName: "codeName1",
            price: "price1",
            kilograms: "kilograms1",
          ),
          MyModel(
            code: "code2",
            codeName: "codeName2",
            price: "price2",
            kilograms: "kilograms2",
          ),
          MyModel(
            code: "code3",
            codeName: "codeName3",
            price: "price3",
            kilograms: "kilograms3",
          ),
          MyModel(
            code: "code4",
            codeName: "codeName4",
            price: "price4",
            kilograms: "kilograms4",
          )
        ];
        return Scaffold(
          appBar: AppBar(),
          body: Theme(
            data: Theme.of(context).copyWith(dividerColor: Colors.black),
            child: Padding(
              padding: const EdgeInsets.all(8.0),
              child: SingleChildScrollView(
                scrollDirection: Axis.vertical,
                child: SingleChildScrollView(
                  scrollDirection: Axis.horizontal,
                  child: DataTable(
                    columnSpacing: 5,
                    decoration: BoxDecoration(
                        border: Border.all(
                      width: 1,
                      color: Colors.black,
                    )),
                    columns: const <DataColumn>[
                      DataColumn(label: Text("code", textAlign: TextAlign.center)),
                      DataColumn(label: VerticalDivider()),
                      DataColumn(
                          label: Text("codename", textAlign: TextAlign.center)),
                      DataColumn(label: VerticalDivider()),
                      DataColumn(label: Text("price", textAlign: TextAlign.center)),
                      DataColumn(label: VerticalDivider()),
                      DataColumn(
                          label: Text("kilograms", textAlign: TextAlign.center)),
                    ],
                    rows: data
                        .map(
                          (i) => DataRow(cells: [
                            DataCell(Text(i.code)),
                            const DataCell(VerticalDivider()),
                            DataCell(Text(i.codeName)),
                            const DataCell(VerticalDivider()),
                            DataCell(Text(i.price)),
                            const DataCell(VerticalDivider()),
                            DataCell(Text(i.kilograms)),
                          ]),
                        )
                        .toList(),
                  ),
                ),
              ),
            ),
          ),
        );
      }
    }
    
    class MyModel {
      String code;
      String codeName;
      String price;
      String kilograms;
    
      MyModel(
          {required this.code,
          required this.codeName,
          required this.price,
          required this.kilograms});
    }

    Here is a complete example of what you want to do

    Login or Signup to reply.
  2. As explained above, every object of MyModel class represents the row of table,
    you just need to put all objects in the List which represents this entire table .

    class MyModel {
      String code;
      String codeName;
      String price;
      String kilograms;
    
      MyModel(
          {required this.code,
          required this.codeName,
          required this.price,
          required this.kilograms});
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search