Skip to content

Commit

Permalink
rewrite with f/widget macro
Browse files Browse the repository at this point in the history
  • Loading branch information
D00mch committed May 17, 2022
1 parent f4e23db commit 4102ae4
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 117 deletions.
2 changes: 1 addition & 1 deletion deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
:deps {org.clojure/clojure {:mvn/version "1.10.1"}
tensegritics/clojuredart
{:git/url "https://github.com/tensegritics/ClojureDart.git"
:sha "698a2369d62e73cf458448f425864adc40237a80"}}}
:sha "a34504047d2ed596c34e676eecf9f456ad432414"}}}
103 changes: 30 additions & 73 deletions src/minataurus/fab.cljd
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
:child (m/FadeTransition :opacity progress :child child))))

(defn- widgets->expanding-action-buttons
[^#/(List m/Widget) children
[children
^#/(m/Animation double) expand-animation
distance]
(let [step (/ 90.0 (- (count children) 1))]
Expand All @@ -46,10 +46,9 @@
(first children))))
result))))

(defn- build-fab [^m/BuildContext ctx
^#/(m/ValueNotifier bool) close-hint]
(defn- build-fab [^m/BuildContext ctx close-hint]
(let [theme (m.Theme/of ctx)
opened? (.value close-hint)
opened? @close-hint
transform-val (if opened? 0.7 1.0)]
(f/nest
(m/AnimatedContainer
Expand All @@ -62,75 +61,33 @@
:curve (m/Interval 0.25 1.0 :curve m.Curves/easeInOut)
:duration (m/Duration. :milliseconds 250))
(m/FloatingActionButton
:onPressed (fn [] (set! (.-value close-hint) (not (.value close-hint))) nil)
:onPressed (fn [] (swap! close-hint not) nil)
:backgroundColor (if opened? m.Colors/white m.Colors/white))
(m/Icon m.Icons/add :color (if opened? (.primaryColor theme) m.Colors/red)))))

(defn- toggle [opened?
^m/State state
^m/AnimationController controller]
(.setState state #(do
(swap! opened? not)
(if @opened?
(.forward controller)
(.reverse controller))
nil)))

(deftype ExpandableFabState
[controller
animation
opened?
listener
^double distance
^#/(m/ValueNotifier bool) close-hint
^#/(List m/Widget) children]

:extends m/State ^:mixin m/SingleTickerProviderStateMixin

(^void initState [this]
(let [_controller (m/AnimationController
:value 0.0
:duration (m/Duration. :milliseconds 250)
:vsync this)
_animation (m/CurvedAnimation
:parent _controller
:curve (m.Curves/fastOutSlowIn)
:reverseCurve (m.Curves/easeOutQuad))
_listener (fn [] (toggle opened? this _controller))]
(reset! controller _controller)
(reset! animation _animation)
(reset! listener _listener)
(.addListener close-hint _listener)))

(^void dispose [_]
(when-let [^m/AnimationController? _controller @controller]
(.dispose _controller))
(.removeListener close-hint @listener))

(^m/Widget build [_ ctx]
(m.SizedBox/expand
:child (m/Stack
:alignment m.Alignment/bottomRight
:clipBehavior m.Clip/none
:children (list* (build-fab ctx close-hint)
(widgets->expanding-action-buttons
children
@animation
distance))))))

(deftype ExpandableFab
[^#/(m/ValueNotifier bool) close-hint
^double distance
^#/(List m/Widget) children]

:extends m/StatefulWidget

(^ExpandableFabState createState [_]
(ExpandableFabState. ;; CAN'T REMOVE THIS DOT
(atom nil) ; animation-controller
(atom nil) ; animation
(atom false) ; opened?
(atom nil) ; listener
distance
close-hint
children)))
(defn fab [distance children]
(f/widget
:ticker tick
:context ctx
:inherit [:close-hint]
:with [controller (m/AnimationController
:value 0.0
:duration (m/Duration. :milliseconds 250)
:vsync tick)
:dispose comment]
(let [animation (m/CurvedAnimation
:parent controller
:curve (m.Curves/fastOutSlowIn)
:reverseCurve (m.Curves/easeOutQuad))]
(if @close-hint
(.forward controller)
(.reverse controller))
(m.SizedBox/expand
:child (m/Stack
:alignment m.Alignment/bottomRight
:clipBehavior m.Clip/none
:children (list* (build-fab ctx close-hint)
(widgets->expanding-action-buttons
children
animation
distance)))))))
88 changes: 45 additions & 43 deletions src/minataurus/main.cljd
Original file line number Diff line number Diff line change
@@ -1,59 +1,61 @@
(ns minataurus.main
(:require
["package:flutter/material.dart" :as m]
["package:feather_icons/feather_icons.dart" :as icons]
[minataurus.fab :as fab]
["package:feather_icons/feather_icons.dart" :as i]
[cljd.flutter.alpha :as f]
[minataurus.fab :refer [fab]]
[minataurus.utils :as utils]))

(def ^:private logger (utils/build-logger "main"))

(defn ^m/Widget accounts-page [_]
(.d logger "created")
(let [^#/(m/ValueNotifier bool) close-hint (m/ValueNotifier false)
button-style (m.TextButton/styleFrom
(let [button-style (m.TextButton/styleFrom
:backgroundColor m.Colors/blue
:elevation 4
:padding (m.EdgeInsets/symmetric :horizontal 16))
text-style (m/TextStyle :color m.Colors/white)]
(m/Scaffold
:backgroundColor m.Colors/black
:floatingActionButton
(fab/ExpandableFab. ;; ERROR IF REMOVE THIS DOT!
close-hint
80.0 ; distance
[(.icon m/TextButton ; children
:onPressed (fn [] (set! (.-value close-hint) false) nil)
:style button-style
:icon (m/Icon icons.FeatherIcons/plus :size 24 :color m.Colors/yellow)
:label (m/Text "Create" :style text-style))
(.icon m/TextButton
:onPressed (fn ^void [] (set! (.-value close-hint) false) nil)
:style button-style
:icon (m/Icon icons.FeatherIcons/gitMerge :size 24 :color m.Colors/yellow)
:label (m/Text "Update" :style text-style))])
:body (m/Padding.
:padding (.all m/EdgeInsets 16)
:child
(m/Column
:crossAxisAlignment m.CrossAxisAlignment/start
:children [(m/SizedBox :height 32)
(m/Text
"Trying ClojureDart \n the first time"
:style (m/TextStyle.
:color m.Colors/white
:fontSize 32.0))
(m/SizedBox :height 32)
(m/Text
(str
"This demo is written with ClojureDart.\n\n"
"You may see the examples in the linked source code:\n"
"custom Stateful and Stateless widgets, "
"Mixins, Animations.\n\n"

"Home this will help someone.")
:style (m/TextStyle
:color m.Colors/white
:fontSize 26.0))])))))
(f/widget
:bind {:close-hint false}
:inherit [:close-hint]
(m/Scaffold
:backgroundColor m.Colors/black
:floatingActionButton
(fab
80.0 ; distance
[(.icon m/TextButton ; children
:onPressed (fn [] (reset! close-hint false) nil)
:style button-style
:icon (m/Icon (.plus i/FeatherIcons) :size 24 :color m.Colors/yellow)
:label (m/Text "Create" :style text-style))
(.icon m/TextButton
:onPressed (fn ^void [] (reset! close-hint false) nil)
:style button-style
:icon (m/Icon (.gitMerge i/FeatherIcons) :size 24 :color m.Colors/yellow)
:label (m/Text "Update" :style text-style))])
:body (m/Padding.
:padding (.all m/EdgeInsets 16)
:child
(m/Column
:crossAxisAlignment m.CrossAxisAlignment/start
:children [(m/SizedBox :height 32)
(m/Text
"Trying ClojureDart \n the first time"
:style (m/TextStyle.
:color m.Colors/white
:fontSize 32.0))
(m/SizedBox :height 32)
(m/Text
(str
"This demo is written with ClojureDart.\n\n"
"You may see the examples in the linked source code:\n"
"custom Stateful and Stateless widgets, "
"Mixins, Animations.\n\n"

"Home this will help someone.")
:style (m/TextStyle
:color m.Colors/white
:fontSize 26.0))]))))))

(defn main []
(m/runApp
Expand Down

0 comments on commit 4102ae4

Please sign in to comment.