Created
May 27, 2021 09:02
-
-
Save iamprincesly/9895a443ee4c84dd1c50fddd893bf860 to your computer and use it in GitHub Desktop.
Flutter Statelessful widget (inline stateful widgets)
This file contains 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 Statelessful<T> extends StatefulWidget { | |
final T Function() initialState; | |
final Widget Function( | |
BuildContext context, | |
T state, | |
void Function(T state) setState, | |
) builder; | |
Statelessful({ | |
Key key, | |
@required this.builder, | |
@required this.initialState, | |
}) : assert(builder != null), | |
assert(initialState != null), | |
super(key: key); | |
@override | |
_StatelessfulState<T> createState() => _StatelessfulState<T>(); | |
} | |
class _StatelessfulState<T> extends State<Statelessful<T>> { | |
T _state; | |
void _setState(T state) { | |
if (state != _state) { | |
setState(() { | |
_state = state; | |
}); | |
} | |
} | |
@override | |
void initState() { | |
super.initState(); | |
_state = widget.initialState(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return widget.builder(context, _state, _setState) ?? Container(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment