Created
October 13, 2024 14:40
-
-
Save danReynolds/47ee5234834628e5bca77f4ffd7e642b to your computer and use it in GitHub Desktop.
FState
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'; | |
class FState<T extends Widget> extends StatefulWidget { | |
final T scoped; | |
final Widget Function(T widget, void Function(VoidCallback)) Function() | |
builder; | |
const FState(this.scoped, this.builder, {super.key}); | |
@override | |
FStateState<T> createState() => FStateState<T>(); | |
} | |
class FStateState<T extends Widget> extends State<FState<T>> { | |
late final _builder = widget.builder(); | |
@override | |
build(context) { | |
return _builder(widget.scoped, setState); | |
} | |
} |
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:example/fstate.dart'; | |
import 'package:flutter/material.dart'; | |
void main() { | |
runApp(const MyApp()); | |
} | |
class MyApp extends StatefulWidget { | |
const MyApp({super.key}); | |
@override | |
createState() => MyAppState(); | |
} | |
class MyAppState extends State<MyApp> { | |
String _title = 'Flutter Demo Home Page'; | |
void updateTitle(String title) { | |
setState(() { | |
_title = title; | |
}); | |
} | |
@override | |
build(context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
theme: ThemeData( | |
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple), | |
useMaterial3: true, | |
), | |
home: MyHomePage( | |
title: _title, | |
updateTitle: updateTitle, | |
), | |
); | |
} | |
} | |
class MyHomePage extends StatelessWidget { | |
final String title; | |
final void Function(String title) updateTitle; | |
const MyHomePage({ | |
super.key, | |
required this.title, | |
required this.updateTitle, | |
}); | |
@override | |
build(context) { | |
return FState<MyHomePage>( | |
this, | |
() { | |
int counter = 0; | |
final controller = TextEditingController(); | |
controller.addListener( | |
() { | |
updateTitle(controller.text); | |
}, | |
); | |
return (widget, setState) { | |
return Scaffold( | |
appBar: AppBar( | |
backgroundColor: Theme.of(context).colorScheme.inversePrimary, | |
title: Text(widget.title), | |
), | |
body: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: [ | |
const Text('You have pushed the button this many times:'), | |
TextField(controller: controller), | |
Text( | |
'$counter', | |
style: Theme.of(context).textTheme.headlineMedium, | |
), | |
], | |
), | |
), | |
floatingActionButton: FloatingActionButton( | |
onPressed: () { | |
setState(() { | |
counter++; | |
}); | |
}, | |
tooltip: 'Increment', | |
child: const Icon(Icons.add), | |
), | |
); | |
}; | |
}, | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment