Is there any way to show the unique value in Dropdownbutton list? - flutter

My code is :
Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text.rich(
TextSpan(
children: [
TextSpan(
text: 'Select District',
style: getSemiBoldStyle(
color: Colormanager.lightGrey,
fontSize: AppSize.s14),
),
TextSpan(
text: AppStrings.star,
style: getSemiBoldStyle(
color: Colormanager.red,
fontSize: AppSize.s14),
),
],
),
),
const SizedBox(
height: AppSize.s4,
),
DropdownSearch<CoverageArea>(
popupProps: PopupProps.menu(
showSearchBox: true,
// disabledItemFn: (String s) => s.startsWith('I'),
searchFieldProps: TextFieldProps(
style: getBoldStyle(
color: Colormanager.black,
fontSize: FontSize.s15),
),
itemBuilder: (context, item, isSelected) =>
isSelected
? Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
item.district!,
style: getBoldStyle(
color: Colormanager.primary),
),
)
: Container(
padding: const EdgeInsets.all(8.0),
margin: const EdgeInsets.only(
bottom: 4),
color: Colormanager.lightGrey
.withOpacity(0.2),
child: Text(
item.district!,
style: getBoldStyle(
color: Colormanager.black,
fontSize: FontSize.s15),
),
),
),
items: controller.coverageAreas,
dropdownDecoratorProps: DropDownDecoratorProps(
baseStyle:
getSemiBoldStyle(color: Colormanager.black),
dropdownSearchDecoration: const InputDecoration(
// labelText: "Menu mode",
// hintText: "country in menu mode",
),
),
onChanged: (value) {
controller.selectedDistrict = value;
controller.selectDistrict.value =
value!.district.toString();
},
itemAsString: (item) => item.district.toString(),
// selectedItem: controller.selectArea.value,
),
],
),
and the api call show me :
{
"Status": true,
"message": "Coverage Area List",
"data": [
{
"id": 898,
"inside": 0,
"zone_name": "OSD Operation",
"district": "Barguna",
"area": "Barguna Sadar",
"district_id": 35,
"zone_id": 14,
"post_code": null,
"h_delivery": null,
"oneRe": null,
"oneUr": null,
"plusRe": null,
"plusUr": null,
"cod": null,
"insurance": null,
"status": 0,
"created_at": "2023-01-17 15:40:08",
"updated_at": "2023-01-17 15:40:08"
},
{
"id": 897,
"inside": 0,
"zone_name": "OSD Operation",
"district": "Barguna",
"area": "Taltoly",
"district_id": 35,
"zone_id": 14,
"post_code": null,
"h_delivery": null,
"oneRe": null,
"oneUr": null,
"plusRe": null,
"plusUr": null,
"cod": null,
"insurance": null,
"status": 0,
"created_at": "2023-01-17 15:39:49",
"updated_at": "2023-01-17 15:39:49"
},
{
"id": 896,
"inside": 0,
"zone_name": "OSD Operation",
"district": "Barguna",
"area": "Amtoly",
"district_id": 35,
"zone_id": 14,
"post_code": null,
"h_delivery": null,
"oneRe": null,
"oneUr": null,
"plusRe": null,
"plusUr": null,
"cod": null,
"insurance": null,
"status": 0,
"created_at": "2023-01-17 15:39:29",
"updated_at": "2023-01-17 15:39:29"
},
{
"id": 895,
"inside": 0,
"zone_name": "OSD Operation",
"district": "Barguna",
"area": "Betage",
"district_id": 35,
"zone_id": 14,
"post_code": null,
"h_delivery": null,
"oneRe": null,
"oneUr": null,
"plusRe": null,
"plusUr": null,
"cod": null,
"insurance": null,
"status": 0,
"created_at": "2023-01-17 15:39:05",
"updated_at": "2023-01-17 15:39:05"
},
{
"id": 891,
"inside": 0,
"zone_name": "OSD Operation",
"district": "Barishal",
"area": "Barisal Bandor Thana",
"district_id": 33,
"zone_id": 14,
"post_code": null,
"h_delivery": null,
"oneRe": null,
"oneUr": null,
"plusRe": null,
"plusUr": null,
"cod": null,
"insurance": null,
"status": 0,
"created_at": "2023-01-17 15:35:28",
"updated_at": "2023-01-17 15:35:28"
},
so on..............
I want to show Barguna district just once and area in dependable dropdownbutton in next. but how can i do it???
This is the output
how can i just show one name just once???

While there are multiple items contains single value, I will suggest you to use String dataType instead of CoverageArea. then you can create a set of string from data.
So the item will be like
final List<String> items = controller.coverageAreas.map((e)=> e.district.trim()).toSet().toList();
now use this items on dropdown. and the value will be return as String DropdownSearch<String>(

Related

Flutter RangeError (index): Invalid value: Only valid value is 0: 2

I can't fetch the initial default address 0 in the next username. It is often a problem when a large number of addresses are found in the list of addresses.
after some fixes type 'sting' is not a subtype of type 'int' of 'index'
The data in the address will be included in the same set of items.
Please help me I am practicing fetch api
This is Code
List<Items> _list = [];
List<Items> _search = [];
var loading = false;
Future fetchMos() async {
setState(() {
loading = true;
});
_list.clear();
var client = http.Client();
String mosUrl =
'';
var url = Uri.parse(mosUrl);
var headers = {
'Client-Token': '',
};
var response = await client.get(url, headers: headers);
if (response.statusCode == 200) {
var data = jsonDecode((utf8.decode(response.bodyBytes)))['items'];
setState(() {
for (var index in data) {
_list.add(Items.fromJson(index));
loading = false;
}
});
}
}
#override
void initState() {
// TODO: implement initState
super.initState();
fetchMos();
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
elevation: 0.0,
),
body: ListView.builder(
itemBuilder: (context, index) {
if (_list.length > 0) {
return _listItem(index);
} else {
return Center(child: CircularProgressIndicator());
}
},
itemCount: _list.length,
),
);
}
_listItem(index) {
return Card(
elevation: 3,
child: Column(
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Padding(
padding: const EdgeInsets.only(left: 5),
child: Text(
"${_list[index].name} ${_list[index].custnum}",
style: TextStyle(
color: Colors.black,
fontSize: 18,
fontFamily: "supermarket"),
),
),
TextButton(
child: const Text(
'แก้ไข',
style: TextStyle(fontFamily: "supermarket", fontSize: 14),
),
onPressed: () {/* ... */},
),
],
),
Padding(
padding: const EdgeInsets.all(5),
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Text(
'bill To / Ship To : ${_list[index].address![index].shipto.toString()}',
style: TextStyle(
color: Colors.black,
fontSize: 16,
fontFamily: "supermarket"),
),
],
),
),
Padding(
padding: const EdgeInsets.all(5),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Text(
"ที่อยู่ : ${_list[index].address![index].addr1.toString()}",
style: TextStyle(
color: Colors.black,
fontSize: 16,
fontFamily: "supermarket"),
),
],
),
),
],
),
);
}
}
This is json
{
"items": [
{
"custnum": "",
"name": "",
"address": [
{
"shipto": 0,
"addr1": "",
"thanon": "",
"tambon": "",
"amphur": "",
"prov_code": "",
"province": "",
"zipcode": "",
"country": "",
"contact": "",
"postcode": ""
},
{
"shipto": 1,
"addr1": "",
"thanon": "",
"tambon": "",
"amphur": "",
"prov_code": "",
"province": "",
"zipcode": "",
"country": "",
"contact": "",
"postcode": ""
},
{
"shipto": 2,
"addr1": "",
"thanon": "",
"tambon": "",
"amphur": "",
"prov_code": "",
"province": "",
"zipcode": "",
"country": "",
"contact": "",
"postcode": ""
},
{
"shipto": 3,
"addr1": "",
"thanon": "",
"tambon": "",
"amphur": "",
"prov_code": "",
"province": "",
"zipcode": "",
"country": "",
"contact": "",
"postcode": ""
}
]
},
{
"custnum": "",
"name": "",
"address": [
{
"shipto": 0,
"addr1": "",
"thanon": "",
"tambon": "",
"amphur": "",
"prov_code": "",
"province": "",
"zipcode": "",
"country": "",
"contact": "",
"postcode": ""
},
{
"shipto": 1,
"addr1": "",
"thanon": "",
"tambon": "",
"amphur": "",
"prov_code": "",
"province": "",
"zipcode": "",
"country": "",
"contact": "",
"postcode": ""
},
{
"shipto": 2,
"addr1": "",
"thanon": "",
"tambon": "",
"amphur": "",
"prov_code": "",
"province": "",
"zipcode": "",
"country": "",
"contact": "",
"postcode": ""
},
{
"shipto": 3,
"addr1": "",
"thanon": "",
"tambon": "",
"amphur": "",
"prov_code": "",
"province": "",
"zipcode": "",
"country": "",
"contact": "",
"postcode": ""
}
],
Text(
'bill To / Ship To : ${_list[index].address![index].shipto.toString()}',
style: TextStyle(
color: Colors.black, fontSize: 16, fontFamily: "supermarket"),
),
As per the above code, you using the same index for _list and address array. so maybe that will create this issue.
${_list[index].address![index].shipto.toString()}
I don't think this is right bcz when your index is 10 then
_list[10].address![10].shipto.toString()
if the address has not 10 elements then it will throw that exception.
and the same things to this code
"ที่อยู่ : ${_list[index].address![index].addr1.toString()}"

how to get API data inside map in flutter

how do I get the API data here based on the user's choice, for example if the user chooses number 3 then the data that is issued is the data that is at number 3 in the API
final List<String> list = <String>['Ganjil', 'Genap' ];
final data = {
'Ganjil': [
'1', //I want to call API data here
'3', //I want to call API data here
'5', //I want to call API data here
'7', //I want to call API data here
],
'Genap': [
'2', //I want to call API data here
'4', //I want to call API data here
'6', //I want to call API data here
'8',
],
};
i want to call data based on existing id in API. how do i do it. Thank
you so when the user selects number 1 it will display API data with ID number 1
Container(
padding: const EdgeInsets.only(left: 12, right: 8),
width: double.infinity,
height: 50,
decoration: BoxDecoration(
color: Colors.white,
),
child: DropdownButtonHideUnderline(
child: DropdownButton(
hint: const Text(
'Pilih Semester',
),
isExpanded: true,
value: mainValue,
items: list
.map(
(e) => DropdownMenuItem<String?>(
value: e,
child: Text(
e,
style: regular5,
),
),
)
.toList(),
onChanged: (value) {
subValue = null;
mainValue = value;
setState(() {});
},
),
),
),
const SizedBox(
height: 16,
),
Text(
'Semester',
style: bold5,
),
const SizedBox(
height: 8,
),
Container(
padding: const EdgeInsets.only(left: 12, right: 8),
width: double.infinity,
height: 50,
decoration: BoxDecoration(
color: Colors.white,
),
child: DropdownButtonHideUnderline(
child: DropdownButton(
hint: Text(
'Pilih Semester',
style: regular5,
),
isExpanded: true,
value: subValue,
items: data[mainValue]
?.map(
(e) => DropdownMenuItem<String?>(
value: e,
child: Text(
e,
style: regular5,
),
),
)
.toList() ??
[],
onChanged: (value) {
subValue = value.toString();
setState(() {});
},
),
),
),
and this respon json
{
"status": "success",
"code": "200",
"data": {
"1": {
"id": "f732bbb0-a34a-474d-8829-23aa66470e22",
"id_dosen": "d6aedfb6-cf88-4e89-8365-0f206822a6c4",
"id_mk": "cb0bced5-a02d-4f46-bd88-6ed61daece10",
"nidn": null,
"dosen": "Yudhy",
"id_kelas_kuliah": "52deb32d-292f-44b9-af69-a90dfc5fbc81",
"kelas_kuliah": "Pendidikan agama islam III - Sistem Informasi - A",
"prodi": "Sistem Informasi",
"kelas": "KARYAWAN",
"semester": "5",
"kelompok_kelas": "A",
"kode": null,
"sks": 2,
"jumlah_kelas": 0,
"matakuliah": "Pendidikan agama islam III ( Islamic Religious Education III ) - A",
"smt": "2022-2023 GANJIL",
"bobot_sks": 2,
"rencana_pertemuan": 14,
"jenis_evaluasi": "KOGNITIF/PENGETAHUAN",
"created_at": "2022-09-09 08:14:14",
"updated_at": "2022-09-09 08:14:14",
"created_by": "Fahmi Nugraha",
"updated_by": "Fahmi Nugraha"
},
"2": {
"id": "3573bcf8-bf00-445b-91bb-8362e98f3e70",
"id_dosen": "d61b7164-cd6c-4bd9-8be8-d2a576790b9c",
"id_mk": "40f02349-887d-47c2-b190-9c5d62adf738",
"nidn": null,
"dosen": "Shadam Hussaeni",
"id_kelas_kuliah": "fb969bb3-e0d9-47ac-9ede-365c78e38994",
"kelas_kuliah": "Bahasa inggris III (Conversation) - Sistem Informasi - A",
"prodi": "Sistem Informasi",
"kelas": "KARYAWAN",
"semester": "5",
"kelompok_kelas": "A",
"kode": null,
"sks": 2,
"jumlah_kelas": 0,
"matakuliah": "Bahasa inggris III (Conversation) ( English III (Conversation) ) - A",
"smt": "2022-2023 GANJIL",
"bobot_sks": 2,
"rencana_pertemuan": 14,
"jenis_evaluasi": "KOGNITIF/PENGETAHUAN",
"created_at": "2022-09-14 08:05:31",
"updated_at": "2022-09-14 08:05:31",
"created_by": "Risca Nurzantika",
"updated_by": "Risca Nurzantika"
}, ...
You can call the API in onChange with the new mainValue
onChanged: (value) {
subValue = value.toString();
callAPI(); // Call API here
setState(() {});
}

Flutter - How to perform multi select in the same list of objects but with a different question?

I have a list of objects called options
List<Map<String, dynamic>> options = [
{"name": "usually", "value": 100, "id": 1},
{"name": "sometimes", "value": 70, "id": 2},
{"name": "rarely", "value": 40, "id": 3},
{"name": "never", "value": 0, "id": 4},
];
also, I have another list called questions
late List<Map<String, dynamic>> questions = [
{"id": 1, "name": "Q1", "options": options},
{"id": 2, "name": "Q2", "options": options},
{"id": 3, "name": "Q3", "options": options},
{"id": 4, "name": "Q4", "options": options},
{"id": 5, "name": "Q5", "options": options},
{"id": 6, "name": "Q6", "options": options},
{"id": 7, "name": "Q7", "options": options},
{"id": 8, "name": "Q8", "options": options},
{"id": 9, "name": "Q9", "options": options},
{"id": 10, "name": "Q10", "options": options},
{"id": 11, "name": "Q11", "options": options},
{"id": 12, "name": "Q12", "options": options},
{"id": 13, "name": "Q13", "options": options},
{"id": 14, "name": "Q14", "options": options},
];
How can I perform multi select for each question which allow the user select one option per one question ?
Code for the UI
return Scaffold(
body: Column(
children: [
Expanded(
child: ListView.separated(
itemCount: questions.length,
separatorBuilder: (context, index) {
return Divider();
},
itemBuilder: (context, index) {
return ListTile(
title: Text(questions[index]['name']),
subtitle: Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: List.generate(
questions[index]['options'].length,
(lowerIndex) => Padding(
padding: const EdgeInsets.all(5.0),
child: InkWell(
onTap: () =>
ChoicesHelper.addValueToSelectedValuesList(
questions[index]['options'][lowerIndex]
['value'],
questions[index]['id'],
questions[index]['options'][lowerIndex]
['id']),
child: Row(
crossAxisAlignment:
CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: [
Container(
width: 20,
height: 20,
color: Colors.grey ),
SizedBox(width: 1),
Text(questions[index]['options']
[lowerIndex]['name'])
])))),
),
);
},
),
),
ElevatedButton(
onPressed: () {
ChoicesHelper.calculateResults();
},
child: Text('Calculate')),
],
),
);
Code for the methods in another class
static List<int> selectedValues = [];
static List<Map<String, dynamic>> answeredQuestionsIds = [];
static void addValueToSelectedValuesList(int value, int questionID, optionID) {
selectedValues.add(value);
answeredQuestionsIds.add({"questionID": questionID,"optionID": optionID});
}
As you can see, I just need to select one box for each question and turn it's color to red, but the problem is that all the row for single question is selected and all the boxes turns into red.
How can I select only one option for one question ?
try this:
class ListWidget extends StatefulWidget {
const ListWidget({Key? key}) : super(key: key);
#override
State<ListWidget> createState() => _ListWidgetState();
}
class _ListWidgetState extends State<ListWidget> {
late List<Map<String, dynamic>> questions = [
{"id": 1, "name": "Q1", "options": options},
{"id": 2, "name": "Q2", "options": options},
{"id": 3, "name": "Q3", "options": options},
{"id": 4, "name": "Q4", "options": options},
{"id": 5, "name": "Q5", "options": options},
{"id": 6, "name": "Q6", "options": options},
{"id": 7, "name": "Q7", "options": options},
{"id": 8, "name": "Q8", "options": options},
{"id": 9, "name": "Q9", "options": options},
{"id": 10, "name": "Q10", "options": options},
{"id": 11, "name": "Q11", "options": options},
{"id": 12, "name": "Q12", "options": options},
{"id": 13, "name": "Q13", "options": options},
{"id": 14, "name": "Q14", "options": options},
];
List<Map<String, dynamic>> options = [
{"name": "usually", "value": 100, "id": 1},
{"name": "sometimes", "value": 70, "id": 2},
{"name": "rarely", "value": 40, "id": 3},
{"name": "never", "value": 0, "id": 4},
];
List<Question> _questions = [];
#override
void initState() {
super.initState();
_questions = questions.map((e) => Question.fromJson(e)).toList();
}
#override
Widget build(BuildContext context) {
return Scaffold(
body: ListView.builder(
itemCount: _questions.length,
itemBuilder: (context, index) {
return _buildQuestion(
_questions[index],
(int? value) {
print("cvalue = ${value}");
var q = Question(
name: _questions[index].name,
id: _questions[index].id,
options: _questions[index].options,
groupValue: value!);
setState(() {
_questions[index] = q;
});
},
);
}),
);
}
_buildQuestion(Question question, Function(int?)? onChanged) {
return Row(
children: [
Expanded(
child: RadioListTile<int?>(
contentPadding: EdgeInsets.zero,
dense: true,
visualDensity: VisualDensity(horizontal: -4),
title: Text(question.options[0].name),
value: question.options[0].value,
groupValue: question.groupValue,
onChanged: onChanged),
),
Expanded(
child: RadioListTile<int?>(
contentPadding: EdgeInsets.zero,
dense: true,
visualDensity: VisualDensity(horizontal: -4),
title: Text(question.options[1].name),
value: question.options[1].value,
groupValue: question.groupValue,
onChanged: onChanged),
),
Expanded(
child: RadioListTile<int?>(
contentPadding: EdgeInsets.zero,
dense: true,
visualDensity: VisualDensity(horizontal: -4),
title: Text(question.options[2].name),
value: question.options[2].value,
groupValue: question.groupValue,
onChanged: onChanged),
),
Expanded(
child: RadioListTile<int?>(
contentPadding: EdgeInsets.zero,
dense: true,
visualDensity: VisualDensity(horizontal: -4),
title: Text(question.options[3].name),
value: question.options[3].value,
groupValue: question.groupValue,
onChanged: onChanged),
),
],
);
}
}
class Question {
final String name;
final int id;
final List<OptionModel> options;
final int groupValue;
Question(
{required this.name,
required this.id,
required this.options,
required this.groupValue});
static Question fromJson(Map<String, dynamic> json) {
return Question(
name: json['name'],
id: json['id'],
groupValue: -1,
options: (json['options'] as List)
.map((e) => OptionModel.fromJson(e))
.toList());
}
}
class OptionModel {
final String name;
final int value;
final int id;
OptionModel({required this.name, required this.id, required this.value});
static OptionModel fromJson(Map<String, dynamic> json) {
return OptionModel(
name: json['name'], id: json['id'], value: json['value']);
}
}

error: Null check operator used on a null value OR List<dynamic> is not a subtype of type Map<String, dynamic>

fetch Two different pages api with same uniq id
I want the user_uniq_id of the API of the button and the user_uniq_id of the API of the team_list to match and show the data of its user after the match.
this code is for first File
This code is of the button that is coming from the api
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:practice/left_team.dart';
import 'package:http/http.dart' as http;
class Button extends StatefulWidget {
#override
_ButtonState createState() => _ButtonState();
}
class _ButtonState extends State<Button> {
var api = Uri.parse('http://192.***.***.***/flutter/teamApi.php');
var response;
var teamApi;
#override
void initState() {
super.initState();
fetchData();
}
fetchData() async {
response = await http.get(api);
print(response.body);
teamApi = jsonDecode(response.body);
setState(() {});
}
#override
Widget build(BuildContext context) {
var color = 0xff453658;
return Scaffold(
appBar: AppBar(
backgroundColor: Color(0xff392850),
title: Row(
children: [
InkWell(
onTap: () {
Navigator.pop(context);
},
child: Icon(Icons.arrow_back)),
SizedBox(
width: 10.0,
),
Text(
"Income",
style: TextStyle(fontStyle: FontStyle.italic),
),
],
),
actions: [
IconButton(
icon: Icon(Icons.person),
onPressed: () => print("open cart"),
),
],
),
body: Column(
children: [
Container(
margin: const EdgeInsets.all(20.0),
padding: const EdgeInsets.all(10.0),
decoration: BoxDecoration(
border: Border.all(width: 2, color: Color(color))),
child: Row(
children: [
Text(
"Total Income:",
style: TextStyle(
fontSize: 30.0,
fontWeight: FontWeight.bold,
fontStyle: FontStyle.italic),
),
SizedBox(
width: 10.0,
),
Text(
"Rs.2000",
style: TextStyle(
fontSize: 20.0,
fontWeight: FontWeight.bold,
fontStyle: FontStyle.italic),
),
],
),
),
SizedBox(
height: 20.0,
),
Flexible(
child: Container(
child: GridView.count(
childAspectRatio: 1.0,
padding: EdgeInsets.only(left: 16, right: 16),
crossAxisCount: 3,
crossAxisSpacing: 18,
mainAxisSpacing: 18,
children: List.generate(
teamApi.length,
(index) => GestureDetector(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
LeftTeam(teamData: teamApi[index])),
);
},
child: Container(
decoration: BoxDecoration(
color: Color(0xff00ffff),
borderRadius: BorderRadius.circular(10)),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Center(
child: Text(teamApi[index]["teamType"],
style: TextStyle(
color: Colors.white,
fontSize: 16,
fontWeight: FontWeight.w600)),
),
],
),
),
),
),
),
),
),
],
),
);
}
}
This is API data of button
[{"teamType":"direct team","team_name":"platinum","team_number":"234","team_uniq_id":"1","team_last_update":"10-may-2021"},{"teamType":"left team","team_name":"gold","team_number":"356","team_uniq_id":"2","team_last_update":"10-may-2021"},{"teamType":"right team","team_name":"silver","team_number":"876","team_uniq_id":"3","team_last_update":"10-may-2021"}]
this is code for second file.
this is model part.
class MyData {
List<Results> user = [];
MyData.fromJson(Map<String, dynamic> json) {
// previous = json['previous'];
// next = json['next'];
if (json['results'] != null) {
user = <Results>[];
json['results'].forEach((v) {
user.add(new Results.fromJson(v));
});
}
}
}
class Results {
String user_name = "";
String user_mother_name = "";
String user_address = "";
String user_mobile = "";
String user_sponsor_id = "";
String sponsor_id = "";
String email = "";
String city = "";
String state = "";
String dob = "";
Results.fromJson(Map<String, dynamic> json) {
user_name = json['user_name'];
user_mother_name = json['user_mother_name'];
user_address = json['user_address'];
user_mobile = json['user_mobile'];
user_sponsor_id = json['user_sponsor_id'];
email = json['email'];
city = json['city'];
state = json['state'];
dob = json['dob'];
}
}
this provider of api link.
import 'dart:convert';
import 'package:flutter/widgets.dart';
import 'package:http/http.dart' as http;
import 'package:practice/LeftTeamFile/model/myData.dart';
class MyHomePageProvider extends ChangeNotifier {
MyData? data;
Future getData(context) async {
var url = Uri.parse(
var url = Uri.parse('http://192.***.***.***/flutter/team_list.php');
var response = await http.get(url);
print("res${response.body}");
var mJson = json.decode(response.body);
this.data = MyData.fromJson(mJson);
this.notifyListeners();
this is teamList part .
import 'package:flutter/material.dart';
import 'package:practice/LeftTeamFile/provider/myHomePageProvider.dart';
import 'package:provider/provider.dart';
class TeamList extends StatefulWidget {
final teamData;
const TeamList({Key? key, this.teamData}) : super(key: key);
#override
_TeamListState createState() => _TeamListState();
}
class _TeamListState extends State<TeamList> {
#override
Widget build(BuildContext context) {
return Scaffold(
body: ListView(
children: [
Container(
padding: EdgeInsets.all(10.0),
height: 100.0,
color: Color(0xffedbf6b),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Row(
children: [
Icon(
Icons.list_alt,
color: Colors.white,
),
Text(
widget.teamData['team_uniq_id'],
style: TextStyle(
fontSize: 20.0,
color: Colors.white,
),
),
],
),
]),
),
SizedBox(
height: 10.0,
),
Stack(
children: [
ChangeNotifierProvider<MyHomePageProvider>(
create: (context) => MyHomePageProvider(),
child: Consumer<MyHomePageProvider>(
builder: (context, provider, child) {
if (provider.data!.team_uniq_id ==
widget.teamData['team_uniq_id']) {
print("prov $provider.data");
return SingleChildScrollView(
scrollDirection: Axis.horizontal,
// Data table widget in not scrollable so we have to wrap it in a scroll view when we have a large data set..
child: SingleChildScrollView(
child: DataTable(
columns: [
DataColumn(
label: Text('Name'),
tooltip: 'represents if user is verified.'),
DataColumn(
label: Text('Mother_name'),
tooltip: 'represents first S no of the user'),
DataColumn(
label: Text('address'),
tooltip: 'represents Sponsor ID of the user'),
DataColumn(
label: Text('mobile'),
tooltip: 'represents User ID of the user'),
DataColumn(
label: Text('User_s_id'),
tooltip: 'represents Name of the user'),
DataColumn(
label: Text('sponsor_id'),
tooltip: 'represents Mobile of the user'),
DataColumn(
label: Text('email'),
tooltip: 'represents Date of the user'),
DataColumn(
label: Text('city'),
tooltip: 'represents Date of the user'),
DataColumn(
label: Text('state'),
tooltip: 'represents Date of the user'),
DataColumn(
label: Text('dob'),
tooltip: 'represents Date of the user'),
],
rows: provider.data!.user
.map((data) =>
// we return a DataRow every time
DataRow(
// List<DataCell> cells is required in every row
cells: [
red when unverified
DataCell(Text(data.user_name)),
DataCell(Text(data.user_mother_name)),
DataCell(Text(data.user_address)),
DataCell(Text(data.user_mobile)),
DataCell(Text(data.user_sponsor_id)),
DataCell(Text(data.sponsor_id)),
DataCell(Text(data.email)),
DataCell(Text(data.city)),
DataCell(Text(data.state)),
DataCell(Text(data.dob)),
]))
.toList(),
),
),
);
}, provider.getData(context);
return Center(child: CircularProgressIndicator());
),
),
],
),
],
),
);
}
}
this is teamlist api
[
{
"teamType": "direct Team",
"team_uniq_id": "1",
"user": [
{
"user_name": "deepak",
"user_mother_name": "Accomodation",
"user_address": "varanasi",
"user_mobile": "5678989",
"user_sponsor_id": "123456",
"sponsor_id": "3456",
"email": "abc#gmai.com",
"city": "varanasi",
"state": "India",
"dob": "12-5-1996"
},
{
"user_name": "deepak",
"user_mother_name": "Accomodation",
"user_address": "varanasi",
"user_mobile": "5678989",
"user_sponsor_id": "123456",
"sponsor_id": "3456",
"email": "abc#gmai.com",
"city": "varanasi",
"state": "India",
"dob": "12-5-1996"
},
{
"user_name": "deepak",
"user_mother_name": "Accomodation",
"user_address": "varanasi",
"user_mobile": "5678989",
"user_sponsor_id": "123456",
"sponsor_id": "3456",
"email": "abc#gmai.com",
"city": "varanasi",
"state": "India",
"dob": "12-5-1996"
}
]
},
{
"teamType": "left Team",
"team_uniq_id": "2",
"user": [
{
"user_name": "Ashu",
"user_mother_name": "manju",
"user_address": "Mirzapur",
"user_mobile": "222222",
"user_sponsor_id": "123456",
"sponsor_id": "3456",
"email": "abc#gmai.com",
"city": "varanasi",
"state": "India",
"dob": "12-5-1996"
},
{
"user_name": "Ashutodh",
"user_mother_name": "manju1",
"user_address": "Mirzapur1",
"user_mobile": "2222221",
"user_sponsor_id": "1234561",
"sponsor_id": "34561",
"email": "abc#gmai.com1",
"city": "varanasi1",
"state": "India1",
"dob": "12-5-19961"
}
]
},
{
"teamType": "Right Team",
"team_uniq_id": "3",
"user": [
{
"user_name": "tosh",
"user_mother_name": "snju",
"user_address": "Allahabad",
"user_mobile": "44444444",
"user_sponsor_id": "333456",
"sponsor_id": "6666666",
"email": "jkl#gmai.com",
"city": "lucknow",
"state": "India",
"dob": "15-3-1956"
}
]
},
{
"teamType": "Total Team",
"team_uniq_id": "4",
"user": [
{
"user_name": "tosh",
"user_mother_name": "snju",
"user_address": "Allahabad",
"user_mobile": "44444444",
"user_sponsor_id": "333456",
"sponsor_id": "6666666",
"email": "jkl#gmai.com",
"city": "lucknow",
"state": "India",
"dob": "15-3-1956"
},
{
"user_name": "deepak",
"user_mother_name": "Accomodation",
"user_address": "varanasi",
"user_mobile": "5678989",
"user_sponsor_id": "123456",
"sponsor_id": "3456",
"email": "abc#gmai.com",
"city": "varanasi",
"state": "India",
"dob": "12-5-1996"
},
{
"user_name": "deepak",
"user_mother_name": "Accomodation",
"user_address": "varanasi",
"user_mobile": "5678989",
"user_sponsor_id": "123456",
"sponsor_id": "3456",
"email": "abc#gmai.com",
"city": "varanasi",
"state": "India",
"dob": "12-5-1996"
},
{
"user_name": "tosh",
"user_mother_name": "snju",
"user_address": "Allahabad",
"user_mobile": "44444444",
"user_sponsor_id": "333456",
"sponsor_id": "6666666",
"email": "jkl#gmai.com",
"city": "lucknow",
"state": "India",
"dob": "15-3-1956"
}
]
}
]
I feel some error is coming.
You can try to make sure you convert the object you are getting from the server to list, to be able to pass in the list into the widget

List inside list with checkbox in flutter

I need to create a checkbox with select all functionality , here is my sample json
[
{
"id": 1,
"name": "categories 1",
"child": [
{
"id": 29,
"name": "sub 1"
},
{
"id": 30,
"name": "sub 2"
},
{
"id": 31,
"name": "sub 3 "
}
]
},
{
"id": 2,
"name": "categories 2",
"child": [
{
"id": 23,
"name": "sub 1"
},
{
"id": 24,
"name": "sub 1"
},
{
"id": 25,
"name": "sub 1"
}
]
},
{
"id": 3,
"name": "categories 3",
"child": [
{
"id": 222,
"name": "sub 1"
},
{
"id": 229,
"name": "sub 1"
},
{
"id": 229,
"name": "sub 1"
}
]
}
]
How i need is
If I check categories 1 all under categories should be selected/unselected and if I select category 2 all under that should be selected and also individual item also need to be selected
Select all should select all checkbox in all categories
Try something like this.
Container(
child: ListView.builder(
itemCount: 50,
shrinkWrap: true,
itemBuilder: (BuildContext context, int index) {
return Container(
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Wrap(
children: [
Container(
child: Icon(
Icons.check_box,
)
),
Container(
child: Text(
'Category ${index+1}'
),
),
],
),
Container(
margin: EdgeInsets.only(left: 16),
child: ListView.builder(
itemCount: 3,
shrinkWrap: true,
itemBuilder: (BuildContext context, int index2) {
return Wrap(
children: [
Container(
child: Icon(
Icons.check_box,
)
),
Container(
child: Text(
'Subcategory ${index2+1}'
),
),
],
);
}
)
)
]
)
);
}
)
)
Then you have to put your own logic according to your requirement.
I think using this is more flexible, tree_view:
var treeView = TreeView(
parentList: [
Parent(
parent: Text('Desktop'),
childList: ChildList(
children: <Widget>[
Parent(
parent: Text('documents'),
childList: ChildList(
children: <Widget>[
Text('Resume.docx'),
Text('Billing-Info.docx'),
],
),
),
Text('MeetingReport.xls'),
Text('MeetingReport.pdf'),
Text('Demo.zip'),
],
),
),
],
);