Hiding widgets#

Actually every widget can be hidden including texts, buttons, groups and so on. It is managed by when attribute. It can be either a data key, a predicate function or a F-filter (from magic-filter).

F-filter receives the data from the getter and other dialog data. You can refer to it, for example F["extended"] or F["dialog_data"]["user"]["name"]

from typing import Dict

from aiogram.filters.state import StatesGroup, State
from magic_filter import F

from aiogram_dialog import Window, DialogManager
from aiogram_dialog.widgets.common import Whenable
from aiogram_dialog.widgets.kbd import Button, Row, Group
from aiogram_dialog.widgets.text import Const, Format, Multi


class MySG(StatesGroup):
    main = State()


async def get_data(**kwargs):
    return {
        "name": "Tishka17",
        "extended": False,
    }


def is_tishka17(data: Dict, widget: Whenable, manager: DialogManager):
    return data.get("name") == "Tishka17"


window = Window(
    Multi(
        Const("Hello"),
        Format("{name}", when="extended"),
        sep=" "
    ),
    Group(
        Row(
            Button(Const("Wait"), id="wait"),
            Button(Const("Ignore"), id="ignore"),
            when=F["extended"],
        ),
        Button(Const("Admin mode"), id="nothing", when=is_tishka17),
    ),
    state=MySG.main,
    getter=get_data,
)
../../_images/whenable.png

If you only change data setting "extended": True the window will look differently

../../_images/whenable_extended.png
class aiogram_dialog.widgets.common.when.Predicate(*args, **kwargs)#
abstract __call__(data, widget, dialog_manager)#

Check if widget should be shown.

Parameters:
  • data (Dict) – Data received from getter

  • widget (Whenable) – Widget we are working with

  • dialog_manager (DialogManager) – Dialog manager to access current context

Returns:

True if widget has to be shown, False otherwise

Return type:

bool