如何将 API 列表数据映射到单个复选框?在颤抖中
how to map API list data to single checkbox? in flutter
嗨,我是 flutter 的新手,我需要帮助来获取 API 数据并显示 select 和 deselect 的复选框。
我使用了示例 API 数据库,其中包含 10 个用户的信息。我想将用户 ID、姓名、用户名、公司名称和 phone 号码显示为每个用户的卡片或列表磁贴。我还想为每个 card/list 图块分配一个复选框,具有选中/取消选中功能。
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter_application_http_get/example.dart';
import 'package:flutter_application_http_get/screen.dart';
import 'package:flutter_application_http_get/sunday_state.dart';
import 'package:http/http.dart' as http;
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Sunday(),
);
}
}
这是我的 ApI 获取方法
class Sunday extends StatefulWidget {
const Sunday({Key? key}) : super(key: key);
@override
_SundayState createState() => _SundayState();
}
class _SundayState extends State<Sunday> {
var users = [];
Future getUserData() async {
var res =
await http.get(Uri.https("jsonplaceholder.typicode.com", "users"));
var jsonData = jsonDecode(res.body) as List;
setState(() {
users = jsonData;
});
}
@override
void initState() {
super.initState();
getUserData();
}
这是我的构建方法
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("User Data"),
),
body: Container(
child: Card(
margin: EdgeInsets.all(20.0),
child: ListView.builder(
itemCount: users.length,
itemBuilder: (context, i) {
final post = users[i];
return Card(
elevation: 5,
child: Padding(
padding: const EdgeInsets.all(12.0),
child: ListView(shrinkWrap: true, children: [
// singlecheckbox(notification),
...notification.map(singlecheckbox).toList(),
Text("${post['id']}"),
Text("${post['name']}"),
])
这是我用来切换复选框的小部件
final notification = [SundayCheckBoxState()];
void togglegroupcheckbox(bool? value) {
if (value == null) return;
setState(() {
// notification.value = value;
notification.forEach((element) => element.value = value);
});
}
Widget singlecheckbox(SundayCheckBoxState checkbox) {
return CheckboxListTile(
controlAffinity: ListTileControlAffinity.leading,
activeColor: Colors.green,
value: checkbox.value,
// title: Text('${users.last['name']}'),
onChanged: togglegroupcheckbox,
);
有很多选项,其中之一是使用 ListTile
并在每行的开头(或末尾)添加 CheckBox
。您也必须跟踪每个复选框的状态,例如在您的 users
列表中。
尝试这样定义您的 ListView.builder
:
ListView.builder(
itemCount: users.length,
itemBuilder: (context, i) {
final post = users[i];
return Card(
elevation: 5,
child: Padding(
padding: const EdgeInsets.all(12.0),
child: ListView(shrinkWrap: true, children: [
// singlecheckbox(notification),
//...notification.map(singlecheckbox).toList(),
ListTile(
isThreeLine: true,
leading: Checkbox(
value: post["checked"] ?? false,
onChanged: (bool? value) {
setState(() {
post["checked"] = value;
});
},
),
title: Text(
"${post['name']}\n${post['company']['name']}"),
subtitle: Text("${post['email']}"),
trailing: Text("${post['username']}"))
])));
})
嗨,我是 flutter 的新手,我需要帮助来获取 API 数据并显示 select 和 deselect 的复选框。 我使用了示例 API 数据库,其中包含 10 个用户的信息。我想将用户 ID、姓名、用户名、公司名称和 phone 号码显示为每个用户的卡片或列表磁贴。我还想为每个 card/list 图块分配一个复选框,具有选中/取消选中功能。
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter_application_http_get/example.dart';
import 'package:flutter_application_http_get/screen.dart';
import 'package:flutter_application_http_get/sunday_state.dart';
import 'package:http/http.dart' as http;
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Sunday(),
);
}
}
这是我的 ApI 获取方法
class Sunday extends StatefulWidget {
const Sunday({Key? key}) : super(key: key);
@override
_SundayState createState() => _SundayState();
}
class _SundayState extends State<Sunday> {
var users = [];
Future getUserData() async {
var res =
await http.get(Uri.https("jsonplaceholder.typicode.com", "users"));
var jsonData = jsonDecode(res.body) as List;
setState(() {
users = jsonData;
});
}
@override
void initState() {
super.initState();
getUserData();
}
这是我的构建方法
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("User Data"),
),
body: Container(
child: Card(
margin: EdgeInsets.all(20.0),
child: ListView.builder(
itemCount: users.length,
itemBuilder: (context, i) {
final post = users[i];
return Card(
elevation: 5,
child: Padding(
padding: const EdgeInsets.all(12.0),
child: ListView(shrinkWrap: true, children: [
// singlecheckbox(notification),
...notification.map(singlecheckbox).toList(),
Text("${post['id']}"),
Text("${post['name']}"),
])
这是我用来切换复选框的小部件
final notification = [SundayCheckBoxState()];
void togglegroupcheckbox(bool? value) {
if (value == null) return;
setState(() {
// notification.value = value;
notification.forEach((element) => element.value = value);
});
}
Widget singlecheckbox(SundayCheckBoxState checkbox) {
return CheckboxListTile(
controlAffinity: ListTileControlAffinity.leading,
activeColor: Colors.green,
value: checkbox.value,
// title: Text('${users.last['name']}'),
onChanged: togglegroupcheckbox,
);
有很多选项,其中之一是使用 ListTile
并在每行的开头(或末尾)添加 CheckBox
。您也必须跟踪每个复选框的状态,例如在您的 users
列表中。
尝试这样定义您的 ListView.builder
:
ListView.builder(
itemCount: users.length,
itemBuilder: (context, i) {
final post = users[i];
return Card(
elevation: 5,
child: Padding(
padding: const EdgeInsets.all(12.0),
child: ListView(shrinkWrap: true, children: [
// singlecheckbox(notification),
//...notification.map(singlecheckbox).toList(),
ListTile(
isThreeLine: true,
leading: Checkbox(
value: post["checked"] ?? false,
onChanged: (bool? value) {
setState(() {
post["checked"] = value;
});
},
),
title: Text(
"${post['name']}\n${post['company']['name']}"),
subtitle: Text("${post['email']}"),
trailing: Text("${post['username']}"))
])));
})