Last active
August 5, 2023 12:52
-
-
Save esDotDev/67e23a065d76fec2749804326daef557 to your computer and use it in GitHub Desktop.
Shows a simple way to separate logic and ui code using a `StatefulWidget` + a private `StatelessWidget`.
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
/// 1. The public facing widget, configuration options live here | |
class MyWidget extends StatefulWidget { | |
@override | |
_MyWidgetState createState() => _MyWidgetState(); | |
} | |
/// 2. The state / controller / bloc / view controller | |
/// event handlers and helper methods can live here | |
class _MyWidgetState extends State<MyWidget> { | |
void _handlePressed() => print('todo'); | |
@override | |
Widget build(BuildContext context) => _MyWidgetStateView(this); | |
} | |
/// 3. The stateless UI, uses `_state` to accomplish tasks, avoiding boilerplate of param passing and callbacks | |
/// Big blobs of layout can live here | |
class _MyWidgetStateView extends StatelessWidget { | |
const _MyWidgetStateView(this.state, {Key? key}) : super(key: key); | |
final _MyWidgetState state; | |
@override | |
Widget build(BuildContext context) => TextButton(onPressed: state._handlePressed, child: FlutterLogo()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment