From 4102ae4b8c38e4fd0721a3a1ffac9d091c589be8 Mon Sep 17 00:00:00 2001 From: liverm0r Date: Tue, 17 May 2022 23:22:10 +0300 Subject: [PATCH] rewrite with f/widget macro --- deps.edn | 2 +- src/minataurus/fab.cljd | 103 ++++++++++++--------------------------- src/minataurus/main.cljd | 88 +++++++++++++++++---------------- 3 files changed, 76 insertions(+), 117 deletions(-) diff --git a/deps.edn b/deps.edn index d2b34e8..b4e4499 100644 --- a/deps.edn +++ b/deps.edn @@ -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"}}} diff --git a/src/minataurus/fab.cljd b/src/minataurus/fab.cljd index 462073f..0888989 100644 --- a/src/minataurus/fab.cljd +++ b/src/minataurus/fab.cljd @@ -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))] @@ -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 @@ -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))))))) diff --git a/src/minataurus/main.cljd b/src/minataurus/main.cljd index 5723cb3..3b5594a 100644 --- a/src/minataurus/main.cljd +++ b/src/minataurus/main.cljd @@ -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