Search code examples
dartclojure

ClojureDart - How to translate .of(context) into ClojureDart?


class SnackBarPage extends StatelessWidget {
  const SnackBarPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Center(
      child: ElevatedButton(
        onPressed: () {
          final snackBar = SnackBar(
            content: const Text('Yay! A SnackBar!'),
            action: SnackBarAction(
              label: 'Undo',
              onPressed: () {
                // Some code to undo the change.
              },
            ),
          );

          // Find the ScaffoldMessenger in the widget tree
          // and use it to show a SnackBar.
          ScaffoldMessenger.of(context).showSnackBar(snackBar);
        },
        child: const Text('Show SnackBar'),
      ),
    );
  }
}

Above is the code that I want to translate into ClojureDart. However, ClojureDart is a dialect of Clojure which is a functional language,


Solution

  • Found! I had to use cljd.flutter.alpha library that exports f/widget, and wraps it around the scaffold. Then, within the widget, I had to use the inherit keyword with the widget I wanted to inherit, in my case, ScaffoldMessenger. After doing so, I have access to the ScaffoldMessenger object with a variable with a kebab-case name. While it can be confusing without seeing it, it gives something like that :

    (f/widget
    :inherit [m/ScaffoldMessenger]
    (m/Scaffold
    ...
    :onPressed (.showSnackBar scaffold-messenger snackbar)
    ...))
    

    With scaffold-messenger variable corresponding to m/ScaffoldMessenger, and snackbar being a function that returns a m/SnackBar widget.