Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: Refactor Badge Control to a Dataclass; create new Control.badge property #4077

Merged
merged 31 commits into from
Oct 29, 2024

Conversation

InesaFitsner
Copy link
Contributor

@InesaFitsner InesaFitsner commented Oct 1, 2024

Description

The Badge class is no more a Flet control and is from now on a simple Python dataclass. The badge property (available in almost all controls) now supports both strings and Badge objects.

Test Code

Previously, worked this way:

import flet as ft

def main(page: ft.Page):
    page.title = "Badge on a NavigationBar destination icon"
    page.navigation_bar = ft.NavigationBar(
        destinations=[
            ft.NavigationBarDestination(
                icon_content=ft.Badge(
                    content=ft.Icon(ft.icons.EXPLORE),
                    small_size=10,
                ),
                label="Explore",
            ),
            ft.NavigationBarDestination(icon=ft.icons.COMMUTE, label="Commute"),
            ft.NavigationBarDestination(
                icon_content=ft.Badge(content=ft.Icon(ft.icons.PHONE), text="10")
            ),
        ]
    )
    page.add(ft.Text("Body!"))


ft.app(main)

The new approach:

import flet as ft


def main(page: ft.Page):
    page.title = "Badge example"

    page.navigation_bar = ft.NavigationBar(
        destinations=[
            ft.NavigationBarDestination(
                icon_content=ft.Icon(
                    ft.icons.EXPLORE,
                    badge=ft.Badge(small_size=10),
                ),
                label="Explore",
            ),
            ft.NavigationBarDestination(
                icon=ft.icons.COMMUTE,
                label="Commute",
            ),
            ft.NavigationBarDestination(
                icon_content=ft.Icon(
                    ft.icons.PHONE,
                    badge="10",
                )
            ),
        ]
    )
    page.add(ft.Text("Body!"))


ft.app(main)

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

Checklist:

  • I signed the CLA.
  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • My changes generate no new warnings
  • New and existing tests pass locally with my changes
  • I have made corresponding changes to the documentation (if applicable)

Screenshots (if applicable):

Additional details

Summary by Sourcery

Refactor the Badge class into a Python dataclass and update the badge property to support both strings and Badge objects across multiple controls. This change simplifies the Badge implementation and enhances its flexibility in usage.

New Features:

  • Introduce support for the badge property in various controls, allowing both strings and Badge objects to be used.

Enhancements:

  • Refactor the Badge class from a Flet control to a simple Python dataclass, simplifying its structure and usage.

Documentation:

  • Update documentation to reflect the changes in the Badge class and its usage across controls.

Copy link
Contributor

sourcery-ai bot commented Oct 1, 2024

Reviewer's Guide by Sourcery

This pull request refactors the Badge class from a Flet control to a simple Python dataclass. The badge property, available in most controls, now supports both strings and Badge objects. This change simplifies the Badge implementation and enhances its flexibility across the Flet framework.

Class diagram for the refactored Badge class

classDiagram
    class Badge {
        +Optional~str~ text
        +OffsetValue offset
        +Optional~Alignment~ alignment
        +Optional~str~ bgcolor
        +Optional~bool~ label_visible
        +OptionalNumber large_size
        +Optional~PaddingValue~ padding
        +OptionalNumber small_size
        +Optional~str~ text_color
        +Optional~TextStyle~ text_style
    }

    class BadgeValue {
        +Optional~Union[str, Badge]~
    }
Loading

File-Level Changes

Change Details Files
Refactored Badge class from a Flet control to a Python dataclass
  • Removed inheritance from Control class
  • Simplified Badge class attributes
  • Removed methods and properties related to Control class
  • Added BadgeValue type alias for Optional[Union[str, Badge]]
sdk/python/packages/flet-core/src/flet_core/badge.py
Updated badge property implementation across Flet controls
  • Added badge parameter to init methods
  • Updated Control class to include badge attribute
  • Modified before_update method to handle badge attribute
sdk/python/packages/flet-core/src/flet_core/control.py
sdk/python/packages/flet-core/src/flet_core/container.py
sdk/python/packages/flet-core/src/flet_core/elevated_button.py
sdk/python/packages/flet-core/src/flet_core/icon.py
sdk/python/packages/flet-core/src/flet_core/text.py
Implemented badge parsing and rendering in Dart
  • Created new file badge.dart for badge parsing logic
  • Implemented parseBadge and badgeFromJSON functions
  • Updated create_control.dart to use new badge parsing
packages/flet/lib/src/utils/badge.dart
packages/flet/lib/src/controls/create_control.dart
Updated import statements and type hints
  • Added import for BadgeValue in various files
  • Updated function signatures to include badge parameter
  • Modified type hints to accommodate new Badge implementation
sdk/python/packages/flet-core/src/flet_core/__init__.py
sdk/python/packages/flet-core/src/flet_core/types.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

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.

Hey @InesaFitsner - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@ndonkoHenri ndonkoHenri changed the title Badge property feat!: Refactor Badge Control to a Dataclass; create new Control.badge property Oct 4, 2024
@ndonkoHenri ndonkoHenri added enhancement Improvement/Optimization controls breaking change Will disrupt existing functionality labels Oct 4, 2024
@FeodorFitsner FeodorFitsner merged commit 0310503 into main Oct 29, 2024
2 checks passed
@ndonkoHenri ndonkoHenri deleted the badge-property branch October 29, 2024 19:09
pengwon added a commit to pengwon/flet that referenced this pull request Oct 30, 2024
* main: (31 commits)
  Migrate `colors` and `icons` variables to Enums (flet-dev#4180)
  feat: expose more properties in Controls (flet-dev#4105)
  feat!: Refactor `Badge` Control to a Dataclass; create new `Control.badge` property (flet-dev#4077)
  fix: clicking on `CupertinoContextMenuAction` doesn't close context menu (flet-dev#3948)
  fix dropdown `max_menu_height` (flet-dev#3974)
  Fix undefined name "Future" --> asyncio.Future (flet-dev#4230)
  wrap ListTile in material widget (flet-dev#4206)
  Update CONTRIBUTING.md (flet-dev#4208)
  TextField: suffix_icon, prefix_icon and icon can be Control or str (flet-dev#4173)
  feat!: enhance `Map` control (flet-dev#3994)
  skip running flutter doctor on windows if no_rich_output is True (flet-dev#4108)
  add --pyinstaller-build-args to pack cli command (flet-dev#4187)
  fix/feat: make `SearchBar`'s view height adjustable; add new properties (flet-dev#4039)
  fix: prevent button `style` from being modified in `before_update()` (flet-dev#4181)
  fix: disabling filled buttons is not visually respected (flet-dev#4090)
  when `label` is set, use `MainAxisSize.min` for the `Row` (flet-dev#3998)
  fix: `NavigationBarDestination.disabled` has no visual effect (flet-dev#4073)
  fix autofill in CupertinoTextField (flet-dev#4103)
  Linechart: jsonDecode tooltip before displaying (flet-dev#4069)
  fixed bgcolor, color and elevation (flet-dev#4126)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking change Will disrupt existing functionality controls enhancement Improvement/Optimization
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants