Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
F
Front-End
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
abdullh.alsoleman
Front-End
Commits
8ffe7c56
Unverified
Commit
8ffe7c56
authored
Jun 21, 2021
by
Karol
Committed by
GitHub
Jun 21, 2021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix painting material toggle (#78733) (#78744)
parent
a1a360bf
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
109 additions
and
13 deletions
+109
-13
AUTHORS
AUTHORS
+1
-0
switch.dart
packages/flutter/lib/src/material/switch.dart
+57
-13
switch_test.dart
packages/flutter/test/material/switch_test.dart
+51
-0
No files found.
AUTHORS
View file @
8ffe7c56
...
...
@@ -80,3 +80,4 @@ Ludwik Trammer <ludwik@gmail.com>
Marian Triebe <m.triebe@live.de>
Alexis Rouillard <contact@arouillard.fr>
Mirko Mucaria <skogsfrae@gmail.com>
Karol Czeryna <karol.czeryna@gmail.com>
packages/flutter/lib/src/material/switch.dart
View file @
8ffe7c56
...
...
@@ -912,26 +912,69 @@ class _SwitchPainter extends ToggleablePainter {
?
(
currentValue
<
0.5
?
onInactiveThumbImageError
:
onActiveThumbImageError
)
:
onInactiveThumbImageError
;
// Paint the track
final
Paint
paint
=
Paint
()
..
color
=
trackColor
;
const
double
trackHorizontalPadding
=
kRadialReactionRadius
-
_kTrackRadius
;
final
Offset
trackPaintOffset
=
_computeTrackPaintOffset
(
size
,
_kTrackWidth
,
_kTrackHeight
);
final
Offset
thumbPaintOffset
=
_computeThumbPaintOffset
(
trackPaintOffset
,
visualPosition
);
final
Offset
radialReactionOrigin
=
Offset
(
thumbPaintOffset
.
dx
+
_kThumbRadius
,
size
.
height
/
2
);
_paintTrackWith
(
canvas
,
paint
,
trackPaintOffset
);
paintRadialReaction
(
canvas:
canvas
,
origin:
radialReactionOrigin
);
_paintThumbWith
(
thumbPaintOffset
,
canvas
,
currentValue
,
thumbColor
,
thumbImage
,
thumbErrorListener
,
);
}
/// Computes canvas offset for track's upper left corner
Offset
_computeTrackPaintOffset
(
Size
canvasSize
,
double
trackWidth
,
double
trackHeight
)
{
final
double
horizontalOffset
=
(
canvasSize
.
width
-
_kTrackWidth
)
/
2.0
;
final
double
verticalOffset
=
(
canvasSize
.
height
-
_kTrackHeight
)
/
2.0
;
return
Offset
(
horizontalOffset
,
verticalOffset
);
}
/// Computes canvas offset for thumb's upper left corner as if it were a
/// square
Offset
_computeThumbPaintOffset
(
Offset
trackPaintOffset
,
double
visualPosition
)
{
// How much thumb radius extends beyond the track
const
double
additionalThumbRadius
=
_kThumbRadius
-
_kTrackRadius
;
final
double
horizontalProgress
=
visualPosition
*
trackInnerLength
;
final
double
thumbHorizontalOffset
=
trackPaintOffset
.
dx
-
additionalThumbRadius
+
horizontalProgress
;
final
double
thumbVerticalOffset
=
trackPaintOffset
.
dy
-
additionalThumbRadius
;
return
Offset
(
thumbHorizontalOffset
,
thumbVerticalOffset
);
}
void
_paintTrackWith
(
Canvas
canvas
,
Paint
paint
,
Offset
trackPaintOffset
)
{
final
Rect
trackRect
=
Rect
.
fromLTWH
(
track
HorizontalPadding
,
(
size
.
height
-
_kTrackHeight
)
/
2.0
,
size
.
width
-
2.0
*
trackHorizontalPadding
,
track
PaintOffset
.
dx
,
trackPaintOffset
.
dy
,
_kTrackWidth
,
_kTrackHeight
,
);
final
RRect
trackRRect
=
RRect
.
fromRectAndRadius
(
trackRect
,
const
Radius
.
circular
(
_kTrackRadius
));
canvas
.
drawRRect
(
trackRRect
,
paint
);
final
Offset
thumbPosition
=
Offset
(
kRadialReactionRadius
+
visualPosition
*
trackInnerLength
,
size
.
height
/
2.0
,
final
RRect
trackRRect
=
RRect
.
fromRectAndRadius
(
trackRect
,
const
Radius
.
circular
(
_kTrackRadius
),
);
paintRadialReaction
(
canvas:
canvas
,
origin:
thumbPosition
);
canvas
.
drawRRect
(
trackRRect
,
paint
);
}
void
_paintThumbWith
(
Offset
thumbPaintOffset
,
Canvas
canvas
,
double
currentValue
,
Color
thumbColor
,
ImageProvider
?
thumbImage
,
ImageErrorListener
?
thumbErrorListener
,
)
{
try
{
_isPainting
=
true
;
if
(
_cachedThumbPainter
==
null
||
thumbColor
!=
_cachedThumbColor
||
thumbImage
!=
_cachedThumbImage
||
thumbErrorListener
!=
_cachedThumbErrorListener
)
{
...
...
@@ -946,9 +989,10 @@ class _SwitchPainter extends ToggleablePainter {
// The thumb contracts slightly during the animation
final
double
inset
=
1.0
-
(
currentValue
-
0.5
).
abs
()
*
2.0
;
final
double
radius
=
_kThumbRadius
-
inset
;
thumbPainter
.
paint
(
canvas
,
thumbP
osition
-
Offset
(
radius
,
radius
),
thumbP
aintOffset
+
Offset
(
0
,
inset
),
configuration
.
copyWith
(
size:
Size
.
fromRadius
(
radius
)),
);
}
finally
{
...
...
packages/flutter/test/material/switch_test.dart
View file @
8ffe7c56
...
...
@@ -92,6 +92,57 @@ void main() {
expect
(
tester
.
getSize
(
find
.
byType
(
Switch
)),
const
Size
(
59.0
,
40.0
));
});
testWidgets
(
'Switch does not get distorted upon changing constraints with parent'
,
(
WidgetTester
tester
)
async
{
const
double
maxWidth
=
300
;
const
double
maxHeight
=
100
;
const
ValueKey
<
String
>
boundaryKey
=
ValueKey
<
String
>(
'switch container'
);
Widget
buildSwitch
({
required
double
width
,
required
double
height
})
{
return
MaterialApp
(
home:
Scaffold
(
body:
Directionality
(
textDirection:
TextDirection
.
ltr
,
child:
SizedBox
(
width:
maxWidth
,
height:
maxHeight
,
child:
RepaintBoundary
(
key:
boundaryKey
,
child:
SizedBox
(
width:
width
,
height:
height
,
child:
Switch
(
dragStartBehavior:
DragStartBehavior
.
down
,
value:
true
,
onChanged:
(
_
)
{},
),
),
),
),
),
),
);
}
await
tester
.
pumpWidget
(
buildSwitch
(
width:
maxWidth
,
height:
maxHeight
,
));
await
expectLater
(
find
.
byKey
(
boundaryKey
),
matchesGoldenFile
(
'switch_test.big.on.png'
),
);
await
tester
.
pumpWidget
(
buildSwitch
(
width:
20
,
height:
10
,
));
await
expectLater
(
find
.
byKey
(
boundaryKey
),
matchesGoldenFile
(
'switch_test.small.on.png'
),
);
});
testWidgets
(
'Switch can drag (LTR)'
,
(
WidgetTester
tester
)
async
{
bool
value
=
false
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment