Skip to content

Commit

Permalink
feat: Export experimental inline FC
Browse files Browse the repository at this point in the history
  • Loading branch information
luo3house committed Sep 14, 2024
1 parent 5c46b98 commit d420167
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 58 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.2.0

* Export experimental inline FC

## 1.1.0

* Add optional parameter constructor for "Ref"
Expand Down
2 changes: 1 addition & 1 deletion lib/flutter_fc_compat.dart
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export 'src/fc_compat_v0.dart'
show defineFC, useEffect, useImperativeHandle, useBuildContext;
show useEffect, useImperativeHandle, useBuildContext;
1 change: 1 addition & 0 deletions lib/flutter_fc_experimental.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export './src/fc_define_fc.dart';
57 changes: 1 addition & 56 deletions lib/src/fc_compat_v0.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';

import 'fc_core.dart';
import 'fc_define_fc.dart';
import 'fc_lib.dart';
export 'fc_foundation.dart' show useState;

Expand All @@ -12,55 +13,6 @@ typedef ForwardRefFC<Props> = Widget Function(
BuildContext context, Props? props, Key? ref);
typedef FC<Props> = Widget Function(BuildContext context, Props? props);

var kFCIndex = 0;
int nextFCId() => ++kFCIndex;

class FCPropsWidget<Props> extends Widget implements FCWidget {
final String name;
final WidgetKeyBuilder<Props> builder;
final Props props;
final Key? ref;

const FCPropsWidget(
this.builder,
this.props,
this.ref,
this.name, {
super.key,
});

@override
Type get runtimeType => FCType("${super.runtimeType}\$$name");

@override
Element createElement() => FCElement<FCPropsWidget>(this);

@override
Widget build(BuildContext context) {
return builder(context, props, ref);
}
}

class FCType implements Type {
final String name;
FCType(this.name);

String get fullName => "FCType_$name";

@override
int get hashCode => fullName.hashCode;

@override
bool operator ==(Object other) {
return other is FCType && other.name == name;
}

@override
String toString() {
return "${super.toString()}(fullName=$fullName)";
}
}

BuildContext useBuildContext() {
return useElement();
}
Expand All @@ -76,13 +28,6 @@ Please use "useDiff" with "addPostFrameCallback" instead.
useDangerousAfterRebuild(effect, deps);
}

MemoizedFC<Props> defineFC<Props>(FC<Props> fn) {
final name = "defineFC_${nextFCId()}";
return ({props, ref, key}) => FCPropsWidget(
(context, props, ref) => fn(context, props), props, ref, name,
key: key);
}

MemoizedFC<Props> forwardRef<Props>(ForwardRefFC<Props> fn) {
final name = "forwardRefStatefulFC_${nextFCId()}";
return ({props, ref, key}) {
Expand Down
67 changes: 67 additions & 0 deletions lib/src/fc_define_fc.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import 'package:flutter/widgets.dart';

import 'fc_core.dart';
export 'fc_foundation.dart' show useState;

typedef WidgetKeyBuilder<Props> = Widget Function(
BuildContext context, Props? props, Key? key);
typedef MemoizedFC<Props> = Widget Function({Props? props, Key? ref, Key? key});
typedef ForwardRefFC<Props> = Widget Function(
BuildContext context, Props? props, Key? ref);
typedef FC<Props> = Widget Function(BuildContext context, Props? props);

var kDangerousFCIndex = 0;
int nextFCId() => ++kDangerousFCIndex;

class FCPropsWidget<Props> extends Widget implements FCWidget {
final String name;
final WidgetKeyBuilder<Props> builder;
final Props props;
final Key? ref;

const FCPropsWidget(
this.builder,
this.props,
this.ref,
this.name, {
super.key,
});

@override
Type get runtimeType => FCType("${super.runtimeType}\$$name");

@override
Element createElement() => FCElement<FCPropsWidget>(this);

@override
Widget build(BuildContext context) {
return builder(context, props, ref);
}
}

class FCType implements Type {
final String name;
FCType(this.name);

String get fullName => "FCType_$name";

@override
int get hashCode => fullName.hashCode;

@override
bool operator ==(Object other) {
return other is FCType && other.name == name;
}

@override
String toString() {
return "${super.toString()}(fullName=$fullName)";
}
}

MemoizedFC<Props> defineFC<Props>(FC<Props> fn) {
final name = "defineFC_${nextFCId()}";
return ({props, ref, key}) => FCPropsWidget(
(context, props, ref) => fn(context, props), props, ref, name,
key: key);
}
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: 1.1.0
version: 1.2.0
homepage: https://github.com/luo3house/flutter_fc

environment:
Expand Down

0 comments on commit d420167

Please sign in to comment.