Skip to content

Commit

Permalink
fix size listener bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
aprosail committed May 16, 2024
2 parents fa69768 + b1fc0b6 commit 0ea55dc
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 16 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
## 0.5.2

- Fix bug about calling `setState` when `build`.

## 0.5.1

- **Deprecated**: still contains bugs.
- Fix bug: get size when build conflict.

## 0.5.0

- **Deprecated**: still contains bugs.
- Widget size change listener.
- Wrap padding (edge insets).
- Text wrapper on string with parameters.
Expand Down
23 changes: 19 additions & 4 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,30 @@ void main() {
runApp(const App());
}

class App extends StatelessWidget {
class App extends StatefulWidget {
const App({super.key});

@override
State<App> createState() => _AppState();
}

class _AppState extends State<App> {
late var _height = MediaQuery.of(context).size.height;

@override
Widget build(BuildContext context) => 'app root'
.textWidget
.text(align: TextAlign.center)
.modifyTextStyle(
context,
(raw) => raw.copyWith(
color: const Color(0xfffefdfa),
fontSize: _height / 6.18,
fontWeight: FontWeight.w900,
),
)
.wrapPadding(const EdgeInsets.all(75))
.wrapCenter
.wrap((context, child) => child.wrapFontSize(context, 50))
.wrapForeground(context, const Color(0xffdedcda))
.wrapBackground(const Color(0xff804044))
.listenSizeChange(this, (size) => _height = size.height)
.ensureTextEnvironment(context);
}
26 changes: 15 additions & 11 deletions lib/src/size.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,25 @@ import 'package:flutter/widgets.dart';
extension WrapSize on Widget {
/// Listen the size change of this widget and handle [listener] as hook.
///
/// This wrapper should be placed close to root of the widget tree,
/// the size is based on the context of this widget,
/// so that the inner size change listener should be close to
/// where the context is build, or there might be potential conflicts.
Widget listenSizeChange(
BuildContext context,
void Function(Size size) listener,
) {
/// 1. The context is gotten from the [state] using [State.context].
/// 2. Don't call [State.setState] inside the [listener].
/// [State.setState] will be called automatically post frame.
/// Because calling it inside the [listener] will cause conflict
/// for it will call [State.setState] when building.
///
/// 3. This wrapper should be placed close to root of the widget tree,
/// the size is based on the context of this widget,
/// so that the inner size change listener should be close to
/// where the context is build, or there might be potential conflicts.
Widget listenSizeChange(State state, void Function(Size size) listener) {
return NotificationListener(
onNotification: (notification) {
if (notification is SizeChangedLayoutNotification) {
final context = state.context;
listener(context.size ?? MediaQuery.of(context).size);
WidgetsBinding.instance.addPostFrameCallback(
(timestamp) => listener(
context.size ?? MediaQuery.of(context).size,
),
// ignore: invalid_use_of_protected_member
(timestamp) => state.setState(() {}),
);
return true;
}
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: wrap
description: Chain style programming syntax sugar utilities for flutter widgets.
version: 0.5.1
version: 0.5.2
repository: https://github.com/aprosail/wrap
environment: {sdk: ">=3.3.4 <4.0.0", flutter: ">=3.19.6"}
topics:
Expand Down

0 comments on commit 0ea55dc

Please sign in to comment.