Skip to content

Commit

Permalink
4.16.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Drawner committed May 27, 2024
1 parent f2888e3 commit 2c5293b
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 34 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@

## 4.16.0
May 26, 2024
- The function, runAsync(), is *deprecated*.
Set to true the parameter, runAsync, instead.
- getter, InheritedWidgetStateMixin, moved to InheritedWidgetStateMixin

## 4.15.0
May 18, 2024
- _StateXInheritedWidget() to StateXInheritedWidget()
Expand Down
68 changes: 37 additions & 31 deletions lib/state_extended.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ abstract class StateX<T extends StatefulWidget> extends State<StatefulWidget>
StateX({StateXController? controller, bool? runAsync, bool? useInherited}) {
// Add to the list of StateX objects present in the app!
_addToMapOfStates(this);
// A flag whether the built-in FutureBuilder runs with every setState() call.
// A flag whether the built-in FutureBuilder always runs.
_runAsync = runAsync ?? false;
// A flag determining whether the built-in InheritedWidget is used or not.
_useInherited = useInherited ?? true;
Expand All @@ -80,16 +80,13 @@ abstract class StateX<T extends StatefulWidget> extends State<StatefulWidget>
StateXController? _controller;

/// Run the built-in FutureBuilder with every setState() call
late bool _runAsync;

/// You need to be able access the widget.
@override
// ignore: avoid_as
T get widget => super.widget as T;

/// A flag determining whether the built-in InheritedWidget is used or not.
bool get useInherited => _useInherited;
// /// A flag determining whether the built-in InheritedWidget is used or not.
// bool get useInherited => _useInherited;

/// Provide the 'main' controller to this 'State View.'
/// If _controller == null, get the 'first assigned' controller if any.
Expand Down Expand Up @@ -1165,6 +1162,7 @@ abstract class StateX<T extends StatefulWidget> extends State<StatefulWidget>
void setState(VoidCallback fn) {
//
if (_setStateAllowed) {
//
_setStateAllowed = false;

// Don't bother if the State object is disposed of.
Expand Down Expand Up @@ -1194,7 +1192,7 @@ abstract class StateX<T extends StatefulWidget> extends State<StatefulWidget>
}
// Copy over certain properties
_recException = state._recException;
_ranAsync = state._ranAsync;
_runAsync = state._runAsync;
}
}

Expand Down Expand Up @@ -1865,21 +1863,21 @@ mixin StateListener implements RouteAware {
);
}

/// The top route has been popped off, and this route shows up.
/// Called when this State is *first* added to as a Route observer?!
@override
void didPopNext() {}
void didPush() {}

/// Called when this route has been pushed.
/// New route has been pushed, and this State object's route is no longer current.
@override
void didPush() {}
void didPushNext() {}

/// Called when this route has been popped off.
/// Called when this State is popped off a route.
@override
void didPop() {}

/// New route has been pushed, and this route is no longer visible.
/// The top route has been popped off, and this route shows up.
@override
void didPushNext() {}
void didPopNext() {}

/// Called when the application's dimensions change. For example,
/// when a phone is rotated.
Expand Down Expand Up @@ -2009,8 +2007,9 @@ mixin FutureBuilderStateMixin on State {
Widget build(BuildContext context) {
// A little trick to determine if the user has overridden this function.
_buildOverridden = false;
// Generate the future just once or evey time
// Don't run runAsync() function if _ranAsync is true.
if (!_ranAsync || _future == null) {
if (!_ranAsync || _runAsync || _future == null) {
_future = runAsync();
}
return FutureBuilder<bool>(
Expand All @@ -2025,31 +2024,35 @@ mixin FutureBuilderStateMixin on State {
bool get buildOverridden => _buildOverridden;
bool _buildOverridden = true;

/// Run the StateX object's initAsync() function
/// Override this function to repeatedly run initAsync()
@Deprecated('Set to true the runAsync parameter instead.')
Future<bool> runAsync() {
// Once true, initAsync() function is never run again
// unless the runAsync() function is overridden.
_ranAsync = true;
return initAsync();
}

/// Don't call runAsync() and initAsync() ever again once this is true.
bool _ranAsync = false;

/// Clean up
@override
void dispose() {
_future = null;
super.dispose();
}

/// Don't call runAsync() and initAsync() ever again once this is true.
bool _ranAsync = false;
// Call initAsync() all the time if set true.
bool _runAsync = false;

/// IMPORTANT
/// The _future must be created first. If the _future is created at the same
/// time as the FutureBuilder, then every time the FutureBuilder's parent is
/// rebuilt, the asynchronous task will be performed again.
Future<bool>? _future;

/// Run the StateX object's initAsync() function
/// Override this function to repeatedly run initAsync()
Future<bool> runAsync() {
// Once true, initAsync() function is never run again
// unless the runAsync() function is overridden.
_ranAsync = true;
return initAsync();
}

/// You're to override this function and initialize any asynchronous operations
Future<bool> initAsync() async => true;

Expand Down Expand Up @@ -2228,7 +2231,8 @@ mixin FutureBuilderStateMixin on State {
/// {@category StateX class}
/// {@category Using InheritedWidget}
mixin InheritedWidgetStateMixin on State {
// A flag determining whether the built-in InheritedWidget is used or not.
/// A flag determining whether the built-in InheritedWidget is used or not.
bool get useInherited => _useInherited;
late bool _useInherited;

// Collect any 'widgets' depending on this State's InheritedWidget.
Expand Down Expand Up @@ -2348,7 +2352,7 @@ mixin InheritedWidgetStateMixin on State {
Widget state(WidgetBuilder? widgetFunc) {
widgetFunc ??= (_) => const SizedBox();
return _useInherited && this is StateX
? _SetStateWidget(stateX: this as StateX, widgetFunc: widgetFunc)
? _SetStateXWidget(stateX: this as StateX, widgetFunc: widgetFunc)
: widgetFunc(context);
}

Expand Down Expand Up @@ -2393,8 +2397,10 @@ class StateXInheritedWidget extends InheritedWidget {
}

/// Supply a widget to depend upon a StateX's InheritedWidget
class _SetStateWidget extends StatelessWidget {
const _SetStateWidget({
class _SetStateXWidget extends StatelessWidget {
///
const _SetStateXWidget({
super.key,
required this.stateX,
required this.widgetFunc,
});
Expand Down Expand Up @@ -2551,7 +2557,7 @@ abstract class AppStateX<T extends StatefulWidget> extends StateIn<T>
@override
Widget state(WidgetBuilder? widgetFunc) {
widgetFunc ??= (_) => const SizedBox(); // Display 'nothing' if not provided
return _SetStateWidget(stateX: this, widgetFunc: widgetFunc);
return _SetStateXWidget(stateX: this, widgetFunc: widgetFunc);
}

/// Catch any errors in the App
Expand Down
4 changes: 2 additions & 2 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -279,10 +279,10 @@ packages:
dependency: "direct main"
description:
name: universal_platform
sha256: d315be0f6641898b280ffa34e2ddb14f3d12b1a37882557869646e0cc363d0cc
sha256: "64e16458a0ea9b99260ceb5467a214c1f298d647c659af1bff6d3bf82536b1ec"
url: "https://pub.dev"
source: hosted
version: "1.0.0+1"
version: "1.1.0"
vector_math:
dependency: transitive
description:
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: state_extended
description: This class extends the capabilities of Flutter's State class and includes a controller.
version: 4.15.0
version: 4.16.0
homepage: https://www.andrioussolutions.com
repository: https://github.com/AndriousSolutions/state_extended

Expand Down

0 comments on commit 2c5293b

Please sign in to comment.