Skip to content

Instantly share code, notes, and snippets.

@iamprincesly
Created May 27, 2021 09:02
Show Gist options
  • Save iamprincesly/9895a443ee4c84dd1c50fddd893bf860 to your computer and use it in GitHub Desktop.
Save iamprincesly/9895a443ee4c84dd1c50fddd893bf860 to your computer and use it in GitHub Desktop.
Flutter Statelessful widget (inline stateful widgets)
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