Skip to content

Instantly share code, notes, and snippets.

@yjbanov
Last active May 9, 2024 00:00
Show Gist options
  • Save yjbanov/560171695b59cc8f0356f40443703ee7 to your computer and use it in GitHub Desktop.
Save yjbanov/560171695b59cc8f0356f40443703ee7 to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp(name: 'Dash'));
}
class MyApp extends StatelessWidget {
const MyApp({super.key, required this.name});
final String name;
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Counter(
name: name,
startValue: 10,
),
);
}
}
// @State('int count = 0')
class Counter extends GeneratedStatefulWidget<_GeneratedCounterState> {
const Counter({super.key, required this.name, required this.startValue});
final String name;
final int startValue;
@override
Widget build(state) {
return Scaffold(
appBar: AppBar(title: Text('Hello $name')),
floatingActionButton: FloatingActionButton(
onPressed: () {
state.count += 1;
},
),
body: Center(
child: Text('Count: ${startValue + state.count}'),
),
);
}
//////////////// GENERATED ///////////////////////////
@override
State<StatefulWidget> createState() => _GeneratedCounterState();
}
abstract class GeneratedStatefulWidget<S extends State> extends StatefulWidget {
const GeneratedStatefulWidget({super.key});
Widget build(S state);
}
class _GeneratedCounterState extends State<Counter> {
int get count => _count;
set count(int value) {
setState(() {
_count = value;
});
}
int _count = 0;
@override
Widget build(BuildContext context) {
return widget.build(this);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment