展示 HN: Flutter_compositions:受 Vue 启发的 Flutter 响应式构建块
Show HN: Flutter_compositions: Vue-inspired reactive building blocks for Flutter

原始链接: https://github.com/yoyo930021/flutter_compositions

## Flutter Compositions:为 Flutter 带来的 Vue 风格响应式编程 Flutter Compositions 将 Vue 3 的 Composition API 的强大功能带到 Flutter 开发中。它提供了一种声明式且简洁的方式来管理状态和逻辑,使用了像 `ref`、`computed`、`watch` 和 `watchEffect` 这样的熟悉概念。 它构建于 `alien_signals` 之上,提供细粒度的响应式,从而实现高效的更新和优化的性能。主要特性包括通过自定义 composables 实现可组合逻辑,使用 `provide/inject` 进行类型安全的依赖注入,以及内置的 composables 用于常见任务,如控制器和动画。 该库通过单个 `setup()` 函数简化开发,取代了传统的生命周期方法并减少了样板代码。它还包含自定义 lint 规则以鼓励最佳实践。该项目是一个 Melos monorepo,提供结构化的开发环境和易于测试的机制。欢迎贡献!

Hacker News 新闻 | 过去 | 评论 | 提问 | 展示 | 招聘 | 提交 登录 展示 HN: Flutter_compositions: 受 Vue 启发的 Flutter 响应式构建块 (github.com/yoyo930021) 8 分,yoyo930021 发布,1 小时前 | 隐藏 | 过去 | 收藏 | 2 评论 SquareWheel 25 分钟前 [–] 这应该放在 Show HN 吗?回复 yoyo930021 11 分钟前 | 父评论 [–] 谢谢回复 考虑申请 YC 2026 冬季批次!申请截止日期为 11 月 10 日 指南 | 常见问题 | 列表 | API | 安全 | 法律 | 申请 YC | 联系方式 搜索:
相关文章

原文

Test Documentation pub package License: MIT

Vue-inspired reactive building blocks for Flutter

Flutter Compositions brings Vue 3's Composition API patterns to Flutter, enabling fine-grained reactivity and composable logic with a clean, declarative API.

📚 Read the full documentation →

This repository uses a Melos-managed monorepo layout:

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

class CounterPage extends CompositionWidget {
  const CounterPage({super.key});

  @override
  Widget Function(BuildContext) setup() {
    // Reactive state
    final count = ref(0);
    final doubled = computed(() => count.value * 2);

    // Side effects
    watch(() => count.value, (value, previous) {
      debugPrint('count: $previous → $value');
    });

    // Return builder
    return (context) => Scaffold(
          appBar: AppBar(title: const Text('Counter')),
          body: Center(
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                Text('Count: ${count.value}'),
                Text('Doubled: ${doubled.value}'),
              ],
            ),
          ),
          floatingActionButton: FloatingActionButton(
            onPressed: () => count.value++,
            child: const Icon(Icons.add),
          ),
        );
  }
}
  • Vue-inspired API - Familiar ref, computed, watch, and watchEffect
  • Fine-grained reactivity - Powered by alien_signals
  • Composable logic - Extract and reuse stateful logic with custom composables
  • Type-safe DI - provide/inject with InjectionKey
  • Built-in composables - Controllers, animations, async data, and more
  • Zero boilerplate - Single setup() function replaces multiple lifecycle methods
  • Lint rules - Custom lints enforce best practices

This is a Melos monorepo. To get started:

# Install Melos
flutter pub global activate melos

# Bootstrap the workspace
melos bootstrap

# Run tests across all packages
melos run test

# Run analysis
melos run analyze
cd packages/flutter_compositions/example
flutter run

Contributions are welcome! Please feel free to submit a Pull Request.

Flutter Compositions is built upon excellent work from the open source community:

  • alien_signals - Provides the core reactivity system with fine-grained signal-based state management
  • flutter_hooks - Inspired composable patterns and demonstrated the viability of composition APIs in Flutter

We are grateful to these projects and their maintainers for paving the way.

MIT © 2025

联系我们 contact @ memedata.com