hook_state 0.1.0 copy "hook_state: ^0.1.0" to clipboard
hook_state: ^0.1.0 copied to clipboard

A Flutter package inspired by React hooks and the `flutter_hooks` package. It offers a similar hooks experience but without the need for additional widgets, allowing you to use just `StatefulWidget` t [...]

example/lib/main.dart

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:hook_state/hook_state.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const HomePage(),
    );
  }
}

final counter = ValueNotifier(0);
final counter2 = ValueNotifier(0);

final controller = StreamController<int>.broadcast();

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> with HookStateMixin {
  @override
  Widget build(BuildContext context) {
    final state = useNotifier(0);
    final state2 = useValueNotifier(counter);
    final state3 = useStream(controller.stream, 0);
    useCallback([counter2], () {
      print('Only callback ${counter.value}');
      final snackbar = SnackBar(content: Text('Counter: ${counter.value}'));
      ScaffoldMessenger.of(context).showSnackBar(snackbar);
    });

    return Scaffold(
      body: Center(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            ElevatedButton(
              onPressed: () {
                state.value++;
              },
              child: Text('useNotifier ${state.value}'),
            ),
            ElevatedButton(
              onPressed: () {
                counter.value++;
              },
              child: Text('useValueNotifier - $state2'),
            ),
            ElevatedButton(
              onPressed: () {
                controller.add(state3 + 1);
              },
              child: Text('useStream - $state3'),
            ),
            ElevatedButton(
              onPressed: () {
                counter2.value++;
              },
              child: const Text('Callback'),
            ),
          ],
        ),
      ),
    );
  }
}
8
likes
0
pub points
59%
popularity

Publisher

verified publisherflutterando.com.br

A Flutter package inspired by React hooks and the `flutter_hooks` package. It offers a similar hooks experience but without the need for additional widgets, allowing you to use just `StatefulWidget` to manage complex states declaratively and reactively.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

flutter

More

Packages that depend on hook_state