Skip to content

Commit

Permalink
chore: compatible with build() in framework
Browse files Browse the repository at this point in the history
  • Loading branch information
luo3house committed Oct 24, 2023
1 parent 0c55d9b commit 0fc3023
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 32 deletions.
26 changes: 3 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,12 @@ An easy way to create Functional Components (FC) in Flutter, with composable hoo
- 🧱 Hot reload
- ⚛️ React style friendly

![About 50% shrink](./image/fc.jpg)

## Install

```yaml
dependencies:
flutter_fc: <latest version>
```
>
> If need destructuring. Dart 3 or greater version is required.
> https://dart.dev/resources/dart-3-migration
>
> ```yaml
> environment:
> sdk: '^>=3.0.3 <4.0.0'
> ```

## Quick Example
Expand All @@ -46,7 +35,7 @@ class Counter extends FCWidget {
const Counter({super.key});
@override
Widget build() {
Widget build(BuildContext context) {
final (counter, setCounter) = useState(0);
return ElevatedButton(
onPressed: () => setCounter(counter + 1),
Expand Down Expand Up @@ -75,7 +64,7 @@ Currently supports these hooks as following:
### useState

```dart
// Dart 3
// Dart >= 3
final (flag, setFlag) = useState(false);
// Dart >= 2.12 < 3.0.0
Expand Down Expand Up @@ -123,15 +112,6 @@ useImperativeHandle(reloadRef, () {
reloadRef.current?.call();
```

### useBuildContext

Retrieve current FC context

```dart
final context = useBuildContext();
final theme = Theme.of(context);
```

## Development Tips

### Define Reusable Widgets
Expand All @@ -142,7 +122,7 @@ class Counter extends FCWidget {
Counter({this.value, super.key});
@override
Widget build() {
Widget build(BuildContext context) {
final (counter, setCounter) = useState(value ?? 0);
useEffect(() => setCounter(value ?? 0), [value]);
return Text("Counter: $counter"");
Expand Down
6 changes: 3 additions & 3 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class CounterScreen extends FCWidget {
const CounterScreen({super.key});

@override
Widget build() {
Widget build(BuildContext context) {
// sdk 2.x interop
// final (counter, setCounter) = useState(0);
final state = useState(0);
Expand All @@ -36,7 +36,7 @@ class ErrorTestScreen extends FCWidget {
const ErrorTestScreen({super.key});

@override
Widget build() {
Widget build(BuildContext context) {
flag++;
final value = flag > 1 ? useMemo(() => 1, []) : useRef(0).current;
return Text("$value");
Expand Down Expand Up @@ -68,7 +68,7 @@ class OverlayHierScreen extends FCWidget {
const OverlayHierScreen({super.key});

@override
Widget build() {
Widget build(BuildContext context) {
// sdk 2.x interop
// final (counter, setCounter) = useState(0);
final state = useState(0);
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ packages:
path: ".."
relative: true
source: path
version: "0.3.0"
version: "0.3.1"
flutter_lints:
dependency: "direct dev"
description:
Expand Down
Binary file removed image/fc.jpg
Binary file not shown.
6 changes: 3 additions & 3 deletions lib/src/fc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ class _FCPropsWidget<Props> extends Widget implements _FCWidget {
Element createElement() => _FCElement<_FCPropsWidget>(this);

@override
Widget build() => builder(props, ref);
Widget build(BuildContext context) => builder(props, ref);
}

/// [StatelessElement]
Expand Down Expand Up @@ -287,7 +287,7 @@ class _FCElement<T extends _FCWidget> extends ComponentElement
_kCurrentDispatcher = _FcUpdateDispatcher(this);
}
try {
final built = widget.build();
final built = widget.build(this);
memoizedHooks = _kCurrentDispatcher!.memoizedHooks;
return built;
} catch (e) {
Expand All @@ -300,7 +300,7 @@ class _FCElement<T extends _FCWidget> extends ComponentElement
}

abstract class _FCWidget implements Widget {
Widget build();
Widget build(BuildContext context);
}

class FCType implements Type {
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: flutter_fc
description: Writing Function Component in Flutter, just as writing in React.
version: 0.3.1
version: 0.4.0
homepage: https://github.com/luo3house/flutter_fc

environment:
Expand Down
2 changes: 1 addition & 1 deletion test/shim_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class _MyFCWidget extends FCWidget {
const _MyFCWidget(this.name);

@override
Widget build() {
Widget build(BuildContext context) {
final (text, setText) = useState<String?>(null);
final greetings = useMemo(() => "Hello $name", [name]);
useEffect(() {
Expand Down

0 comments on commit 0fc3023

Please sign in to comment.