Skip to content

Commit 14e9afe

Browse files
committed
Image rounded corners
1 parent 31fa0f1 commit 14e9afe

File tree

3 files changed

+48
-10
lines changed

3 files changed

+48
-10
lines changed

client/lib/controls/image.dart

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,31 @@ class ImageControl extends StatelessWidget {
4444
converter: (store) => store.state.pageUri,
4545
builder: (context, pageUri) {
4646
return baseControl(
47-
Image.network(getAssetUrl(pageUri!, src),
48-
width: width, height: height, repeat: repeat, fit: fit),
47+
_clipCorners(
48+
Image.network(getAssetUrl(pageUri!, src),
49+
width: width, height: height, repeat: repeat, fit: fit),
50+
control),
4951
parent,
5052
control);
5153
});
5254
} else {
5355
return baseControl(
54-
Image.network(src,
55-
width: width, height: height, repeat: repeat, fit: fit),
56+
_clipCorners(
57+
Image.network(src,
58+
width: width, height: height, repeat: repeat, fit: fit),
59+
control),
5660
parent,
5761
control);
5862
}
5963
}
64+
65+
Widget _clipCorners(Widget image, Control control) {
66+
var borderRadius = parseBorderRadius(control, "borderRadius");
67+
return borderRadius != null
68+
? ClipRRect(
69+
borderRadius: borderRadius,
70+
child: image,
71+
)
72+
: image;
73+
}
6074
}

sdk/python/flet/image.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1+
import json
2+
from typing import Optional
3+
14
from beartype import beartype
25

6+
from flet import border_radius
7+
from flet.border_radius import BorderRadius
38
from flet.control import Control, OptionalNumber
9+
from flet.embed_json_encoder import EmbedJsonEncoder
410
from flet.ref import Ref
511

612
try:
@@ -33,6 +39,7 @@ def __init__(
3339
src: str = None,
3440
repeat: ImageRepeat = None,
3541
fit: ImageFit = None,
42+
border_radius: BorderRadius = None,
3643
):
3744

3845
Control.__init__(
@@ -50,6 +57,7 @@ def __init__(
5057
self.src = src
5158
self.fit = fit
5259
self.repeat = repeat
60+
self.border_radius = border_radius
5361

5462
def _get_control_name(self):
5563
return "image"
@@ -102,3 +110,18 @@ def height(self):
102110
@beartype
103111
def height(self, value: OptionalNumber):
104112
self._set_attr("height", value)
113+
114+
# border_radius
115+
@property
116+
def border_radius(self):
117+
return self.__border_radius
118+
119+
@border_radius.setter
120+
@beartype
121+
def border_radius(self, value: Optional[BorderRadius]):
122+
self.__border_radius = value
123+
if value and isinstance(value, (int, float)):
124+
value = border_radius.all(value)
125+
self._set_attr(
126+
"borderRadius", json.dumps(value, cls=EmbedJsonEncoder) if value else None
127+
)

sdk/python/playground/images.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from time import sleep
44

55
import flet
6-
from flet import Column, ElevatedButton, Image, Page, Row, Text, Theme
6+
from flet import Column, ElevatedButton, Image, Page, Row, Text, Theme, border_radius
77
from flet.stack import Stack
88

99
logging.basicConfig(level=logging.DEBUG)
@@ -21,21 +21,22 @@ def main(page: Page):
2121
height=100,
2222
fit="contain",
2323
)
24-
images = Row(expand=1, wrap=True, scroll="always")
24+
images = Row(expand=1, wrap=False, scroll="always")
2525

2626
page.add(img, images)
2727

2828
for i in range(0, 30):
2929
images.controls.append(
3030
Image(
31-
src=f"https://picsum.photos/100/100?{i}",
32-
width=100,
33-
height=100,
31+
src=f"https://picsum.photos/200/200?{i}",
32+
width=200,
33+
height=200,
3434
fit="none",
3535
repeat="noRepeat",
36+
border_radius=border_radius.all(10),
3637
)
3738
)
3839
page.update()
3940

4041

41-
flet.app(name="test1", port=8550, target=main, view=flet.FLET_APP)
42+
flet.app(name="test1", port=8550, target=main, view=flet.WEB_BROWSER)

0 commit comments

Comments
 (0)