Skip to content

fix(button): apply first-time style patches and clear stale style internals#6119

Open
ndonkoHenri wants to merge 10 commits intomainfrom
fix-button
Open

fix(button): apply first-time style patches and clear stale style internals#6119
ndonkoHenri wants to merge 10 commits intomainfrom
fix-button

Conversation

@ndonkoHenri
Copy link
Contributor

@ndonkoHenri ndonkoHenri commented Feb 2, 2026

Fix #6118

Description

When a Button starts without bgcolor, the first click (None -> GREEN) could update Python state but not the UI.
Also, after changing style props back to defaults, stale style data could remain and keep old visuals.

Root cause

  1. Dart patch apply path

    • First-time style updates arrive as an ADD patch to _internals.style.
    • If _internals didn’t exist yet, patch target resolution failed to materialize missing intermediate maps.
  2. Python control internals

    • Button / IconButton wrote _internals["style"] when style-related props were set,
    • but didn’t clear it when props returned to defaults.

Changes

  • Dart

    • Enhanced getPatchTarget(...) with optional createMissingMaps.
    • ADD operations now resolve targets with createMissingMaps: true, allowing creation of missing intermediate maps (e.g. _internals) before inserting new keys.
  • Python

    • Clear stale style internals when style-driving values are default:
      • self._internals.pop("style", None)

Summary by Sourcery

Ensure button style patches correctly materialize intermediate state and clear stale style metadata.

Bug Fixes:

  • Fix JSON patch ADD operations so missing intermediate maps (e.g., control internals) are created before inserting new entries, ensuring initial style updates apply to controls like Button.
  • Clear stale style internals for Button and IconButton when style-driving properties return to default values so visuals revert correctly.

Tests:

  • Add a control patching test to verify ADD operations create missing intermediate maps in control properties.

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We've reviewed this pull request using the Sourcery rules engine

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes button style update issues where first-time style changes (e.g., None -> GREEN) failed to appear in the UI, and stale style data persisted after properties returned to defaults.

Changes:

  • Enhanced Dart patch resolution to create missing intermediate maps (like _internals) when applying ADD operations
  • Modified Python Button and IconButton controls to clear stale style internals when style properties reset to defaults

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
sdk/python/packages/flet/src/flet/controls/material/button.py Clears _internals["style"] when style properties return to defaults
sdk/python/packages/flet/src/flet/controls/material/icon_button.py Clears _internals["style"] when style properties return to defaults
packages/flet/lib/src/models/control.dart Adds createMissingMaps parameter to getPatchTarget to materialize missing intermediate maps during ADD operations
packages/flet/test/utils/control_test.dart Adds test case verifying ADD patch creates missing intermediate map

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Feb 2, 2026

Deploying flet-docs with  Cloudflare Pages  Cloudflare Pages

Latest commit: 99350a8
Status: ✅  Deploy successful!
Preview URL: https://6815e2fb.flet-docs.pages.dev
Branch Preview URL: https://fix-button.flet-docs.pages.dev

View logs

@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Feb 2, 2026

Deploying flet-examples with  Cloudflare Pages  Cloudflare Pages

Latest commit: 99350a8
Status: ✅  Deploy successful!
Preview URL: https://5fb1bc7e.flet-examples.pages.dev
Branch Preview URL: https://fix-button.flet-examples.pages.dev

View logs

When a dict transitions from empty to non-empty (or vice versa), mark the item as replaced and run dataclass removal/addition handlers instead of performing a dict comparison. This ensures correct patch generation by calling _item_replaced, _dataclass_removed and _dataclass_added with the appropriate path/parent/frozen context.
Update test_object_diff_frozen.py to reflect new diff behavior: instead of add/remove ops on path ['_internals','style'], the tests now expect a single 'replace' on ['_internals'] with the style wrapped in a dict, and a 'replace' to an empty dict when style is removed. This aligns the tests with the changed diff output for Button internals.
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.

bug: First Button.bgcolor update is ignored when initial value is None

2 participants