Skip to content

Instantly share code, notes, and snippets.

@kristoffer-paulsson
Last active November 10, 2019 18:13
Show Gist options
  • Save kristoffer-paulsson/35a9d7f9c5bb35603cdc92568232a250 to your computer and use it in GitHub Desktop.
Save kristoffer-paulsson/35a9d7f9c5bb35603cdc92568232a250 to your computer and use it in GitHub Desktop.
Centering widgets in a responsive way within ScrollView.
from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.metrics import dp
from kivy.properties import NumericProperty
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.gridlayout import GridLayout
LIPSUM = """
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
""" # noqa E501
Builder.load_string("""
<ExampleScroll@FloatLayout>:
ScrollView:
do_scroll_x: False
bar_width: 10
bar_color: app.theme_cls.primary_color
bar_color_acrive: app.theme_cls.accent_color
effect_cls: "DampedScrollEffect"
scroll_type: ['bars']
ScrollCenterLayout:
cols: 1
rel_max: dp(800)
rel_min: dp(400)
orientation: 'vertical'
size_hint_y: None
height: self.minimum_height
MDLabel:
text: app.label_text + app.label_text
halign: 'justify'
padding: dp(16), dp(16)
markup: True
font_style: 'Body1'
theme_text_color: 'Primary'
size_hint_y: None
height: self.texture_size[1]
MDLabel:
text: app.label_text + app.label_text
halign: 'justify'
padding: dp(16), dp(16)
markup: True
font_style: 'Body1'
theme_text_color: 'Primary'
size_hint_y: None
height: self.texture_size[1]
Widget
""")
class ScrollCenterLayout(GridLayout):
rel_max = NumericProperty(dp(800))
rel_min = NumericProperty(dp(400))
def __init__(self, **kwargs):
super(ScrollCenterLayout, self).__init__(**kwargs)
self.rel_max = kwargs.get('rel_max', dp(800))
self.rel_min = kwargs.get('rel_min', dp(400))
def on_width(self, instance, value):
if self.rel_max < value:
padding = max(value * .125, (value - self.rel_max) / 2)
elif self.rel_min < value:
padding = min(value * .125, (value - self.rel_min) / 2)
elif self.rel_min < value:
padding = (value - self.rel_min) / 2
else:
padding = 0
self.padding[0] = self.padding[2] = padding
class ExampleScroll(FloatLayout):
pass
class Example(MDApp):
title = "Dialogs"
label_text = LIPSUM
def build(self):
self.theme_cls.primary_palette = "LightGreen"
self.theme_cls.accent_palette = "Green"
self.theme_cls.theme_style = "Dark"
return ExampleScroll()
Example().run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment