Skip to content

Instantly share code, notes, and snippets.

@cgrand
Last active May 1, 2024 21:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cgrand/3de5cf080cdbcfa4627c20b4094aa739 to your computer and use it in GitHub Desktop.
Save cgrand/3de5cf080cdbcfa4627c20b4094aa739 to your computer and use it in GitHub Desktop.
(ns workshop.main
(:require ["package:flutter/material.dart" :as m]
["package:google_fonts/google_fonts.dart" :as fonts]
#_["example.dart" :as ex]
[cljd.flutter.alpha2 :as f]))
(def normal-text-style
(fonts/GoogleFonts.sourceCodePro
.fontSize 26
.fontWeight m/FontWeight.w700
.color m/Colors.grey.shade600))
(def normal-icon-style
(m/IconButton.styleFrom
.foregroundColor m/Colors.grey.shade800
.backgroundColor m/Colors.grey.shade200))
(def ^m/Widget home
(f/build
:context ctx
:get {nav m/Navigator}
(m/Scaffold .appBar (m/AppBar .title (m/Text "Bonjour")))
.body
(m/Padding .padding (m/EdgeInsets.all 12))
(m/Column
.mainAxisAlignment m/MainAxisAlignment.center
.crossAxisAlignment m/CrossAxisAlignment.stretch
.children
[(m/Text "Welcome to our awesome todo"
.style normal-text-style)
(m/SizedBox .height 10)
(m/IconButton
.icon (m/Icon m/Icons.navigate_next)
.style normal-icon-style
.onPressed
(fn []
(let [navigation (m/Navigator.of ctx)])
(.pushNamed nav "/suite")
nil))])))
(defn todo-item [{:keys [title tags author creation-date]}]
(m/CheckboxListTile .value false .onChanged (fn [_])
.title (m/Text title)
.subtitle (m/Row
.mainAxisAlignment m/MainAxisAlignment.spaceBetween
.children [(m/Text tags) (m/Text (str "@" author)) (m/Text creation-date)])))
(def ^m/Widget suite
(f/build
:get {nav m/Navigator}
(m/Scaffold .appBar (m/AppBar .title (m/Text "Suite")))
.body
m/Center
(m/Padding .padding (m/EdgeInsets.all 16))
(m/Column
.mainAxisAlignment m/MainAxisAlignment.center
.crossAxisAlignment m/CrossAxisAlignment.center)
.children
[(m/Text "Exemple TODO")
(todo-item {:title "Hello worlkd"
:tags "prio"
:author "baptiste"
:creation-date "12/12/12"})
(m/IconButton
.icon (m/Icon m/Icons.navigate_next)
.onPressed (fn [] (.pushNamed nav "/prenom") nil)
.style normal-icon-style)]))
(def ^m/Widget prenom
(f/build
:get [m/Navigator]
(m/Scaffold .appBar (m/AppBar .title (m/Text "Comment tu t'appelles ?")))
.body
(m/Padding .padding (m/EdgeInsets.all 16))
m/Center
:managed [controller (m/TextEditingController)]
(m/Column
.mainAxisAlignment m/MainAxisAlignment.center
.children
[(f/widget
:watch [error-text
(f/sub controller
#(when (-> ^m/TextEditingValue % .-text .-isEmpty) "EMPTY"))]
(m/TextField .controller controller
.decoration (m/InputDecoration
.errorText error-text)))
(m/IconButton
.style normal-icon-style
.icon (m/Icon m/Icons.navigate_next)
.onPressed (fn []
(when (-> controller .-text .-isNotEmpty)
(swap! app-state assoc :current-user (.-text controller))
(.pushNamed navigator "/todos")
nil)))])))
(def ^m/Widget todo-list
(f/build
(m/Scaffold
.appBar (m/AppBar .title (m/Text "TODO List"))
.floatingActionButton
(f/widget
:context ctx
(m/IconButton
.style normal-icon-style
.icon (f/widget (m/Icon m/Icons.add))
.onPressed (fn []
(m/showModalBottomSheet .context ctx
.builder
(f/build
:get {nav m/Navigator}
:managed [controller1 (m/TextEditingController)
controller2 (m/TextEditingController)]
(m/Padding .padding (m/EdgeInsets.all 16))
(m/Column
.mainAxisSize m/MainAxisSize.min
.crossAxisAlignment m/CrossAxisAlignment.stretch
.children
[(m/SizedBox .height 16)
(m/TextField
.controller controller1
.decoration
(m/InputDecoration .labelText "Description"))
(m/SizedBox .height 16)
(m/TextField
.controller controller2
.decoration
(m/InputDecoration .labelText "Tags"))
(m/SizedBox .height 16)
(m/ElevatedButton
.onPressed (fn []
(swap! app-state update :todos
(fnil conj [])
{:title (.-text controller1)
:author (:current-user @app-state)
:tags (.-text controller2)
:done false
:creation-date
(str
(.-hour (DateTime/now))
":"
(.-minute (DateTime/now)))})
(.pop nav))
.child (m/Text "Save "))
(m/SizedBox .height 16)])))
nil))))
.body
(m/Padding .padding (m/EdgeInsets.all 16))
m/Center
(f/widget
:watch [has-todos (f/sub app-state (comp some? seq :todos))]
(if has-todos
(f/widget
:watch [todos (f/sub app-state :todos)]
(m/ListView.builder
.itemCount (count todos)
.itemBuilder
(f/build [idx]
(todo-item (get todos idx)))))
(m/Column
.mainAxisAlignment m/MainAxisAlignment.center
.children
[(m/Text "Coucou")])))))
(def routes
{"/" home
"/suite" suite
"/prenom" prenom
"/todos" todo-list})
(defonce app-state (atom {}))
(defn main []
(m/runApp
(f/widget
#_#_:let [_ (dart:core/print ex/name)]
:key "root"
(m/MaterialApp
.theme (m/ThemeData
.useMaterial3 true
.iconTheme (m/IconThemeData .size 32))
.routes routes))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment