1- //
21// Software Name: OUDS Flutter
32// SPDX-FileCopyrightText: Copyright (c) Orange SA
43// SPDX-License-Identifier: MIT
@@ -16,45 +15,50 @@ import 'package:ouds_flutter_demo/ui/components/checkbox/checkbox_customization.
1615///
1716/// The CheckboxCodeGenerator class is responsible for dynamically generating Flutter
1817/// code for the customization of a checkbox component. It leverages the checkbox's
19- /// customization state, specifically the enabled and error states , and generates
20- /// the corresponding code in string format, which can be used for rendering or
21- /// previewing the checkbox with the selected properties.
18+ /// customization state, specifically the enabled, error, read-only , and tristate states,
19+ /// and generates the corresponding code in string format, which can be used for rendering
20+ /// or previewing the checkbox with the selected properties.
2221///
2322class CheckboxCodeGenerator {
24- // Static method to generate the code based on checkbox customization state
23+ /// Static method to generate the code based on checkbox customization state.
2524 static String updateCode (BuildContext context, bool indeterminate) {
26- // Get the text value for the checkbox from customization state
27- String value = 'isChecked' ;
28-
29- return """OudsCheckbox(\n value: $value ,\n ${disableCode (context )}\n ${errorCode (context )}${tristateCode (context , indeterminate )}""" ;
30- }
31-
32- // Method to generate the disable code for the checkbox onChanged callback
33- static String disableCode (BuildContext context) {
3425 final CheckboxCustomizationState ? customizationState = CheckboxCustomization .of (context);
3526
36- // Return the onChanged callback code with its enabled or disabled state
37- return "onChanged: ${customizationState ?.hasEnabled == true ? "(bool? value) { \n "
38- "setState(() {\n "
39- "isChecked = value;\n "
40- "});\n }" : 'null' }," ;
41- }
27+ // Base list for building the checkbox code dynamically.
28+ final List <String > code = [];
29+ code.add ('OudsCheckbox(' );
30+ code.add (' value: isChecked,' );
4231
43- // Method to generate the error code for the checkbox
44- static String errorCode (BuildContext context) {
45- final CheckboxCustomizationState ? customizationState = CheckboxCustomization .of (context);
32+ // Add the onChanged callback only when enabled.
33+ if (customizationState? .hasEnabled == true ) {
34+ code.add (' onChanged: (bool? value) {' );
35+ code.add (' setState(() {' );
36+ code.add (' isChecked = value;' );
37+ code.add (' });' );
38+ code.add (' },' );
39+ } else {
40+ code.add (' onChanged: null,' );
41+ }
4642
47- // Return the isError property based on the customization state
48- return 'isError: ${customizationState ?.hasError == true ? 'true' : 'false' },' ;
49- }
43+ // Add the isError property only when true.
44+ if (customizationState? .hasError == true ) {
45+ code.add (' isError: true,' );
46+ }
47+
48+ // Add the readOnly property only when true.
49+ if (customizationState? .hasReadOnly == true ) {
50+ code.add (' readOnly: true,' );
51+ }
5052
51- // Method to generate the tristate code for the checkbox
52- static String tristateCode (BuildContext context, bool indeterminate) {
53- String end = """\n );""" ;
54- if (indeterminate == false ) {
55- return end;
53+ // Add tristate only when indeterminate is true.
54+ if (indeterminate) {
55+ code.add (' tristate: true,' );
5656 }
57- // Return the tristate property based on the indeterminate state
58- return "\n tristate: $indeterminate , $end " ;
57+
58+ // End of the widget declaration.
59+ code.add (');' );
60+
61+ // Return the formatted code as a single string.
62+ return code.join ('\n ' );
5963 }
6064}
0 commit comments