Skip to content

Instantly share code, notes, and snippets.

@eernstg
Last active May 14, 2024 20:29
Show Gist options
  • Save eernstg/61abcc3d7d66b627005307f1062de702 to your computer and use it in GitHub Desktop.
Save eernstg/61abcc3d7d66b627005307f1062de702 to your computer and use it in GitHub Desktop.
Example from 'Cutting the stateful boilerplate' using the proposed macros, such as `@Stateful`
import 'package:flutter/material.dart';
void main() {
runApp(const App(name: 'Dash'));
}
@Stateless()
class App {
@Input() String get name;
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Counter(
name: name,
startValue: 10,
),
);
}
}
@Stateful()
class Counter {
@Input() String get name;
@Assert('startValue >= 0')
@Input(0) int get startValue;
int _count = 0;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Hello $name')),
floatingActionButton: FloatingActionButton(
onPressed: () {
setState(() {
_count += 1;
});
},
),
body: Center(
child: Text('Count: ${startValue + _count}'),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment