11import {
2- SwitchBase , Color , colorProperty , backgroundColorProperty , backgroundInternalProperty , checkedProperty
2+ SwitchBase , Color , colorProperty , backgroundColorProperty , backgroundInternalProperty , checkedProperty , offBackgroundColorProperty
33} from "./switch-common" ;
44
55export * from "./switch-common" ;
@@ -54,6 +54,24 @@ export class Switch extends SwitchBase {
5454 super . disposeNativeView ( ) ;
5555 }
5656
57+ private setNativeBackgroundColor ( value : string | number | Color ) {
58+ if ( value instanceof Color ) {
59+ this . nativeViewProtected . getTrackDrawable ( ) . setColorFilter ( value . android , android . graphics . PorterDuff . Mode . SRC_IN ) ;
60+ } else {
61+ this . nativeViewProtected . getTrackDrawable ( ) . clearColorFilter ( ) ;
62+ }
63+ }
64+
65+ _onCheckedPropertyChanged ( newValue : boolean ) {
66+ if ( this . offBackgroundColor ) {
67+ if ( ! newValue ) {
68+ this . setNativeBackgroundColor ( this . offBackgroundColor ) ;
69+ } else {
70+ this . setNativeBackgroundColor ( this . backgroundColor ) ;
71+ }
72+ }
73+ }
74+
5775 [ checkedProperty . getDefault ] ( ) : boolean {
5876 return false ;
5977 }
@@ -76,10 +94,8 @@ export class Switch extends SwitchBase {
7694 return - 1 ;
7795 }
7896 [ backgroundColorProperty . setNative ] ( value : number | Color ) {
79- if ( value instanceof Color ) {
80- this . nativeViewProtected . getTrackDrawable ( ) . setColorFilter ( value . android , android . graphics . PorterDuff . Mode . SRC_IN ) ;
81- } else {
82- this . nativeViewProtected . getTrackDrawable ( ) . clearColorFilter ( ) ;
97+ if ( ! this . offBackgroundColor || this . checked ) {
98+ this . setNativeBackgroundColor ( value ) ;
8399 }
84100 }
85101
@@ -89,4 +105,13 @@ export class Switch extends SwitchBase {
89105 [ backgroundInternalProperty . setNative ] ( value : any ) {
90106 //
91107 }
108+
109+ [ offBackgroundColorProperty . getDefault ] ( ) : number {
110+ return - 1 ;
111+ }
112+ [ offBackgroundColorProperty . setNative ] ( value : number | Color ) {
113+ if ( ! this . checked ) {
114+ this . setNativeBackgroundColor ( value ) ;
115+ }
116+ }
92117}
0 commit comments