From d420167b01eec4c18f0b4d89276ee74dc5a528d5 Mon Sep 17 00:00:00 2001 From: luoyz Date: Sat, 14 Sep 2024 21:01:13 +0800 Subject: [PATCH] feat: Export experimental inline FC --- CHANGELOG.md | 4 ++ lib/flutter_fc_compat.dart | 2 +- lib/flutter_fc_experimental.dart | 1 + lib/src/fc_compat_v0.dart | 57 +-------------------------- lib/src/fc_define_fc.dart | 67 ++++++++++++++++++++++++++++++++ pubspec.yaml | 2 +- 6 files changed, 75 insertions(+), 58 deletions(-) create mode 100644 lib/flutter_fc_experimental.dart create mode 100644 lib/src/fc_define_fc.dart diff --git a/CHANGELOG.md b/CHANGELOG.md index 3bc9f85..d73a805 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 1.2.0 + +* Export experimental inline FC + ## 1.1.0 * Add optional parameter constructor for "Ref" diff --git a/lib/flutter_fc_compat.dart b/lib/flutter_fc_compat.dart index 3430437..d6113df 100644 --- a/lib/flutter_fc_compat.dart +++ b/lib/flutter_fc_compat.dart @@ -1,2 +1,2 @@ export 'src/fc_compat_v0.dart' - show defineFC, useEffect, useImperativeHandle, useBuildContext; + show useEffect, useImperativeHandle, useBuildContext; diff --git a/lib/flutter_fc_experimental.dart b/lib/flutter_fc_experimental.dart new file mode 100644 index 0000000..ad59044 --- /dev/null +++ b/lib/flutter_fc_experimental.dart @@ -0,0 +1 @@ +export './src/fc_define_fc.dart'; diff --git a/lib/src/fc_compat_v0.dart b/lib/src/fc_compat_v0.dart index af54494..c4a3d1e 100644 --- a/lib/src/fc_compat_v0.dart +++ b/lib/src/fc_compat_v0.dart @@ -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; @@ -12,55 +13,6 @@ typedef ForwardRefFC = Widget Function( BuildContext context, Props? props, Key? ref); typedef FC = Widget Function(BuildContext context, Props? props); -var kFCIndex = 0; -int nextFCId() => ++kFCIndex; - -class FCPropsWidget extends Widget implements FCWidget { - final String name; - final WidgetKeyBuilder 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(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(); } @@ -76,13 +28,6 @@ Please use "useDiff" with "addPostFrameCallback" instead. useDangerousAfterRebuild(effect, deps); } -MemoizedFC defineFC(FC fn) { - final name = "defineFC_${nextFCId()}"; - return ({props, ref, key}) => FCPropsWidget( - (context, props, ref) => fn(context, props), props, ref, name, - key: key); -} - MemoizedFC forwardRef(ForwardRefFC fn) { final name = "forwardRefStatefulFC_${nextFCId()}"; return ({props, ref, key}) { diff --git a/lib/src/fc_define_fc.dart b/lib/src/fc_define_fc.dart new file mode 100644 index 0000000..2b4e59d --- /dev/null +++ b/lib/src/fc_define_fc.dart @@ -0,0 +1,67 @@ +import 'package:flutter/widgets.dart'; + +import 'fc_core.dart'; +export 'fc_foundation.dart' show useState; + +typedef WidgetKeyBuilder = Widget Function( + BuildContext context, Props? props, Key? key); +typedef MemoizedFC = Widget Function({Props? props, Key? ref, Key? key}); +typedef ForwardRefFC = Widget Function( + BuildContext context, Props? props, Key? ref); +typedef FC = Widget Function(BuildContext context, Props? props); + +var kDangerousFCIndex = 0; +int nextFCId() => ++kDangerousFCIndex; + +class FCPropsWidget extends Widget implements FCWidget { + final String name; + final WidgetKeyBuilder 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(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 defineFC(FC fn) { + final name = "defineFC_${nextFCId()}"; + return ({props, ref, key}) => FCPropsWidget( + (context, props, ref) => fn(context, props), props, ref, name, + key: key); +} diff --git a/pubspec.yaml b/pubspec.yaml index 99731ef..07bbaa9 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -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: