0

I want to load data table after application load. But when application load automatically data table also load. I need that data table load with button click event.

This is the primary widget.

Widget build

Container(
    child: OutlineButton(
        child: Text('UPDATE',style: TextStyle(color: Colors.white)),
        color: Colors.orange,
        onPressed: () {abc();},
      ),
    ),
    itemCount==1 ? Container(
      padding: EdgeInsets.all(15.0),
      child: SingleChildScrollView(
        child: Column(
          children: [
            activityHistory()
          ],
        ),
      ),
    ):

I add that Data table to list view builder. I want to call that from button click.

Called Datatable Widget

    return ListView.builder(
        shrinkWrap: true,
        physics: NeverScrollableScrollPhysics(),
        itemBuilder: (context, index) {
          if (itemCount != 0) {
            print("OK");
            return Container(
                child:DataTable(

                  sortAscending: sort,
                  sortColumnIndex: 0,
                  columns: [
                    DataColumn(
                        label: Text("Code", style: TextStyle(fontSize: 16)),
                        numeric: false,
                        onSort: (columnIndex, ascending) {
                          setState(() {
                            sort = !sort;
                          });
                          onSortColum(columnIndex, ascending);
                        }),
                    DataColumn(
                      label: Text("QTY", style: TextStyle(fontSize: 16)),
                      numeric: false,
                    ),
                  ],
                  rows: avengers
                      .map(
                        (avenger) => DataRow(
                        selected: selectedAvengers.contains(avenger),
                        cells: [
                          DataCell(
                            Text(avenger.name),
                            onTap: () {
                              print('Selected ${avenger.name}');
                            },
                          ),
                          DataCell(
                            Text(avenger.weapon),
                          ),
                        ]),
                  ).toList(),
                ),
            );
          }else {
            print("SizedBox");
            return SizedBox();
          }
        }
    );}

Now that Data table load with the application . But i want it load after application load with button click .

How can i load that table after application load and button click event ?

1 Answer 1

1

Initially button is NOT clicked - show SizedBox. When button is clicked we pass callback function to onPressed and show ListView.

setState(() {
   clicked = true;
});

Something like this.

class _MyHomePageState extends State<MyHomePage> {
  bool clicked = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Load list on click"),
      ),
      body: activityHistory(),
      floatingActionButton: FloatingActionButton(
        onPressed: (){
          setState(() {
            clicked = true;
          });
        },
        child: Icon(Icons.play_arrow),
      ),
    );
  }
  
  Widget activityHistory() {
    if (!clicked) return SizedBox();
    
    return ListView(
      children: [
        Text("Item 1"),
        Text("Item 2"),
        Text("Item 3"),
        Text("Item 4"),
        Text("Item 5"),
      ],
    ); 
  }
}
1
  • Thanks for the answer. I fixed that issue with your solution.
    – sanjeewa
    Dec 4, 2020 at 10:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.