Skip to content

Commit f2d14b4

Browse files
author
Ahmed Amine Zribi
committed
Merge branch '49-create-component---switch' of github.com:Orange-OpenSource/ouds-flutter into 49-create-component---switch
2 parents c3a3573 + f850a7f commit f2d14b4

File tree

6 files changed

+121
-99
lines changed

6 files changed

+121
-99
lines changed

app/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
## [Unreleased](https://github.com/Orange-OpenSource/ouds-flutter/compare/0.3.0...develop)
8+
### Added
9+
- [DemoApp][Library] Create component - `Switch` ([#49](https://github.com/Orange-OpenSource/ouds-flutter/issues/47))
810

911
### Fixed
10-
1112
- [DemoApp][Library] Token `size` values now adapt based on device type ([#218](https://github.com/Orange-OpenSource/ouds-flutter/issues/218))
1213
- [DemoApp] Color order mismatch in `Divider` component ([#216](https://github.com/Orange-OpenSource/ouds-flutter/issues/216))
1314

ouds_core/lib/components/checkbox/ouds_checkbox_item.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import 'package:flutter/material.dart';
22
import 'package:ouds_core/components/checkbox/ouds_checkbox.dart';
33
import 'package:ouds_core/components/control/ouds_control_item.dart';
4+
import 'package:ouds_core/components/switch/ouds_switch_item.dart';
45

56
///
67
/// <a href="https://unified-design-system.orange.com/472794e18/p/23f1c1-checkbox" class="external" target="_blank">OUDS Checkbox design guidelines</a>
@@ -102,7 +103,7 @@ class OudsCheckboxItem extends StatelessWidget {
102103
error: isError,
103104
readOnly: readOnly,
104105
errorComponentName: "OudsCheckboxItem",
105-
componentName: "OudsCheckboxItem",
106+
componentType: OudsControlItemType.checkbox,
106107
divider: divider,
107108
reversed: reversed,
108109
onTap: onChanged != null

ouds_core/lib/components/control/ouds_control_item.dart

Lines changed: 48 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
// Software Name: OUDS Flutter
2+
// SPDX-FileCopyrightText: Copyright (c) Orange SA
3+
// SPDX-License-Identifier: MIT
4+
//
5+
// This software is distributed under the MIT license,
6+
// the text of which is available at https://opensource.org/license/MIT/
7+
// or see the "LICENSE" file for more details.
8+
//
9+
// Software description: Flutter library of reusable graphical components
10+
//
11+
112
import 'package:flutter/material.dart';
213
import 'package:flutter_svg/flutter_svg.dart';
314
import 'package:ouds_core/components/control/internal/controller/ouds_interaction_state_controller.dart';
@@ -8,6 +19,12 @@ import 'package:ouds_core/components/control/internal/modifier/ouds_control_text
819
import 'package:ouds_core/components/control/internal/ouds_control_state.dart';
920
import 'package:ouds_theme_contract/ouds_theme.dart';
1021

22+
enum OudsControlItemType {
23+
switchButton,
24+
checkbox,
25+
radio,
26+
}
27+
1128
/// Refactor of controls for [Checkbox], [Switch], and [RadioButton].
1229
/// This implementation provides a customizable control item with properties such as text, icon, and interaction states.
1330
/// It manages its own interaction state and can respond to tap events if not in read-only mode.
@@ -22,7 +39,7 @@ class OudsControlItem extends StatefulWidget {
2239
final bool readOnly;
2340
final bool error;
2441
final String errorComponentName;
25-
final String componentName;
42+
final OudsControlItemType componentType;
2643
final Widget Function() indicator;
2744
final String? additionalText;
2845

@@ -32,7 +49,7 @@ class OudsControlItem extends StatefulWidget {
3249
super.key,
3350
required this.text,
3451
required this.errorComponentName,
35-
required this.componentName,
52+
required this.componentType,
3653
required this.indicator,
3754
this.helperText,
3855
this.icon,
@@ -187,8 +204,12 @@ class OudsControlItemState extends State<OudsControlItem> {
187204
),
188205
alignment: Alignment.center,
189206
child: SizedBox(
190-
height: widget.componentName != "OudsSwitchButtonItem" ? OudsTheme.of(context).componentsTokens(context).controlItem.sizeLoader : null,
191-
width: widget.componentName != "OudsSwitchButtonItem" ? OudsTheme.of(context).componentsTokens(context).controlItem.sizeLoader : null,
207+
height: widget.componentType != OudsControlItemType.switchButton
208+
? OudsTheme.of(context).componentsTokens(context).controlItem.sizeLoader
209+
: null,
210+
width: widget.componentType != OudsControlItemType.switchButton
211+
? OudsTheme.of(context).componentsTokens(context).controlItem.sizeLoader
212+
: null,
192213
child: widget.indicator(),
193214
),
194215
),
@@ -249,8 +270,12 @@ class OudsControlItemState extends State<OudsControlItem> {
249270
),
250271
alignment: Alignment.center,
251272
child: SizedBox(
252-
height: widget.componentName != "OudsSwitchButtonItem" ? OudsTheme.of(context).componentsTokens(context).controlItem.sizeLoader : null,
253-
width: widget.componentName != "OudsSwitchButtonItem" ? OudsTheme.of(context).componentsTokens(context).controlItem.sizeLoader : null,
273+
height: widget.componentType != OudsControlItemType.switchButton
274+
? OudsTheme.of(context).componentsTokens(context).controlItem.sizeLoader
275+
: null,
276+
width: widget.componentType != OudsControlItemType.switchButton
277+
? OudsTheme.of(context).componentsTokens(context).controlItem.sizeLoader
278+
: null,
254279
child: widget.indicator(),
255280
),
256281
),
@@ -265,39 +290,33 @@ class OudsControlItemState extends State<OudsControlItem> {
265290
final List<Widget> columnChildren = [
266291
Text(
267292
widget.text,
268-
style: TextStyle(
269-
fontSize: OudsTheme.of(context).fontTokens.sizeLabelLarge,
270-
letterSpacing: OudsTheme.of(context).fontTokens.letterSpacingLabelLarge,
271-
fontWeight: OudsTheme.of(context).fontTokens.weightLabelDefault,
272-
color: controlItemTextModifier.getTextColor(controlItemState, widget.error),
273-
),
293+
style: OudsTheme.of(context).typographyTokens.typeLabelDefaultLarge(context).copyWith(
294+
color: controlItemTextModifier.getTextColor(controlItemState, widget.error),
295+
),
274296
),
275297
];
276-
277298
if (hasAdditionalText) {
278299
columnChildren.add(SizedBox(height: OudsTheme.of(context).componentsTokens(context).controlItem.spaceRowGap));
279-
columnChildren.add(Text(
280-
widget.additionalText!,
281-
style: TextStyle(
282-
fontSize: OudsTheme.of(context).fontTokens.sizeLabelMedium,
283-
letterSpacing: OudsTheme.of(context).fontTokens.letterSpacingLabelMedium,
284-
fontWeight: OudsTheme.of(context).fontTokens.weightStrong,
285-
color: controlItemTextModifier.getAdditionalTextColor(controlItemState),
300+
columnChildren.add(
301+
Text(
302+
widget.additionalText!,
303+
style: OudsTheme.of(context).typographyTokens.typeLabelStrongMedium(context).copyWith(
304+
color: controlItemTextModifier.getAdditionalTextColor(controlItemState),
305+
),
286306
),
287-
));
307+
);
288308
}
289309

290310
if (hasHelperText) {
291311
columnChildren.add(SizedBox(height: OudsTheme.of(context).componentsTokens(context).controlItem.spaceRowGap));
292-
columnChildren.add(Text(
293-
widget.helperText!,
294-
style: TextStyle(
295-
fontSize: OudsTheme.of(context).fontTokens.sizeLabelMedium,
296-
letterSpacing: OudsTheme.of(context).fontTokens.letterSpacingLabelMedium,
297-
fontWeight: OudsTheme.of(context).fontTokens.weightBodyDefault,
298-
color: controlItemTextModifier.getHelperTextColor(controlItemState),
312+
columnChildren.add(
313+
Text(
314+
widget.helperText!,
315+
style: OudsTheme.of(context).typographyTokens.typeLabelDefaultMedium(context).copyWith(
316+
color: controlItemTextModifier.getHelperTextColor(controlItemState),
317+
),
299318
),
300-
));
319+
);
301320
}
302321

303322
return Expanded(

ouds_core/lib/components/radio_button/ouds_radio_button_item.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import 'package:flutter/material.dart';
1515
import 'package:ouds_core/components/control/ouds_control_item.dart';
1616
import 'package:ouds_core/components/radio_button/ouds_radio_button.dart';
17+
import 'package:ouds_core/components/switch/ouds_switch_item.dart';
1718

1819
///
1920
/// [OUDS Radio Button Design Guidelines](https://unified-design-system.orange.com/472794e18/p/90c467-radio-button)
@@ -118,7 +119,7 @@ class OudsRadioButtonItem<T> extends StatelessWidget {
118119
error: isError,
119120
readOnly: readOnly,
120121
errorComponentName: "OudsRadioButtonItem",
121-
componentName: "OudsRadioButtonItem",
122+
componentType: OudsControlItemType.checkbox,
122123
divider: divider,
123124
outlined: _selected == true ? outlined : false,
124125
selected: outlined,

ouds_core/lib/components/switch/ouds_switch.dart

Lines changed: 56 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
/*
2-
* // Software Name: OUDS Flutter
3-
* // SPDX-FileCopyrightText: Copyright (c) Orange SA
4-
* // SPDX-License-Identifier: MIT
5-
* //
6-
* // This software is distributed under the MIT license,
7-
* // the text of which is available at https://opensource.org/license/MIT/
8-
* // or see the "LICENSE" file for more details.
9-
* //
10-
* // Software description: Flutter library of reusable graphical components
11-
* //
12-
*/
1+
// Software Name: OUDS Flutter
2+
// SPDX-FileCopyrightText: Copyright (c) Orange SA
3+
// SPDX-License-Identifier: MIT
4+
//
5+
// This software is distributed under the MIT license,
6+
// the text of which is available at https://opensource.org/license/MIT/
7+
// or see the "LICENSE" file for more details.
8+
//
9+
// Software description: Flutter library of reusable graphical components
10+
//
11+
1312
import 'package:flutter/material.dart';
1413
import 'package:flutter_svg/flutter_svg.dart';
1514
import 'package:ouds_core/components/control/internal/interaction/ouds_inherited_interaction_model.dart';
@@ -89,49 +88,52 @@ class _OudsSwitchState extends State<OudsSwitch> {
8988
final switchTickModifier = OudsControlTickModifier(context);
9089
final switchButton = OudsTheme.of(context).componentsTokens(context).switchButton;
9190

92-
return Semantics(
93-
enabled: widget.onChanged != null,
94-
toggled: widget.value,
95-
child: Material(
96-
color: Colors.transparent,
97-
child: SizedBox(
98-
width: switchButton.sizeMinWidth,
99-
height: switchButton.sizeMinHeight,
100-
child: InkWell(
101-
onTap: widget.onChanged != null
102-
? () {
103-
bool? newValue;
104-
setState(() {
105-
newValue = !widget.value;
106-
});
107-
widget.onChanged!(newValue!);
108-
}
109-
: null,
110-
splashColor: switchBackgroundModifier.getBackgroundColor(switchState),
111-
highlightColor: switchBackgroundModifier.getBackgroundColor(switchState),
112-
onHover: (hovering) {
113-
setState(() {
114-
_isHovered = hovering;
115-
});
116-
},
117-
onHighlightChanged: (highlighted) {
118-
setState(() {
119-
_isPressed = highlighted;
120-
});
121-
},
122-
onFocusChange: (focused) {
123-
setState(() {
124-
_isFocused = focused;
125-
});
126-
},
127-
child: Container(
128-
width: switchButton.sizeWidthTrack,
129-
height: switchButton.sizeHeightTrack,
130-
decoration: BoxDecoration(
131-
borderRadius: BorderRadius.circular(switchButton.borderRadius),
132-
color: widget.value ? switchTickModifier.getTickSwitchColor(switchState) : switchButton.colorTrackUnselected,
91+
return MergeSemantics(
92+
child: Semantics(
93+
enabled: widget.onChanged != null,
94+
toggled: widget.value,
95+
button: true,
96+
child: Material(
97+
color: Colors.transparent,
98+
child: SizedBox(
99+
width: switchButton.sizeMinWidth,
100+
height: switchButton.sizeMinHeight,
101+
child: InkWell(
102+
onTap: widget.onChanged != null
103+
? () {
104+
bool? newValue;
105+
setState(() {
106+
newValue = !widget.value;
107+
});
108+
widget.onChanged!(newValue!);
109+
}
110+
: null,
111+
splashColor: switchBackgroundModifier.getBackgroundColor(switchState),
112+
highlightColor: switchBackgroundModifier.getBackgroundColor(switchState),
113+
onHover: (hovering) {
114+
setState(() {
115+
_isHovered = hovering;
116+
});
117+
},
118+
onHighlightChanged: (highlighted) {
119+
setState(() {
120+
_isPressed = highlighted;
121+
});
122+
},
123+
onFocusChange: (focused) {
124+
setState(() {
125+
_isFocused = focused;
126+
});
127+
},
128+
child: Container(
129+
width: switchButton.sizeWidthTrack,
130+
height: switchButton.sizeHeightTrack,
131+
decoration: BoxDecoration(
132+
borderRadius: BorderRadius.circular(switchButton.borderRadius),
133+
color: widget.value ? switchTickModifier.getTickSwitchColor(switchState) : switchButton.colorTrackUnselected,
134+
),
135+
child: _buildCursorIndicator(context, switchState, isPressed, isHovered),
133136
),
134-
child: _buildCursorIndicator(context, switchState, isPressed, isHovered),
135137
),
136138
),
137139
),

ouds_core/lib/components/switch/ouds_switch_item.dart

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
/*
2-
* // Software Name: OUDS Flutter
3-
* // SPDX-FileCopyrightText: Copyright (c) Orange SA
4-
* // SPDX-License-Identifier: MIT
5-
* //
6-
* // This software is distributed under the MIT license,
7-
* // the text of which is available at https://opensource.org/license/MIT/
8-
* // or see the "LICENSE" file for more details.
9-
* //
10-
* // Software description: Flutter library of reusable graphical components
11-
* //
12-
*/
1+
// Software Name: OUDS Flutter
2+
// SPDX-FileCopyrightText: Copyright (c) Orange SA
3+
// SPDX-License-Identifier: MIT
4+
//
5+
// This software is distributed under the MIT license,
6+
// the text of which is available at https://opensource.org/license/MIT/
7+
// or see the "LICENSE" file for more details.
8+
//
9+
// Software description: Flutter library of reusable graphical components
10+
//
1311

1412
import 'package:flutter/material.dart';
1513
import 'package:ouds_core/components/control/ouds_control_item.dart';
@@ -102,7 +100,7 @@ class OudsSwitchButtonItem extends StatelessWidget {
102100
error: isError,
103101
readOnly: readOnly,
104102
errorComponentName: "OudsSwitchButtonItem",
105-
componentName: "OudsSwitchButtonItem",
103+
componentType: OudsControlItemType.switchButton,
106104
divider: divider,
107105
reversed: reversed,
108106
onTap: onChanged != null

0 commit comments

Comments
 (0)