Last active
May 14, 2024 20:29
-
-
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`
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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