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
2a9de0aa
Commit
2a9de0aa
authored
Dec 04, 2015
by
Adam Barth
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add the ability to custom paint foregrounds
parent
a2b8f8b9
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
103 additions
and
32 deletions
+103
-32
proxy_box.dart
packages/flutter/lib/src/rendering/proxy_box.dart
+46
-25
basic.dart
packages/flutter/lib/src/widgets/basic.dart
+10
-6
center_test.dart
packages/unit/test/widget/center_test.dart
+1
-1
custom_paint_test.dart
packages/unit/test/widget/custom_paint_test.dart
+46
-0
No files found.
packages/flutter/lib/src/rendering/proxy_box.dart
View file @
2a9de0aa
...
@@ -938,61 +938,82 @@ abstract class CustomPainter {
...
@@ -938,61 +938,82 @@ abstract class CustomPainter {
void
paint
(
PaintingCanvas
canvas
,
Size
size
);
void
paint
(
PaintingCanvas
canvas
,
Size
size
);
bool
shouldRepaint
(
CustomPainter
oldDelegate
);
bool
shouldRepaint
(
CustomPainter
oldDelegate
);
bool
hitTest
(
Point
position
)
=>
true
;
bool
hitTest
(
Point
position
)
=>
null
;
}
}
/// Delegates its painting
to [onPaint]
/// Delegates its painting
///
///
/// When asked to paint, custom paint first calls its callback with the current
/// When asked to paint, custom paint first asks painter to paint with the
/// canvas and then paints its children. The coodinate system of the canvas
/// current canvas and then paints its children. After painting its children,
/// matches the coordinate system of the custom paint object. The callback is
/// custom paint asks foregroundPainter to paint. The coodinate system of the
/// expected to paint with in a rectangle starting at the origin and
/// canvas matches the coordinate system of the custom paint object. The
/// encompassing a region of the given size. If the callback paints outside
/// painters are expected to paint with in a rectangle starting at the origin
/// and encompassing a region of the given size. If the painters paints outside
/// those bounds, there might be insufficient memory allocated to rasterize the
/// those bounds, there might be insufficient memory allocated to rasterize the
/// painting commands and the resulting behavior is undefined.
/// painting commands and the resulting behavior is undefined.
///
///
/// Note: Custom paint calls its
callback
during paint, which means you cannot
/// Note: Custom paint calls its
painters
during paint, which means you cannot
/// dirty layout or paint information during the callback.
/// dirty layout or paint information during the callback.
class
RenderCustomPaint
extends
RenderProxyBox
{
class
RenderCustomPaint
extends
RenderProxyBox
{
RenderCustomPaint
({
RenderCustomPaint
({
CustomPainter
painter
,
CustomPainter
painter
,
CustomPainter
foregroundPainter
,
RenderBox
child
RenderBox
child
})
:
_painter
=
painter
,
super
(
child
)
{
})
:
_painter
=
painter
,
_foregroundPainter
=
foregroundPainter
,
super
(
child
);
assert
(
painter
!=
null
);
}
CustomPainter
get
painter
=>
_painter
;
CustomPainter
get
painter
=>
_painter
;
CustomPainter
_painter
;
CustomPainter
_painter
;
void
set
painter
(
CustomPainter
newPainter
)
{
void
set
painter
(
CustomPainter
newPainter
)
{
assert
(
newPainter
!=
null
||
!
attached
);
if
(
_painter
==
newPainter
)
if
(
_painter
==
newPainter
)
return
;
return
;
CustomPainter
oldPainter
=
_painter
;
CustomPainter
oldPainter
=
_painter
;
_painter
=
newPainter
;
_painter
=
newPainter
;
if
(
newPainter
==
null
)
_checkForRepaint
(
_painter
,
oldPainter
);
}
CustomPainter
get
foregroundPainter
=>
_foregroundPainter
;
CustomPainter
_foregroundPainter
;
void
set
foregroundPainter
(
CustomPainter
newPainter
)
{
if
(
_foregroundPainter
==
newPainter
)
return
;
return
;
if
(
oldPainter
==
null
CustomPainter
oldPainter
=
_foregroundPainter
;
||
newPainter
.
runtimeType
!=
oldPainter
.
runtimeType
_foregroundPainter
=
newPainter
;
||
newPainter
.
shouldRepaint
(
oldPainter
))
_checkForRepaint
(
_foregroundPainter
,
oldPainter
);
}
void
_checkForRepaint
(
CustomPainter
newPainter
,
CustomPainter
oldPainter
)
{
if
(
newPainter
==
null
)
{
assert
(
oldPainter
!=
null
);
// We should be called only for changes.
markNeedsPaint
();
}
else
if
(
oldPainter
==
null
||
newPainter
.
runtimeType
!=
oldPainter
.
runtimeType
||
newPainter
.
shouldRepaint
(
oldPainter
))
{
markNeedsPaint
();
markNeedsPaint
();
}
}
}
void
attach
()
{
bool
hitTestChildren
(
HitTestResult
result
,
{
Point
position
})
{
assert
(
_painter
!=
null
);
if
(
_foregroundPainter
!=
null
&&
(
_foregroundPainter
.
hitTest
(
position
)
??
false
))
super
.
attach
();
return
true
;
return
super
.
hitTestChildren
(
result
,
position:
position
);
}
}
bool
hitTestSelf
(
Point
position
)
{
bool
hitTestSelf
(
Point
position
)
{
return
_painter
.
hitTest
(
position
);
return
_painter
!=
null
&&
(
_painter
.
hitTest
(
position
)
??
true
);
}
void
_paintWithPainter
(
Canvas
canvas
,
Offset
offset
,
CustomPainter
painter
)
{
canvas
.
translate
(
offset
.
dx
,
offset
.
dy
);
painter
.
paint
(
canvas
,
size
);
canvas
.
translate
(-
offset
.
dx
,
-
offset
.
dy
);
}
}
void
paint
(
PaintingContext
context
,
Offset
offset
)
{
void
paint
(
PaintingContext
context
,
Offset
offset
)
{
assert
(
_painter
!=
null
);
if
(
_painter
!=
null
)
context
.
canvas
.
translate
(
offset
.
dx
,
offset
.
dy
);
_paintWithPainter
(
context
.
canvas
,
offset
,
_painter
);
_painter
.
paint
(
context
.
canvas
,
size
);
context
.
canvas
.
translate
(-
offset
.
dx
,
-
offset
.
dy
);
super
.
paint
(
context
,
offset
);
super
.
paint
(
context
,
offset
);
if
(
_foregroundPainter
!=
null
)
_paintWithPainter
(
context
.
canvas
,
offset
,
_foregroundPainter
);
}
}
}
}
...
...
packages/flutter/lib/src/widgets/basic.dart
View file @
2a9de0aa
...
@@ -137,21 +137,25 @@ class DecoratedBox extends OneChildRenderObjectWidget {
...
@@ -137,21 +137,25 @@ class DecoratedBox extends OneChildRenderObjectWidget {
}
}
class
CustomPaint
extends
OneChildRenderObjectWidget
{
class
CustomPaint
extends
OneChildRenderObjectWidget
{
CustomPaint
({
Key
key
,
this
.
painter
,
Widget
child
})
CustomPaint
({
Key
key
,
this
.
painter
,
this
.
foregroundPainter
,
Widget
child
})
:
super
(
key:
key
,
child:
child
)
{
:
super
(
key:
key
,
child:
child
);
assert
(
painter
!=
null
);
}
final
CustomPainter
painter
;
final
CustomPainter
painter
;
final
CustomPainter
foregroundPainter
;
RenderCustomPaint
createRenderObject
()
=>
new
RenderCustomPaint
(
painter:
painter
);
RenderCustomPaint
createRenderObject
()
=>
new
RenderCustomPaint
(
painter:
painter
,
foregroundPainter:
foregroundPainter
);
void
updateRenderObject
(
RenderCustomPaint
renderObject
,
CustomPaint
oldWidget
)
{
void
updateRenderObject
(
RenderCustomPaint
renderObject
,
CustomPaint
oldWidget
)
{
renderObject
.
painter
=
painter
;
renderObject
.
painter
=
painter
;
renderObject
.
foregroundPainter
=
foregroundPainter
;
}
}
void
didUnmountRenderObject
(
RenderCustomPaint
renderObject
)
{
void
didUnmountRenderObject
(
RenderCustomPaint
renderObject
)
{
renderObject
.
painter
=
null
;
renderObject
.
painter
=
null
;
renderObject
.
foregroundPainter
=
null
;
}
}
}
}
...
...
packages/unit/test/widget/center_test.dart
View file @
2a9de0aa
...
@@ -7,7 +7,7 @@ import 'package:flutter/widgets.dart';
...
@@ -7,7 +7,7 @@ import 'package:flutter/widgets.dart';
import
'package:test/test.dart'
;
import
'package:test/test.dart'
;
void
main
(
)
{
void
main
(
)
{
test
(
'Can be placed in an infinte box'
,
()
{
test
(
'Can be placed in an infin
i
te box'
,
()
{
testWidgets
((
WidgetTester
tester
)
{
testWidgets
((
WidgetTester
tester
)
{
tester
.
pumpWidget
(
new
Block
(<
Widget
>[
new
Center
()]));
tester
.
pumpWidget
(
new
Block
(<
Widget
>[
new
Center
()]));
});
});
...
...
packages/unit/test/widget/custom_paint_test.dart
0 → 100644
View file @
2a9de0aa
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import
'package:flutter_test/flutter_test.dart'
;
import
'package:flutter/widgets.dart'
;
import
'package:test/test.dart'
;
class
TestCustomPainter
extends
CustomPainter
{
TestCustomPainter
({
this
.
log
,
this
.
name
});
List
<
String
>
log
;
String
name
;
void
paint
(
Canvas
canvas
,
Size
size
)
{
log
.
add
(
name
);
}
bool
shouldRepaint
(
TestCustomPainter
oldPainter
)
=>
true
;
}
void
main
(
)
{
test
(
'Control test for custom painting'
,
()
{
testWidgets
((
WidgetTester
tester
)
{
List
<
String
>
log
=
<
String
>[];
tester
.
pumpWidget
(
new
CustomPaint
(
painter:
new
TestCustomPainter
(
log:
log
,
name:
'background'
),
foregroundPainter:
new
TestCustomPainter
(
log:
log
,
name:
'foreground'
),
child:
new
CustomPaint
(
painter:
new
TestCustomPainter
(
log:
log
,
name:
'child'
)
)
));
expect
(
log
,
equals
([
'background'
,
'child'
,
'foreground'
]));
});
});
}
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