gpt4 book ai didi

checkbox - Flutter Alert 对话框数据表 Checkbox

转载 作者:IT王子 更新时间:2023-10-29 07:17:25 27 4
gpt4 key购买 nike

我在警报对话框中有一个带有复选框的数据表,当我单击该复选框时,它没有选中(选中=选中)该框。我跟着这个video

这是我的尝试

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"),
];
}
}

请让我知道,是否缺少某些东西或我需要更改某些东西。这对我很有帮助。提前致谢。

最佳答案

您用于更新对话框的 setState 不适用于对话框。它正在为 DataTableDemoState 类调用 setState。如果你想更新对话框,使它成为另一个有状态的小部件,然后当你想更新时调用它的 setState 方法。像这样:

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

其中 MyDialogContent 是一个有状态的小部件,您可以在 MyDialogContentState 类中更新对话框内容。希望有帮助

关于checkbox - Flutter Alert 对话框数据表 Checkbox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56920668/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com