Replace Literals with Enums
See original GitHub issuePlease 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:
- Created 10 months ago
- Comments:6 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
That sounds great and right! I’m going to do a PR with that. Or you probably want to help? 😉
It’s been published to Flet pre-releases: