question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Replace Literals with Enums

See original GitHub issue

Please Describe The Problem To Be Solved

When it’s needed to set an attribute such as theme, alignment, type of scroll, etc , we have to type “magic” literals after consulting them in the documentation or in the IDE, leading to misstyping errors.

def main(page: flet.Page):
    page.theme_mode = "light"
    page.add(
        Row(
            controls=[
                Text(
                    "Hello World!"
                )
            ],
            alignment="center"
        )
    )

(Optional): Suggest A Solution My suggestion is to replace the use of literals with Enums, to make it easier to set attributes to an usercontrol, and reusable the values related with same features. This is something that other UI frameworks such as Flutter and Jetpack Compose do.

Jetpack Compose:

@Composable
fun App() {
    var text by remember { mutableStateOf("Hello, World!") }

    MaterialTheme {
        Column(
            modifier = Modifier.fillMaxSize(),
            verticalArrangement = Arrangement.Center,
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
           Button(onClick = {
            text = "Hello, Desktop!"
           }) {
               Text(text)
            }
        }
    }
}

Flutter:

Column(
  crossAxisAlignment: CrossAxisAlignment.start,
  mainAxisSize: MainAxisSize.min,
  children: <Widget>[
    const Text('We move under cover and we move as one'),
    const Text('Through the night, we have one shot to live another day'),
    const Text('We cannot let a stray gunshot give us away'),
    const Text('We will fight up close, seize the moment and stay in it'),
    const Text('It’s either that or meet the business end of a bayonet'),
    const Text('The code word is ‘Rochambeau,’ dig me?'),
    Text('Rochambeau!', style: DefaultTextStyle.of(context).style.apply(fontSizeFactor: 2.0)),
  ],
)

In the case of Flet, the implementation coulbe like this:

class ThemeMode(enum.Enum):
    NONE = None
    SYSTEM = "system"
    LIGHT = "light"
    DARK = "dark"


class MainAxisAlignment(enum.Enum):
    NONE = None
    START = "start"
    END = "end"
    CENTER = "center"
    SPACE_BETWEEN = "spaceBetween"
    SPACE_AROUND = "spaceAround"
    SPACE_EVENLY = "spaceEvenly"


def main(page: flet.Page):
    page.theme_mode = ThemeMode.DARK
    page.add(
        Row(
            controls=[
                Text(
                    "Hello World!"
                )
            ],
            alignment=MainAxisAlignment.CENTER
        )
    )

Issue Analytics

  • State:closed
  • Created 10 months ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
FeodorFitsnercommented, Nov 20, 2022

That sounds great and right! I’m going to do a PR with that. Or you probably want to help? 😉

1reaction
FeodorFitsnercommented, Nov 24, 2022

It’s been published to Flet pre-releases:

pip install flet --pre
Read more comments on GitHub >

github_iconTop Results From Across the Web

Replace enums with string literals #324 - palantir/blueprint
Let's replace enums with string literals. Instead of something like export enums RegionCardinality { CELLS, FULL_COLUMNS, } ...
Read more >
Replace String Literals If/elseIf block with Enum - java
I'm new to using Java Enums and I've read that replace IF logic that compares String literals should be replaced with an Enum....
Read more >
Literal types and Enums - mypy 0.991 documentation
Literal types may contain one or more literal bools, ints, strs, bytes, and enum values. However, literal types cannot contain arbitrary expressions: types...
Read more >
13 Alternatives to enums in TypeScript
The previous chapter explored how TypeScript enums work. In this chapter, we take a look at alternatives to enums. 13.1 Unions of singleton...
Read more >
Enums to replace hardcoded string constants
It is possible to use an enum while still preserving the convenience of a plain class when comparing the enum members to plain...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found