Skip to content

Instantly share code, notes, and snippets.

@marenovakovic
Created January 24, 2023 11:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marenovakovic/5daba2fb5ebb83a93f9ce6314ce7fb9f to your computer and use it in GitHub Desktop.
Save marenovakovic/5daba2fb5ebb83a93f9ce6314ce7fb9f to your computer and use it in GitHub Desktop.
Counter Flutter app
module fsharp.Main
open Flutter.Foundation
open Flutter.Rendering
open Flutter.Widgets
open Flutter.Material
open Elmish
open Elmish.Flutter
type Model = { count: int }
type Msg =
| Increment
| Decrement
let init () = { count = 0 }, Cmd.none
let update msg model =
match msg with
| Increment -> { model with count = model.count + 1 }, Cmd.none
| Decrement -> { model with count = model.count - 1 }, Cmd.none
let buttons dispatch : Widget =
Row(
mainAxisAlignment = MainAxisAlignment.center,
children =
[| MaterialButton(child = Text("Increment"), onPressed = fun () -> Increment |> dispatch)
MaterialButton(child = Text("Decrement"), onPressed = fun () -> Decrement |> dispatch)
SizedBox(width = 8) |]
)
let view model dispatch context : Widget =
Scaffold(
appBar = AppBar(title = Text("Hello from F#!")),
body =
Center(
child =
Column(
mainAxisAlignment = MainAxisAlignment.center,
children =
[| Text("Count:")
Text(model.count |> string)
SizedBox(height = 8)
buttons dispatch |]
)
)
)
type MyApp(?key: Key) =
inherit StatelessWidget(?key = key)
override _.build(context) =
MaterialApp(title = "Hello from F#!", home = ElmishWidget.From(init, update, view))
let main () = MyApp() |> runApp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment