Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
296 views
in Technique[技术] by (71.8m points)

dart - How can i create a table flutter?

I have Three Lists and I have to Generate the Table

These Lists From User Input

px =[0.1,0.2,0.3,........];
x=  [0,1,2,........];

And this 3rd List is the Multiplication of 1st 2nd List

Mutiply=[0,0.2,0.6,......];

I Want to generate the Table

Example Pic Attached of Required final output

Length of List is from user input So (How can I generate the Table)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Easy approach will be by creating a model class for function, like

class F {
  final double x;
  final double px;
  final double multiply;
  F({
    required this.x,
    required this.px,
  }) : multiply = x * px;
}

On state-level there will a List<F> data and it will increase on user input.

For dynamic increase in the DataRow, we can map it and pass on rows:

          data .map(
                    (f) => DataRow(
                      cells: [
                        DataCell(Text(f.x.toString())),
                        DataCell(Text(f.px.toString())),
                        DataCell(Text(
                          f.multiply.toStringAsFixed(3),
                        )),
                      ],
                    ),
                  )
                  .toList()

Full Widget


class UserManager extends StatefulWidget {
  const UserManager({Key? key}) : super(key: key);

  @override
  _UserManagerState createState() => _UserManagerState();
}

class _UserManagerState extends State<UserManager> {
  List<F> data = [];

  final xController = TextEditingController();
  final pxController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: [
          TextField(
            controller: xController,
            decoration: const InputDecoration(hintText: "x"),
          ),
          TextField(
            controller: pxController,
            decoration: const InputDecoration(hintText: "px"),
          ),
          ElevatedButton(
              onPressed: () {
                // get Text and parse to double
                final xVal = xController.text.toString();
                final pxVal = pxController.text.toString();

                double? x = double.tryParse(xVal);
                double? px = double.tryParse(pxVal);

                if (x == null || px == null) {
                  /// allow only number
                  return;
                }
                // else add to data list
                setState(() {
                  data.add(F(x: x, px: px));
                });

                /// clear
                xController.clear();
                pxController.clear();
              },
              child: Text("ADD")),
          DataTable(
              headingRowColor:
                  MaterialStateProperty.all(Colors.blueGrey.withOpacity(.5)),
              showBottomBorder: true,
              columns: const [
                DataColumn(
                  label: Text("x"),
                ),
                DataColumn(
                  label: Text("p(x)"),
                ),
                DataColumn(
                  label: Text("x*p(x)"),
                ),
              ],
              rows: data
                  .map(
                    (f) => DataRow(
                      cells: [
                        DataCell(Text(f.x.toString())),
                        DataCell(Text(f.px.toString())),
                        DataCell(Text(
                          f.multiply.toStringAsFixed(3),
                        )),
                      ],
                    ),
                  )
                  .toList()),
        ],
      ),
    );
  }
}


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...