I used predefined data for a ui but it is not working. Here is my attempt:
List? authId=[1,3];
List<CheckBoxModel> checkBoxListTileModel =
CheckBoxModel.getUsersAuthorization1();
@override
Widget build(BuildContext context) {
return Scaffold(
body: ListView.builder(
itemCount: checkBoxListTileModel.length,
itemBuilder: (BuildContext context, int index) {
return Card(
child: Container(
padding: EdgeInsets.all(10.0),
child: Column(
children: <Widget>[
CheckboxListTile(
dense: true,
title: Text('${checkBoxListTileModel[index].name}',),
value: authId==checkBoxListTileModel[index].id ? true : false,
onChanged: (bool? val) {
checkBoxListTileModel[index].isCheck = val;
})
],
),
),
);
}),
);
}
}
I can handle in reading the data. I can get them but when i try to change them or used list problem happens. It does not work. When i took authId as an int it works but still not work properly, i cannot change its value.
Here is my list implementation.
class CheckBoxModel {
int? id;
String? name;
bool? isCheck;
CheckBoxModel({this.id, this.name, this.isCheck});
static List<CheckBoxModel> getUsersAuthorization1() {
return <CheckBoxModel> [
CheckBoxModel(
id: 1,
name: 'Get',
isCheck: false,
),
CheckBoxModel(
id: 2,
name: 'Insert',
isCheck: false,
),
CheckBoxModel(
id: 3,
name: 'Update',
isCheck: false,
),
];
}
}
CodePudding user response:
My solution:
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
List<CheckBoxModel> checkBoxListTileModel =
CheckBoxModel.getUsersAuthorization1();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: ListView.builder(
itemCount: checkBoxListTileModel.length,
itemBuilder: (BuildContext context, int index) {
return Card(
child: Container(
padding: const EdgeInsets.all(10.0),
child: Column(
children: <Widget>[
CheckboxListTile(
dense: true,
title: Text(
'${checkBoxListTileModel[index].name}',
),
value: checkBoxListTileModel[index].isCheck,
onChanged: (bool? val) {
setState(() {
checkBoxListTileModel[index].isCheck = val;
});
})
],
),
),
);
}),
);
}
}
class CheckBoxModel {
int? id;
String? name;
bool? isCheck;
CheckBoxModel({this.id, this.name, this.isCheck});
static List<CheckBoxModel> getUsersAuthorization1() {
return <CheckBoxModel>[
CheckBoxModel(
id: 1,
name: 'Get',
isCheck: false,
),
CheckBoxModel(
id: 2,
name: 'Insert',
isCheck: false,
),
CheckBoxModel(
id: 3,
name: 'Update',
isCheck: false,
),
];
}
}
CodePudding user response:
I do not consider use List as good solution for it. But if you really need it:
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
List<CheckBoxModel> checkBoxListTileModel =
CheckBoxModel.getUsersAuthorization1();
List? authId = [1, 3];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: ListView.builder(
itemCount: checkBoxListTileModel.length,
itemBuilder: (BuildContext context, int index) {
return Card(
child: Container(
padding: const EdgeInsets.all(10.0),
child: Column(
children: <Widget>[
CheckboxListTile(
dense: true,
title: Text(
'${checkBoxListTileModel[index].name}',
),
value: authId!
.where((e) => e == checkBoxListTileModel[index].id)
.iterator
.moveNext(),
onChanged: (bool? val) {
setState(() {
if (val!) {
authId!.add(checkBoxListTileModel[index].id);
} else {
authId!.removeWhere(
(e) => e == checkBoxListTileModel[index].id);
}
});
})
],
),
),
);
}),
);
}
}
class CheckBoxModel {
int? id;
String? name;
CheckBoxModel({this.id, this.name});
static List<CheckBoxModel> getUsersAuthorization1() {
return <CheckBoxModel>[
CheckBoxModel(
id: 1,
name: 'Get',
),
CheckBoxModel(
id: 2,
name: 'Insert',
),
CheckBoxModel(
id: 3,
name: 'Update',
),
];
}
}
