2

I am having a data table with checkbox inside alert dialog box, When i click the check box, it is not checking(Checked=checked) the box. I followed this video

Here is my try out

import 'package:flutter/material.dart';

void main(){
  runApp(new MaterialApp(
    debugShowCheckedModeBanner: false,
    home: new DataTableDemo(),
    theme: ThemeData.light(),
  ));
}

class DataTableDemo extends StatefulWidget {

  final String title = "Data Table Flutter Demo";

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

class DataTableDemoState extends State<DataTableDemo> {
  List<User> users;
  List<User> selectedUsers;
  bool sort;

  @override
  void initState() {
    sort = false;
    selectedUsers = [];
    users = User.getUsers();
    super.initState();
  }

  onSortColum(int columnIndex, bool ascending) {
    if (columnIndex == 0) {
      if (ascending) {
        users.sort((a, b) => a.firstName.compareTo(b.firstName));
      } else {
        users.sort((a, b) => b.firstName.compareTo(a.firstName));
      }
    }
  }

  onSelectedRow(bool selected, User user) async {
    setState(() {
      if (selected) {
        selectedUsers.add(user);
      } else {
        selectedUsers.remove(user);
      }
    });
  }

  deleteSelected() async {
    setState(() {
      if (selectedUsers.isNotEmpty) {
        List<User> temp = [];
        temp.addAll(selectedUsers);
        for (User user in temp) {
          users.remove(user);
          selectedUsers.remove(user);
        }
      }
    });
  }

  SingleChildScrollView dataBody() {

    return SingleChildScrollView(
      scrollDirection: Axis.vertical,
      child: DataTable(
        sortAscending: sort,
        sortColumnIndex: 0,
        columns: [
          DataColumn(
              label: Text("FIRST NAME"),
              numeric: false,
              tooltip: "This is First Name",
              onSort: (columnIndex, ascending) {
                setState(() {
                  sort = !sort;
                });
                onSortColum(columnIndex, ascending);
              }),
          DataColumn(
            label: Text("LAST NAME"),
            numeric: false,
            tooltip: "This is Last Name",
          ),
        ],
        rows: users
            .map(
              (user) => DataRow(
                      selected: selectedUsers.contains(user),
                      onSelectChanged: (b) {
                        print("Onselect");
                        onSelectedRow(b, user);
                      },
                      cells: [
                        DataCell(
                          Text(user.firstName),
                          onTap: () {
                            print('Selected ${user.firstName}');
                          },
                        ),
                        DataCell(
                          Text(user.lastName),
                        ),
                      ]),
            )
            .toList(),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      floatingActionButton: new FloatingActionButton(
        child: new Icon(Icons.add),
        onPressed: (){
          showDialog(
            context: context,
            builder: (_) => new AlertDialog(
              title: new Text('Data Table Flutter'),
              content: new Container(
                height: 500,
                width: 400,
                child: new SingleChildScrollView(
                  scrollDirection: Axis.horizontal,
                  child: new Column(
                    children: <Widget>[
                      dataBody()
                    ],
                  ),
                )
              ),
              actions: <Widget>[
                new OutlineButton(
                  child: Text('SELECTED ${selectedUsers.length}'),
                  onPressed: () {},
                ),
                new OutlineButton(
                  child: Text('DELETE SELECTED'),
                  onPressed: selectedUsers.isEmpty
                      ? null
                      : () {
                          deleteSelected();
                        },
                ),
              ],
            )
          );
        },
      ),
    );
  }
}

class User {
  String firstName;
  String lastName;

  User({this.firstName, this.lastName});

  static List<User> getUsers() {
    return <User>[
      User(firstName: "Aaryan", lastName: "Shah"),
      User(firstName: "Ben", lastName: "John"),
      User(firstName: "Carrie", lastName: "Brown"),
      User(firstName: "Deep", lastName: "Sen"),
      User(firstName: "Emily", lastName: "Jane"),
    ];
  }
}

Please let me know, is some thing is missing or some thing I need to change. It will be very helpful for me. Thanks in advance.

1 Answer 1

1

The setState that you are using for updating the dialog is not for the dialog. It is calling the setState for the DataTableDemoState class. If you want to update the dialog make it another stateful widget and then when you want to update is call its setState method. Something like this:

        ......
        content: new SingleChildScrollView(
        child: new Material(
          child: new MyDialogContent(list: countries),
        ),
      ),
    );
  },

Where MyDialogContent is a stateful widget and you update your dialog content in the MyDialogContentState class. Hope that helps

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for your reply @nick.tdr , I will try and let you know
you are welcome. If that helps please accept the answer.
Can you please help me to solve this, I tried by writing but not worked for me please look at this codesee.herokuapp.com/d0cc61d380
yes I saw your code. Like I said if you need to update the dialog state, you need to call dialog's setState method. So move the logic where you are changing the checkbox state into the _MyDialogState class's setState method.
Thanks for the reply, Sure I will try that, and update u
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.