Skip to content

Commit 87bf0a8

Browse files
committed
The example code has been optimized
1 parent f7cc3e7 commit 87bf0a8

File tree

1 file changed

+44
-76
lines changed

1 file changed

+44
-76
lines changed

examples/material_scroll.py

+44-76
Original file line numberDiff line numberDiff line change
@@ -2,93 +2,52 @@
22
import sys
33

44
from kivy.core.window import Window
5-
from kivy.metrics import dp
6-
from kivy.uix.boxlayout import BoxLayout
7-
from kivy.lang import Builder
85
from kivy import __version__ as kv__version__
9-
from kivymd import __version__
6+
from kivy.lang import Builder
7+
from kivy.metrics import dp
8+
109
from kivymd.app import MDApp
10+
from kivymd import __version__
11+
from kivymd.uix.list import (
12+
MDListItem,
13+
MDListItemHeadlineText,
14+
MDListItemSupportingText,
15+
MDListItemLeadingIcon,
16+
)
17+
1118
from materialyoucolor import __version__ as mc__version__
1219

1320
from examples.common_app import CommonApp
1421

1522
MAIN_KV = """
16-
<Item>:
17-
size_hint_y: None
18-
height: "50dp"
19-
text: ""
20-
sub_text: ""
21-
icon: ""
22-
spacing: "5dp"
23-
24-
MDIcon:
25-
icon: root.icon
26-
size_hint: None, 1
27-
width: self.height
28-
29-
BoxLayout:
30-
orientation: "vertical"
31-
32-
MDLabel:
33-
text: root.text
34-
35-
MDLabel:
36-
adaptive_height: True
37-
text: root.sub_text
38-
font_style: "Body"
39-
role: "medium"
40-
shorten: True
41-
shorten_from: "right"
42-
theme_text_color: "Custom"
43-
text_color: app.theme_cls.onSurfaceVariantColor[:-1] + [0.9]
44-
4523
MDScreen:
4624
md_bg_color: app.theme_cls.backgroundColor
4725
48-
BoxLayout:
49-
orientation: "vertical"
26+
MDScrollView:
27+
do_scroll_x: False
5028
51-
MDScrollView:
52-
do_scroll_x: False
29+
MDBoxLayout:
30+
id: main_scroll
31+
orientation: "vertical"
32+
adaptive_height: True
5333
5434
MDBoxLayout:
55-
id: main_scroll
56-
spacing: "20dp"
57-
orientation: "vertical"
58-
adaptive_height: True
59-
padding: "10dp", 0
60-
61-
MDBoxLayout:
62-
adaptive_height: True
63-
64-
MDLabel:
65-
theme_font_size: "Custom"
66-
text: "OS Info"
67-
font_size: "55sp"
68-
adaptive_height: True
69-
padding: "10dp", "20dp", 0, 0
70-
71-
BoxLayout:
72-
orientation: "vertical"
73-
size_hint_x: None
74-
width: dp(70)
75-
padding: 0, "20dp", "10dp", 0
76-
77-
MDIconButton:
78-
on_release: app.open_menu(self)
79-
size_hint: None, None
80-
size: [dp(50)] * 2
81-
icon: "menu"
82-
pos_hint: {"center_x": .8, "center_y": .9}
83-
84-
Widget:
35+
adaptive_height: True
36+
37+
MDLabel:
38+
theme_font_size: "Custom"
39+
text: "OS Info"
40+
font_size: "55sp"
41+
adaptive_height: True
42+
padding: "10dp", "20dp", 0, 0
43+
44+
MDIconButton:
45+
icon: "menu"
46+
on_release: app.open_menu(self)
47+
pos_hint: {"center_y": .5}
8548
"""
8649

8750

88-
class Item(BoxLayout):
89-
pass
90-
91-
9251
class Example(MDApp, CommonApp):
9352
def build(self):
9453
self.theme_cls.theme_style = "Dark"
@@ -124,11 +83,20 @@ def on_start(self):
12483
pass
12584

12685
for info_item in info:
127-
widget = Item()
128-
widget.text = info_item
129-
widget.sub_text = str(info[info_item][0])
130-
widget.icon = info[info_item][1]
131-
self.root.ids.main_scroll.add_widget(widget)
86+
self.root.ids.main_scroll.add_widget(
87+
MDListItem(
88+
MDListItemLeadingIcon(
89+
icon=info[info_item][1],
90+
),
91+
MDListItemHeadlineText(
92+
text=info_item,
93+
),
94+
MDListItemSupportingText(
95+
text=str(info[info_item][0]),
96+
),
97+
pos_hint={"center_x": .5, "center_y": .5},
98+
)
99+
)
132100

133101
Window.size = [dp(350), dp(600)]
134102

0 commit comments

Comments
 (0)