https://github.com/josiahsrc/flutter_functional_widget
https://github.com/josiahsrc/flutter_functional_widget
Last synced: 12 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/josiahsrc/flutter_functional_widget
- Owner: josiahsrc
- License: mit
- Created: 2024-07-14T16:39:12.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-07-14T21:19:31.000Z (almost 2 years ago)
- Last Synced: 2025-03-13T09:49:21.989Z (over 1 year ago)
- Language: C++
- Size: 271 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Flutter functional widget
🚧 EXPERIMENTAL! 🚧
An experiment to see what a functional widget might look like in flutter. Taking inspiration from react to try to reduce boilerplate.
EDIT: There is a much more flushed out implementation here: https://github.com/dart-lang/language/blob/main/working/macros/example/lib/functional_widget.dart
(I wasn't aware of this at the time of writing this)
## New API
For example, you might define a function like this
```dart
// 8 lines of code
@Functional()
Widget _fab(BuildContext context, {VoidCallback? onPressed}) {
return FloatingActionButton(
onPressed: onPressed,
tooltip: 'Increment',
child: const Icon(Icons.add),
);
}
```
And be able to use the macro version of the widget like you would any other widget.
```dart
Widget build(BuildContext context) {
return Scaffold(
floatingActionButton: Fab(
onPressed: () {
print("hello world!");
}
),
);
}
```
## Standard API
The equivalent in the current way to do this is the following, requiring more lines of code.
```dart
// 12 lines of code
class Fab extends StatelessWidget {
const Fab({super.key, this.onPressed});
final VoidCallback? onPressed;
@override
Widget build(BuildContext context) {
return FloatingActionButton(
onPressed: onPressed,
tooltip: 'Increment',
child: const Icon(Icons.add),
);
}
}
```
## Limitations
1. Macros don't support default parameters or generics yet.
1. As far as I know, you can't generate a new type from a macro. Until that's resolved, this approach generates a function that uses a Builder widget.