flutter_fc 1.2.0 copy "flutter_fc: ^1.2.0" to clipboard
flutter_fc: ^1.2.0 copied to clipboard

Writing Function Component in Flutter, just as writing in React.

() => Text("FC in Flutter") #

Pub Version Github Action

An easy way to create Functional Components (FC) in Flutter, with composable hooks.

The FC has been deployed in some production app builds. FC aims to save your time.

Features #

  • ⏱️ No need to generate codes
  • 🖨️ No need to verbose StateXXXWidget & State<XXX> classes
  • 📄 Tiny implementations without external deps
  • 🪝 Built-in powerful composable hooks
  • 🐇 Speed up developing
  • 🎯 Focus on performance optimization
  • 🧱 Hot reload
  • ⚛️ React style friendly

Install #

dependencies:
  flutter_fc: ^1.0.0
copied to clipboard

Quick Start #

No need to create a StatefulWidget class and a State for it.

class Counter extends FCWidget {
  const Counter({super.key});

  @override
  Widget build(BuildContext context) {
    final (counter, setCounter) = useState(0);
    return ElevatedButton(
      onPressed: () => setCounter(counter + 1),
        child: Text("Counter: $counter"),
    );
  }
}
copied to clipboard

Dynamically create a temporary widget type, Not recommended.

final Counter = defineFC((context, props) {
  final (counter, setCounter) = useState(0);
  return ElevatedButton(
    onPressed: () => setCounter(counter + 1),
      child: Text("Counter: $counter"),
  );
});
copied to clipboard

Using hooks #

useState #

Create or restore with initial value stored in element, and get a function to let it update and rebuild.

final (loading, setLoading) = useState(false);
copied to clipboard

useSetState instead in case of just want to trigger an rebuild.

final update = useSetState();

update(); // trigger an rebuild
copied to clipboard

useIsMounted #

Return a function, call to get whether element is mounted.

final isMounted = useIsMounted();

Timer(const Duration(seconds: 3), () {
  if (isMounted()) {
    // element is still present
  }
});
copied to clipboard

useElement #

Retrieve current building element. It inherits BuildContext so...

final context = useElement();
final theme = Theme.of(context);
final navigator = Navigator.of(context);
copied to clipboard

useDidChangeDependencies #

Post a callback, called on element's dependencies were changed.

useReassemble #

Post a callback, called on element receives reassemble directive.

useReassemble(() => textController.clear());
copied to clipboard

useDispose #

Post a callback, called before element unmounts.

final timer = useMemo(() => Timer(...));
useDispose(timer.cancel);
copied to clipboard

useDiff #

Post a callback, called on dependencies are different from before.

final (flag, setFlag) = useState(false);
useDiff(() {
  print("Flag is changed to: $flag");

  // DO NOT TRIGGER UPDATE HERE setFlag(false);
}, [flag]);
copied to clipboard

useMemo #

Give a factory to create value, get the same object on each build until dependencies were changed.

final (percent, setPercent) = useState(20);
final prettierPercent = useMemo(() => "${percent} %", [percent]);
copied to clipboard

useRef #

Create or restore with initial value stored in a Ref, which holds the value only.

final timerRef = useRef<Timer>(); // nullable

final flagRef = useRefMust(false); // not null
copied to clipboard

useValue #

Create or restore with initial value stored in an ValueNotifier, which update listeners on its value has changed.

final loading = useValue(() => false);

setLoading(bool newValue) => loading.value = newValue;

return ValueListenableBuilder(
  valueListenable: loading,
  builder: (context, flag, child) => flag
    ? const Text("Loading") 
    : const SizedBox(),
);
copied to clipboard

useDisposable #

Create or restore a disposable instance. It may be called with .disposed() if it inherits from ChangeNotifier or StreamSink,

Commonly used descendant classes:

  • ChangeNotifier
  • ValueNotifier
  • StreamController
  • FocusNode
  • TextEditingController
// auto disposed on unmount
final controller = useDisposable(() => TextEditingController());
copied to clipboard

Acknowledgement #

React

Dart 3

License #

MIT (c) 2023-present, Luo3House.

3
likes
130
points
35
downloads

Publisher

unverified uploader

Weekly Downloads

2025.01.07 - 2025.12.02

Writing Function Component in Flutter, just as writing in React.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

flutter

More

Packages that depend on flutter_fc