FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
flutter/packages/flutter/lib/src/widgets/_web_image_web.dart at master · flutter/flutter · GitHub
flutter
flutter
Repository navigation
Code
Issues
5k+
(5k+)
Pull requests
723
(723)
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
flutter
/
packages
/
flutter
/
lib
/
src
/
widgets
/
_web_image_web.dart
Copy path
More file actions
More file actions
Latest commit
History
History
History
491 lines (439 loc) · 16.6 KB
Breadcrumbs
flutter
/
packages
/
flutter
/
lib
/
src
/
widgets
/
_web_image_web.dart
Copy path
File metadata and controls
491 lines (439 loc) · 16.6 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/// @docImport 'package:flutter/rendering.dart';
library
;
import
'dart:ui_web'
as
ui_web;
import
'package:flutter/foundation.dart'
;
import
'../painting/_web_image_info_web.dart'
;
import
'../rendering/box.dart'
;
import
'../rendering/platform_view.dart'
;
import
'../rendering/shifted_box.dart'
;
import
'../web.dart'
as
web;
import
'basic.dart'
;
import
'framework.dart'
;
import
'platform_view.dart'
;
/// Displays an
`<img>`
element with
`src`
set to
[src]
.
class
ImgElementPlatformView
extends
StatefulWidget
{
/// Creates a platform view backed with an
`<img>`
element.
ImgElementPlatformView
(
this
.src, {
super
.key}) {
if
(
!
_registered) {
_register
();
}
}
static
const
String
_viewType
=
'Flutter__ImgElementImage__'
;
static
bool
_registered
=
false
;
static
void
_register
() {
assert
(
!
_registered);
_registered
=
true
;
ui_web.platformViewRegistry.
registerViewFactory
(_viewType, (
int
viewId, {
Object
?
params}) {
// Create a new <img> element. The browser is able to display the image
// without fetching it over the network again.
final
img
=
web.document.
createElement
(
'img'
)
as
web.
HTMLImageElement
;
// Set `width` and `height`, otherwise the engine will issue a warning.
img.style
..width
=
'100%'
..height
=
'100%'
..pointerEvents
=
'none'
;
return
img;
});
}
/// The
`src`
URL for the
`<img>`
tag.
final
String
?
src;
@override
State
<
ImgElementPlatformView
>
createState
()
=>
_ImgElementPlatformViewState
();
}
class
_ImgElementPlatformViewState
extends
State
<
ImgElementPlatformView
> {
web.
HTMLImageElement
?
_imgElement;
void
_onPlatformViewCreated
(
int
viewId) {
final
img
=
ui_web.platformViewRegistry.
getViewById
(viewId)
as
web.
HTMLImageElement
;
// Platform view creation is asynchronous. If this State was already
// disposed (or widget.src became null) before the creation callback fired,
// clear the <img> element's src immediately.
if
(
!
mounted
||
widget.src
==
null
) {
img.src
=
''
;
return
;
}
_imgElement
?
.src
=
''
;
_imgElement
=
img;
img.src
=
widget.src
!
;
}
@override
void
didUpdateWidget
(
ImgElementPlatformView
oldWidget) {
super
.
didUpdateWidget
(oldWidget);
if
(widget.src
!=
oldWidget.src) {
if
(widget.src
==
null
) {
_imgElement
?
.src
=
''
;
_imgElement
=
null
;
}
else
{
_imgElement
?
.src
=
widget.src
!
;
}
}
}
@override
void
dispose
() {
// Clear the src attribute when the platform view is disposed so the browser
// can eagerly reclaim the decoded image buffer.
_imgElement
?
.src
=
''
;
_imgElement
=
null
;
super
.
dispose
();
}
@override
Widget
build
(
BuildContext
context) {
if
(widget.src
==
null
) {
return
const
SizedBox
.
expand
();
}
return
HtmlElementView
(
viewType
:
ImgElementPlatformView
._viewType,
onPlatformViewCreated
:
_onPlatformViewCreated,
hitTestBehavior
:
PlatformViewHitTestBehavior
.transparent,
);
}
}
/// A widget which displays and lays out an underlying HTML element in a
/// platform view.
class
RawWebImage
extends
SingleChildRenderObjectWidget
{
/// Creates a
[RawWebImage]
.
RawWebImage
({
super
.key,
required
this
.image,
this
.debugImageLabel,
this
.width,
this
.height,
this
.fit,
this
.alignment
=
Alignment
.center,
this
.matchTextDirection
=
false
,
})
:
super
(child
:
ImgElementPlatformView
(image.htmlImage.src));
/// The underlying HTML element to be displayed.
final
WebImageInfo
image;
/// A debug label explaining the image.
final
String
?
debugImageLabel;
/// The requested width for this widget.
final
double
?
width;
/// The requested height for this widget.
final
double
?
height;
/// How the HTML element should be inscribed in the box constraining it.
final
BoxFit
?
fit;
/// How the image should be aligned in the box constraining it.
final
AlignmentGeometry
alignment;
/// Whether or not the alignment of the image should match the text direction.
final
bool
matchTextDirection;
@override
RenderObject
createRenderObject
(
BuildContext
context) {
return
RenderWebImage
(
image
:
image.htmlImage,
width
:
width,
height
:
height,
fit
:
fit,
alignment
:
alignment,
matchTextDirection
:
matchTextDirection,
textDirection
:
matchTextDirection
||
alignment
is
!
Alignment
?
Directionality
.
of
(context)
:
null
,
);
}
@override
void
updateRenderObject
(
BuildContext
context,
RenderWebImage
renderObject) {
renderObject
..image
=
image.htmlImage
..width
=
width
..height
=
height
..fit
=
fit
..alignment
=
alignment
..matchTextDirection
=
matchTextDirection
..textDirection
=
matchTextDirection
||
alignment
is
!
Alignment
?
Directionality
.
of
(context)
:
null
;
}
}
/// Lays out and positions the child HTML element similarly to
[RenderImage]
.
class
RenderWebImage
extends
RenderShiftedBox
{
/// Creates a new
[RenderWebImage]
.
RenderWebImage
({
RenderBox
?
child,
required
this
._image,
this
._width,
this
._height,
this
._fit,
this
._alignment
=
Alignment
.center,
this
._matchTextDirection
=
false
,
this
._textDirection,
})
:
super
(child);
Alignment
?
_resolvedAlignment;
bool
?
_flipHorizontally;
bool
_needsClip
=
false
;
void
_resolve
() {
if
(_resolvedAlignment
!=
null
) {
return
;
}
_resolvedAlignment
=
alignment.
resolve
(textDirection);
_flipHorizontally
=
matchTextDirection
&&
textDirection
==
TextDirection
.rtl;
}
void
_markNeedResolution
() {
_resolvedAlignment
=
null
;
_flipHorizontally
=
null
;
markNeedsLayout
();
}
/// Whether to paint the image in the direction of the
[TextDirection]
.
///
/// If this is true, then in
[TextDirection.ltr]
contexts, the image will be
/// drawn with its origin in the top left (the "normal" painting direction for
/// images); and in
[TextDirection.rtl]
contexts, the image will be drawn with
/// a scaling factor of -1 in the horizontal direction so that the origin is
/// in the top right.
///
/// This is occasionally used with images in right-to-left environments, for
/// images that were designed for left-to-right locales. Be careful, when
/// using this, to not flip images with integral shadows, text, or other
/// effects that will look incorrect when flipped.
///
/// If this is set to true,
[textDirection]
must not be null.
bool
get
matchTextDirection
=>
_matchTextDirection;
bool
_matchTextDirection;
set
matchTextDirection
(
bool
value) {
if
(value
==
_matchTextDirection) {
return
;
}
_matchTextDirection
=
value;
_markNeedResolution
();
}
/// The text direction with which to resolve
[alignment]
.
///
/// This may be changed to null, but only after the
[alignment]
and
///
[matchTextDirection]
properties have been changed to values that do not
/// depend on the direction.
TextDirection
?
get
textDirection
=>
_textDirection;
TextDirection
?
_textDirection;
set
textDirection
(
TextDirection
?
value) {
if
(_textDirection
==
value) {
return
;
}
_textDirection
=
value;
_markNeedResolution
();
}
/// The image to display.
web.
HTMLImageElement
get
image
=>
_image;
web.
HTMLImageElement
_image;
set
image
(web.
HTMLImageElement
value) {
if
(value
==
_image) {
return
;
}
// If we get a clone of our image, it's the same underlying native data -
// return early.
if
(value.src
==
_image.src) {
return
;
}
final
bool
sizeChanged
=
_image.naturalWidth
!=
value.naturalWidth
||
_image.naturalHeight
!=
value.naturalHeight;
_image
=
value;
markNeedsPaint
();
if
(sizeChanged
&&
(_width
==
null
||
_height
==
null
)) {
markNeedsLayout
();
}
}
/// If non-null, requires the image to have this width.
///
/// If null, the image will pick a size that best preserves its intrinsic
/// aspect ratio.
double
?
get
width
=>
_width;
double
?
_width;
set
width
(
double
?
value) {
if
(value
==
_width) {
return
;
}
_width
=
value;
markNeedsLayout
();
}
/// If non-null, require the image to have this height.
///
/// If null, the image will pick a size that best preserves its intrinsic
/// aspect ratio.
double
?
get
height
=>
_height;
double
?
_height;
set
height
(
double
?
value) {
if
(value
==
_height) {
return
;
}
_height
=
value;
markNeedsLayout
();
}
/// How to inscribe the image into the space allocated during layout.
///
/// The default varies based on the other fields. See the discussion at
///
[paintImage]
.
BoxFit
?
get
fit
=>
_fit;
BoxFit
?
_fit;
set
fit
(
BoxFit
?
value) {
if
(value
==
_fit) {
return
;
}
_fit
=
value;
markNeedsLayout
();
}
/// How to align the image within its bounds.
///
/// If this is set to a text-direction-dependent value,
[textDirection]
must
/// not be null.
AlignmentGeometry
get
alignment
=>
_alignment;
AlignmentGeometry
_alignment;
set
alignment
(
AlignmentGeometry
value) {
if
(value
==
_alignment) {
return
;
}
_alignment
=
value;
_markNeedResolution
();
}
/// Find a size for the render image within the given constraints.
///
/// - The dimensions of the RenderImage must fit within the constraints.
/// - The aspect ratio of the RenderImage matches the intrinsic aspect
///
ratio of the image.
/// - The RenderImage's dimension are maximal subject to being smaller than
///
the intrinsic size of the image.
Size
_sizeForConstraints
(
BoxConstraints
constraints) {
// Folds the given |width| and |height| into |constraints| so they can all
// be treated uniformly.
constraints
=
BoxConstraints
.
tightFor
(width
:
_width, height
:
_height).
enforce
(constraints);
return
constraints.
constrainSizeAndAttemptToPreserveAspectRatio
(
Size
(_image.naturalWidth.
toDouble
(), _image.naturalHeight.
toDouble
()),
);
}
@override
double
computeMinIntrinsicWidth
(
double
height) {
assert
(height
>=
0.0
);
if
(_width
==
null
&&
_height
==
null
) {
return
0.0
;
}
return
_sizeForConstraints
(
BoxConstraints
.
tightForFinite
(height
:
height)).width;
}
@override
double
computeMaxIntrinsicWidth
(
double
height) {
assert
(height
>=
0.0
);
return
_sizeForConstraints
(
BoxConstraints
.
tightForFinite
(height
:
height)).width;
}
@override
double
computeMinIntrinsicHeight
(
double
width) {
assert
(width
>=
0.0
);
if
(_width
==
null
&&
_height
==
null
) {
return
0.0
;
}
return
_sizeForConstraints
(
BoxConstraints
.
tightForFinite
(width
:
width)).height;
}
@override
double
computeMaxIntrinsicHeight
(
double
width) {
assert
(width
>=
0.0
);
return
_sizeForConstraints
(
BoxConstraints
.
tightForFinite
(width
:
width)).height;
}
@override
bool
hitTestSelf
(
Offset
position)
=>
true
;
@override
@protected
Size
computeDryLayout
(
covariant
BoxConstraints
constraints) {
return
_sizeForConstraints
(constraints);
}
@override
void
performLayout
() {
_resolve
();
assert
(_resolvedAlignment
!=
null
);
assert
(_flipHorizontally
!=
null
);
size
=
_sizeForConstraints
(constraints);
if
(child
==
null
) {
return
;
}
// Size and position the <img> element child.
//
// The `ImgElementPlatformView` which contains the `<img>` element is sized
// independently of the `RenderWebImage`. The `RenderWebImage` needs to
// size and position its child to match the `BoxFit` and `Alignment`
// properties.
//
// The child could be smaller or larger than the `RenderWebImage`. If the
// child is larger, then it will be clipped in the `paint` step.
final
inputSize
=
Size
(image.naturalWidth.
toDouble
(), image.naturalHeight.
toDouble
());
final
BoxFit
resolvedFit
=
fit
??
BoxFit
.scaleDown;
final
FittedSizes
fittedSizes
=
applyBoxFit
(resolvedFit, inputSize, size);
if
(fittedSizes.source
==
inputSize) {
// If `fittedSizes.source` is the entire original image, then the child
// can be sized to `fittedSizes.destination` because there will be no
// clipping and the child will be exactly the destination size.
final
Size
childSize
=
fittedSizes.destination;
child
!
.
layout
(
BoxConstraints
.
tight
(childSize));
// In case the `childSize` is smaller than the layout constraints (for
// example, if the `fit` is `BoxFit.fitWidth` and the resulting child
// size causes it to "letterbox" in the containing box) then the child
// needs to be offset in the containing box.
final
double
halfWidthDelta
=
(size.width
-
childSize.width)
/
2.0
;
final
double
halfHeightDelta
=
(size.height
-
childSize.height)
/
2.0
;
final
double
dx
=
halfWidthDelta
+
(_flipHorizontally
!
?
-
_resolvedAlignment
!
.x
:
_resolvedAlignment
!
.x)
*
halfWidthDelta;
final
double
dy
=
halfHeightDelta
+
_resolvedAlignment
!
.y
*
halfHeightDelta;
final
childParentData
=
child
!
.parentData
!
as
BoxParentData
;
childParentData.offset
=
Offset
(dx, dy);
_needsClip
=
false
;
}
else
{
// Otherwise, the destination box only shows a portion of the image and
// the rest of the image overflows out of the box. The size of the child
// needs to be worked out from the size of the source and destination
// boxes. The offset of the child needs to be worked out from the
// alignment. In the `paint` method, a clip will be applied so that only
// the portion of the child that is in the destination box will be shown.
final
Size
sourceSize
=
fittedSizes.source;
final
Size
destinationSize
=
fittedSizes.destination;
// Only `BoxFit.fill` distorts the aspect ratio of the image. The rest of
// the `BoxFit` values preserve the aspect ratio. Furthermore, if the
// `fit` is `BoxFit.fill` then `fittedSizes.source == inputSize` since the
// entire image is drawn into the box. So the case of `BoxFit.fill` is
// never hit in this branch of the conditional. Therefore, the child
// size should be the source size multiplied by a scalar value. The scale
// can be determined by examining the relative sizes of the `sourceSize`
// and `destinationSize`. Since aspect ratio is preserved, `sourceSize`
// and `destinationSize` must have the same aspect ratio. Therefore, the
// scale to multiply the `inputSize` to get the `childSize` is equal to
// the ratio of the `destinationSize` to the `sourceSize`.
assert
(
(sourceSize.aspectRatio
-
destinationSize.aspectRatio).
abs
()
<
precisionErrorTolerance,
);
final
double
scale
=
destinationSize.width
/
sourceSize.width;
final
Size
childSize
=
inputSize
*
scale;
child
!
.
layout
(
BoxConstraints
.
tight
(childSize));
// Now that the child has been sized, it must also be offset by an
// appropriate amount so that the specified portion of the image is shown
// in the destination box. `Alignment.inscribe` gives the sub-rectangle
// of source size that will be shown in the destination box. Again, the
// source and destination box have the same aspect ratio, so to compute
// the offset for the child, first compute the offset that the original
// sized image would need to put the sub-rectangle given by
// `Alignment.inscribe` at the origin, and then scale that offset by the
// same scale computed above to get the offset for the child.
final
Rect
sourceRect
=
_resolvedAlignment
!
.
inscribe
(sourceSize,
Offset
.zero
&
inputSize);
final
Offset
childOffset
=
Offset
(
-
sourceRect.left,
-
sourceRect.top)
*
scale;
final
childParentData
=
child
!
.parentData
!
as
BoxParentData
;
childParentData.offset
=
childOffset;
_needsClip
=
true
;
}
}
@override
void
paint
(
PaintingContext
context,
Offset
offset) {
if
(child
==
null
) {
return
;
}
if
(_needsClip) {
final
Rect
destinationRect
=
Offset
.zero
&
size;
context.
pushClipRect
(needsCompositing, offset, destinationRect,
super
.paint);
}
else
{
super
.
paint
(context, offset);
}
}
@override
void
debugFillProperties
(
DiagnosticPropertiesBuilder
properties) {
super
.
debugFillProperties
(properties);
properties.
add
(
DiagnosticsProperty
<web.
HTMLImageElement
>(
'image'
, image));
properties.
add
(
DoubleProperty
(
'width'
, width, defaultValue
:
null
));
properties.
add
(
DoubleProperty
(
'height'
, height, defaultValue
:
null
));
properties.
add
(
EnumProperty
<
BoxFit
>(
'fit'
, fit, defaultValue
:
null
));
properties.
add
(
DiagnosticsProperty
<
AlignmentGeometry
>(
'alignment'
, alignment, defaultValue
:
null
),
);
}
}
Back
|
FazBrowse Home
|
New Git URL