|
| 1 | +from kivy.lang import Builder |
| 2 | +from kivymd.app import MDApp |
| 3 | +from kivymd.uix.list import MDListItem |
| 4 | +from examples.common_app import CommonApp |
| 5 | +from kivy.properties import StringProperty |
| 6 | +from kivymd.icon_definitions import md_icons |
| 7 | + |
| 8 | +class IconItem(MDListItem): |
| 9 | + icon = StringProperty() |
| 10 | + text = StringProperty() |
| 11 | + |
| 12 | +MAIN_KV = """ |
| 13 | +#: import images_path kivymd.images_path |
| 14 | +
|
| 15 | +<IconItem> |
| 16 | + theme_bg_color:"Custom" |
| 17 | + md_bg_color:[0,0,0,0] |
| 18 | + MDListItemLeadingIcon: |
| 19 | + icon: root.icon |
| 20 | +
|
| 21 | + MDListItemSupportingText: |
| 22 | + text: root.text |
| 23 | +
|
| 24 | +MDScreen: |
| 25 | + md_bg_color:app.theme_cls.backgroundColor |
| 26 | + BoxLayout: |
| 27 | + padding:[dp(10), dp(30), dp(10), dp(10)] |
| 28 | + orientation:"vertical" |
| 29 | + |
| 30 | + MDSearchBar: |
| 31 | + id: search_bar |
| 32 | + supporting_text: "Search in text" |
| 33 | + view_root: root |
| 34 | + on_text: app.set_list_md_icons(text=args[-1], search=True) |
| 35 | + |
| 36 | + # Search Bar items |
| 37 | + MDSearchBarLeadingContainer: |
| 38 | + MDSearchLeadingIcon: |
| 39 | + icon: "menu" |
| 40 | + on_release: app.open_menu(self) |
| 41 | +
|
| 42 | + MDSearchBarTrailingContainer: |
| 43 | + MDSearchTrailingIcon: |
| 44 | + icon:"microphone" |
| 45 | + MDSearchTrailingAvatar: |
| 46 | + source:f"{images_path}/logo/kivymd-icon-128.png" |
| 47 | + |
| 48 | + # Search View |
| 49 | + MDSearchViewLeadingContainer: |
| 50 | + MDSearchLeadingIcon: |
| 51 | + icon:"arrow-left" |
| 52 | + on_release: search_bar.close_view() |
| 53 | +
|
| 54 | + MDSearchViewTrailingContainer: |
| 55 | + MDSearchTrailingIcon: |
| 56 | + icon:"window-close" |
| 57 | + on_release: search_bar.text = "" |
| 58 | +
|
| 59 | + MDSearchViewContainer: |
| 60 | + RecycleView: |
| 61 | + id: rv |
| 62 | + key_viewclass: 'viewclass' |
| 63 | + key_size: 'height' |
| 64 | +
|
| 65 | + RecycleBoxLayout: |
| 66 | + default_size: None, dp(48) |
| 67 | + default_size_hint: 1, None |
| 68 | + size_hint_y: None |
| 69 | + height: self.minimum_height |
| 70 | + orientation: 'vertical' |
| 71 | + Widget: |
| 72 | +
|
| 73 | + BoxLayout: |
| 74 | + size_hint_y:None |
| 75 | + height:dp(30) |
| 76 | + padding:[dp(50), 0] |
| 77 | + spacing:dp(10) |
| 78 | + MDLabel: |
| 79 | + text:"Bar dock" |
| 80 | + halign:"right" |
| 81 | + MDSwitch: |
| 82 | + on_active:search_bar.docked = args[-1] |
| 83 | +""" |
| 84 | + |
| 85 | +class Example(MDApp, CommonApp): |
| 86 | + |
| 87 | + def build(self): |
| 88 | + return Builder.load_string(MAIN_KV) |
| 89 | + |
| 90 | + def on_start(self): |
| 91 | + self.set_list_md_icons() |
| 92 | + |
| 93 | + def set_list_md_icons(self, text="", search=False): |
| 94 | + def add_icon_item(name_icon): |
| 95 | + self.root.ids.rv.data.append( |
| 96 | + { |
| 97 | + "viewclass": "IconItem", |
| 98 | + "icon": name_icon, |
| 99 | + "text": name_icon, |
| 100 | + "callback": lambda x: x, |
| 101 | + } |
| 102 | + ) |
| 103 | + |
| 104 | + self.root.ids.rv.data = [] |
| 105 | + for name_icon in md_icons.keys(): |
| 106 | + if search: |
| 107 | + if text in name_icon: |
| 108 | + add_icon_item(name_icon) |
| 109 | + else: |
| 110 | + add_icon_item(name_icon) |
| 111 | + |
| 112 | +Example().run() |
0 commit comments