Skip to content

Conversation

@tomasz-brak
Copy link

Description of the problem

Describe what problem Closes #1680

Description of Changes

Don't add spacing with the Boxlayout's spacer, instead

Screenshots of the solution to the problem

image image

Code for testing new changes

from doctest import debug

from regex import T
from sympy import false
from kivymd.app import MDApp

from kivy.logger import Logger

from kivymd.uix.boxlayout import MDBoxLayout
from kivymd.uix.button.button import MDButton, MDButtonText
from kivymd.uix.dialog.dialog import (
    MDDialog,
    MDDialogButtonContainer,
    MDDialogContentContainer,
    MDDialogHeadlineText,
    MDDialogIcon,
    MDDialogSupportingText,
)
from kivymd.uix.divider.divider import MDDivider
from kivymd.uix.label.label import MDIcon
from kivymd.uix.list.list import (
    MDListItem,
    MDListItemLeadingIcon,
    MDListItemSupportingText,
)
from kivymd.uix.screen import MDScreen
from kivy.uix.widget import Widget

Logger.setLevel("DEBUG")


class OpenDialog(MDButton):
    def __init__(self, content_top: bool, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.bind(on_release=lambda x: self.action())
        self.dialog = MDDialog(MDDialogContentContainer(MDIcon(icon="weight")))
        if content_top:
            print("Adding content on top")
            self.dialog.add_widget(
                MDDialogHeadlineText(text="Dialog with content on top")
            )

    def action(self):
        self.dialog.open()


def show_alert_dialog(self):
    MDDialog(
        # ----------------------------Icon-----------------------------
        MDDialogIcon(
            icon="refresh",
        ),
        # -----------------------Headline text-------------------------
        MDDialogHeadlineText(
            text="Reset settings?",
        ),
        # -----------------------Supporting text-----------------------
        MDDialogSupportingText(
            text="This will reset your app preferences back to their "
            "default settings. The following accounts will also "
            "be signed out:",
        ),
        # -----------------------Custom content------------------------
        MDDialogContentContainer(
            MDDivider(),
            MDListItem(
                MDListItemLeadingIcon(
                    icon="gmail",
                ),
                MDListItemSupportingText(
                    text="[email protected]",
                ),
                theme_bg_color="Custom",
                md_bg_color=self.theme_cls.transparentColor,
            ),
            MDListItem(
                MDListItemLeadingIcon(
                    icon="gmail",
                ),
                MDListItemSupportingText(
                    text="[email protected]",
                ),
                theme_bg_color="Custom",
                md_bg_color=self.theme_cls.transparentColor,
            ),
            MDDivider(),
            orientation="vertical",
        ),
        # ---------------------Button container------------------------
        MDDialogButtonContainer(
            Widget(),
            MDButton(
                MDButtonText(text="Cancel"),
                style="text",
            ),
            MDButton(
                MDButtonText(text="Accept"),
                style="text",
            ),
            spacing="8dp",
        ),
        # -------------------------------------------------------------
    ).open()


class MainApp(MDApp):
    def build(self):
        self.theme_cls.theme_style = "Dark"
        self.theme_cls.primary_palette = "Orange"

        return MDScreen(
            MDBoxLayout(
                OpenDialog(
                    False,
                    MDButtonText(
                        text="Nowhere",
                    ),
                    pos_hint={"center_x": 0.5, "center_y": 0.5},
                ),
                OpenDialog(
                    True,
                    MDButtonText(
                        text="Top only",
                    ),
                    pos_hint={"center_x": 0.5, "center_y": 0.5},
                ),
                MDButton(
                    MDButtonText(
                        text="Show alert dialog",
                    ),
                    pos_hint={"center_x": 0.5, "center_y": 0.5},
                    on_release=lambda x: show_alert_dialog(self),
                ),
            )
        )


MainApp().run()

@tomasz-brak tomasz-brak changed the title remove universal spacing add spacers based on children Fix: Wrong spaing when using Dialog withoutMDDialogHeadlineText Nov 21, 2025
@HeaTTheatR
Copy link
Member

HeaTTheatR commented Nov 22, 2025

Hi!
Have you checked how your changes will behave with different dialog content?
Without buttons, without a title, with a title but without content, with a title and without content but with buttons, and so on...

@tomasz-brak
Copy link
Author

I think, i tested almost every combination possible, and i didn't encounter any issues

i used this code to speedup the testing

import dataclasses
import inspect

from kivy.logger import Logger
from kivy.uix.widget import Widget

from kivymd.app import MDApp
from kivymd.uix.boxlayout import MDBoxLayout
from kivymd.uix.button.button import MDButton, MDButtonText
from kivymd.uix.dialog.dialog import (
    MDDialog,
    MDDialogButtonContainer,
    MDDialogContentContainer,
    MDDialogHeadlineText,
    MDDialogIcon,
    MDDialogSupportingText,
)
from kivymd.uix.divider.divider import MDDivider
from kivymd.uix.label.label import MDIcon, MDLabel
from kivymd.uix.list.list import (
    MDListItem,
    MDListItemLeadingIcon,
    MDListItemSupportingText,
)
from kivymd.uix.screen import MDScreen
from kivymd.uix.selectioncontrol.selectioncontrol import MDCheckbox

Logger.setLevel("DEBUG")


class Options:
    icon = True
    supporting_text = True
    headline = True
    divider = True
    item_list = True
    buttons = True


opt = Options()


def show_dialog(*_, **__):
    MDDialog(
        (
            MDDialogIcon(
                icon="refresh",
            )
            if opt.icon
            else None
        ),
        (
            MDDialogHeadlineText(
                text="Reset settings?",
            )
            if opt.headline
            else None
        ),
        (
            MDDialogSupportingText(
                text="This will reset your app preferences back to their "
                "default settings. The following accounts will also "
                "be signed out:",
            )
            if opt.supporting_text
            else None
        ),
        MDDialogContentContainer(
            MDDivider() if opt.divider else None,
            (
                MDListItem(
                    MDListItemLeadingIcon(
                        icon="gmail",
                    ),
                    MDListItemSupportingText(
                        text="[email protected]",
                    ),
                    theme_bg_color="Custom",
                )
                if opt.item_list
                else None
            ),
            (
                MDListItem(
                    MDListItemLeadingIcon(
                        icon="gmail",
                    ),
                    MDListItemSupportingText(
                        text="[email protected]",
                    ),
                    theme_bg_color="Custom",
                )
                if opt.item_list
                else None
            ),
            MDDivider() if opt.divider else None,
            orientation="vertical",
        ),
        (
            MDDialogButtonContainer(
                Widget(),
                MDButton(
                    MDButtonText(text="Cancel"),
                    style="text",
                ),
                MDButton(
                    MDButtonText(text="Accept"),
                    style="text",
                ),
                spacing="8dp",
            )
            if opt.buttons
            else None
        ),
    ).open()


class CheckItem(MDBoxLayout):
    def __init__(self, text, **kwargs):
        super().__init__(**kwargs)

        self.orientation = "horizontal"

        self.label = MDLabel(
            text=text,
            halign="left",
            valign="center",
        )

        self.checkbox = MDCheckbox(
            size_hint=(None, None),
            size=("48dp", "48dp"),
            pos_hint={"center_y": 0.5},
            active=True,
        )

        self.add_widget(self.label)
        self.add_widget(self.checkbox)


class MainApp(MDApp):
    def build(self):
        self.theme_cls.theme_style = "Dark"
        self.theme_cls.primary_palette = "Orange"
        layout = MDBoxLayout(
            MDButton(
                MDButtonText(text="Show Dialog"),
                pos_hint={"center_x": 0.5, "center_y": 0.5},
                on_release=show_dialog,
            ),
            orientation="vertical",
        )
        attributes = inspect.getmembers(
            opt, lambda a: not (inspect.isroutine(a))
        )
        fields = [
            dataclass_field
            for dataclass_field in attributes
            if not (dataclass_field[0].startswith("__"))
        ]
        print(fields)
        for option in fields:
            print(option)
            check_item = CheckItem(text=option[0].replace("_", " ").title())
            check_item.checkbox.bind(
                on_active=lambda instance, value, option=option[0]: setattr(
                    opt, option, value
                )
            )
            layout.add_widget(check_item)
        return MDScreen(layout)


MainApp().run()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MDDialog headline should be optional

2 participants