Skip to content

Commit

Permalink
doc: Update RtWatcher widget documentation and examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
CarLeonDev committed Aug 15, 2024
1 parent a14cbf1 commit 0684b62
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 3 deletions.
82 changes: 79 additions & 3 deletions website/src/content/docs/widgets/rt_watcher.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,82 @@ sidebar:
order: 7
---

:::caution
This section is under construction.
:::
import { HM, HT, HN } from "@/components/Highlight";
import CodeTabs from '@/components/CodeTabs.astro';
import { Tabs, TabItem } from "@/components/Tabs";
import ZappButton from "@/components/ZappButton.astro";
import { Code } from "@astrojs/starlight/components";
import { marks } from '@/examples/marks.ts'

import counterCode from '@/examples/rt_watcher/lib/counter.dart?raw';
import counterViewCode from '@/examples/rt_watcher/lib/counter_view.dart?raw';
import mainCode from '@/examples/rt_watcher/lib/main.dart?raw';

The <HT><a href="https://pub.dev/documentation/flutter_reactter/latest/flutter_reactter/RtWatcher-class.html">`RtWatcher`</a></HT> widget allows you to watch states and automatically rebuilds the widget tree whenever a monitored state changes.

## Syntax

The <HT>`RtWatcher`</HT> widget has two ways to use it:

- **Using the default constructor**:

```dart showLineNumbers=false
RtWatcher(
Widget builder(
BuildContext context,
RtState watch(RtState state),
), {
Key? key,
},
)
```

- **Using the Builder constructor**:

```dart showLineNumbers=false
RtWatcher.builder({
Key? key,
Widget? child,
required Widget builder(
BuildContext context,
RtState watch(RtState state),
widget? child,
),
})
```

## Properties

- `key`: An optional <HT>`Key`</HT> to use for identifying the widget.
- `child`: An optional <HT>`Widget`</HT> that remains static while the widget tree is rebuilt.
Useful for parts of the UI that don't depend on the watched states and can remain unchanged.
It is passed to the <HM>`builder`</HM> function if it is defined.
- <HM>`builder`</HM>: A required function that returns a widget.
This function is called whenever any of the states being watched change.
It takes three parameters:
- `context`: The <HT>`BuildContext`</HT> of the <HT>`RtWatcher`</HT> widget.
- <HM>`watch`</HM>: A function to watch the state that should trigger a rebuild when it changes.
- `child`: The `child` widget passed to <HT>`RtWatcher`</HT>.

## Usage

The following example demonstrates how to use the <HT>`RtlWatcher`</HT> widget to listen for changes in a state and rebuild the widget tree accordingly.

<CodeTabs>
<ZappButton path="examples/rt_watcher"/>

<Tabs>
<TabItem>
<HM single slot="label">counter.dart</HM>
<Code code={counterCode} lang="dart" mark={[...marks, {range: "11-38"}]}/>
</TabItem>

<TabItem label="counter_view.dart">
<Code code={counterViewCode} lang="dart" mark={marks} />
</TabItem>

<TabItem label="main.dart">
<Code code={mainCode} lang="dart" mark={marks} />
</TabItem>
</Tabs>
</CodeTabs>
41 changes: 41 additions & 0 deletions website/src/examples/rt_watcher/lib/counter.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import 'package:flutter/material.dart';
import 'package:flutter_reactter/flutter_reactter.dart';

class Counter extends StatelessWidget {
const Counter({Key? key}) : super(key: key);

@override
Widget build(BuildContext context) {
final count = UseState(0);

return RtWatcher.builder(
// This widget remains static and does not rebuild when `count` changes.
// It is passed as a `child` to the `builder` function.
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
child: const Icon(Icons.remove),
onPressed: () => count.value--,
),
SizedBox(width: 8),
ElevatedButton(
child: const Icon(Icons.add),
onPressed: () => count.value++,
),
],
),
// Rebuid the widget when `count` changes
builder: (context, watch, child) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text("Count: ${watch(count).value}"),
SizedBox(height: 8),
child!,
],
);
},
);
}
}
18 changes: 18 additions & 0 deletions website/src/examples/rt_watcher/lib/counter_view.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import 'package:flutter/material.dart';
import 'counter.dart';

class CounterView extends StatelessWidget {
const CounterView({Key? key}) : super(key: key);

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Counter"),
),
body: const Center(
child: Counter(),
),
);
}
}
15 changes: 15 additions & 0 deletions website/src/examples/rt_watcher/lib/main.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import 'package:flutter/material.dart';
import 'counter_view.dart';

void main() {
runApp(MyApp());
}

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: CounterView(),
);
}
}
14 changes: 14 additions & 0 deletions website/src/examples/rt_watcher/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: RtWatcher
description: RtWatcher example
version: 0.1.0

environment:
sdk: ">=2.19.0 <4.0.0"

dependencies:
flutter:
sdk: flutter
flutter_reactter: ^7.3.1

flutter:
uses-material-design: true

0 comments on commit 0684b62

Please sign in to comment.