Last active
May 3, 2021 14:14
-
-
Save IsmailAlamKhan/a32bac80c7a9e0cd881604215245dc69 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
final Color darkBlue = Color.fromARGB(255, 18, 32, 47); | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue), | |
debugShowCheckedModeBanner: false, | |
home: Home(), | |
); | |
} | |
} | |
class Home extends StatelessWidget { | |
Home({Key? key}) : super(key: key); | |
final List<CheckBoxClass> list = [ | |
CheckBoxClass('Ok', false), | |
CheckBoxClass('No', false), | |
]; | |
final GlobalKey<FormState> formKey = GlobalKey<FormState>(); | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
actions: [ | |
IconButton( | |
icon: Icon(Icons.save), | |
onPressed: () { | |
formKey.currentState?.validate(); | |
}) | |
], | |
), | |
body: Center( | |
child: Form( | |
key: formKey, | |
child: FormField<List<bool>>( | |
validator: (value) { | |
final _list = value?.where((element) => element).toList(); | |
if (_list?.isEmpty ?? true) { | |
return 'Please choose one'; | |
} | |
}, | |
builder: (state) => Column( | |
children: [ | |
Wrap( | |
children: List.generate( | |
list.length, | |
(index) { | |
final item = list[index]; | |
return Padding( | |
padding: const EdgeInsets.all(8.0), | |
child: CheckboxListTile( | |
title: Text(item.text), | |
value: state.value?[index] ?? false, | |
onChanged: (value) { | |
list[index].val = value!; | |
final _list = list.map<bool>((e) => e.val).toList(); | |
state.didChange(_list); | |
}, | |
), | |
); | |
}, | |
), | |
), | |
if (state.hasError) Text(state.errorText ?? ''), | |
], | |
), | |
), | |
), | |
), | |
); | |
} | |
} | |
class CheckBoxClass { | |
final String text; | |
bool val; | |
CheckBoxClass(this.text, this.val); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment