How to add dynamically items to the table in flutter?
Here’s a working example hacked from a flutter doc example. Click the button to add rows.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { // This widget is the root of your application. @override Widget build(BuildContext context) { return MaterialApp( title: 'Add Rows', home: MyHomePage(title: 'Add Rows'), ); } } class MyHomePage extends StatefulWidget { MyHomePage({Key key, this.title}) : super(key: key); final String title; @override _MyHomePageState createState() => _MyHomePageState(); } class _MyHomePageState extends State<MyHomePage> { List<DataRow> _rowList = [ DataRow(cells: <DataCell>[ DataCell(Text('AAAAAA')), DataCell(Text('1')), DataCell(Text('Yes')), ]), ]; void _addRow() { // Built in Flutter Method. setState(() { // This call to setState tells the Flutter framework that something has // changed in this State, which causes it to rerun the build method below. _rowList.add(DataRow(cells: <DataCell>[ DataCell(Text('BBBBBB')), DataCell(Text('2')), DataCell(Text('No')), ])); }); } @override Widget build(BuildContext context) { return Scaffold( body: Center( child: DataTable(columns: [ DataColumn(label: Text('Patch')), DataColumn(label: Text('Version')), DataColumn(label: Text('Ready')), ], rows: _rowList), ), floatingActionButton: FloatingActionButton.extended( onPressed: _addRow, label: Text('Add Row'), backgroundColor: Colors.green, ), ); } } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.