Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 38 additions & 47 deletions example/lib/combine_example/combine_example.dart
Original file line number Diff line number Diff line change
@@ -1,63 +1,54 @@
import 'dart:async';

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

final _ref = CombineRef<String>([
ref(0),
ref('I go second'),
futureRef(
() async {
await Future.delayed(
const Duration(
seconds: 3,
),
);
return 'I go third';
},
),
CombineRef(
[
futureRef(() async {
return 'I go fourth';
}),
transformRef<String>(
Ref('HEHE'),
debounceTransformer(
const Duration(
milliseconds: 500,
),
),
)
],
(ref) {
return ref.join(',');
},
),
], (ref) {
return 'Combined ref: ${ref.join(',')}';
});
final _countRef = ref(0);
final _prefixRef = ref('Result');

// computedRef tự động phát hiện dependency vào _countRef và _prefixRef
final _labelRef = computedRef<String>(
() => '${_prefixRef.state}: ${_countRef.state}',
);

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

class CombineExample extends StatelessWidget {
const CombineExample({
super.key,
});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Computed Example')),
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ReactiveWidget<String>(
ref: _ref,
builder: (context, value) => Text(value),
ref: _labelRef,
builder: (context, value) => Text(
value,
style: const TextStyle(fontSize: 24),
),
),
const SizedBox(height: 20),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: () => _countRef.update((v) => v - 1),
child: const Text('-'),
),
const SizedBox(width: 16),
ElevatedButton(
onPressed: () => _countRef.update((v) => v + 1),
child: const Text('+'),
),
],
),
const SizedBox(height: 12),
ElevatedButton(
onPressed: () {},
child: const Text(
'Update state',
),
)
onPressed: () {
_prefixRef.state =
_prefixRef.state == 'Result' ? 'Count' : 'Result';
},
child: const Text('Toggle Prefix'),
),
],
),
);
Expand Down
26 changes: 10 additions & 16 deletions example/lib/improved_example/improved_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,13 @@ class User {
String toString() => 'User(name: $name, age: $age)';
}

// Tạo các ref
// Tạo các ref nguồn
final nameRef = ref('John');
final ageRef = ref(30);

// Sử dụng combine2Refs để kết hợp 2 ref
final userRef = combine2Refs<String, int, User>(
nameRef,
ageRef,
(name, age) => User(name: name, age: age),
// computedRef tự động phát hiện dependency vào nameRef và ageRef
final userRef = computedRef<User>(
() => User(name: nameRef.state, age: ageRef.state),
);

// Sử dụng select để chỉ lắng nghe một phần của state
Expand Down Expand Up @@ -67,16 +65,14 @@ class _ImprovedExampleState extends State<ImprovedExample> {
// Đăng ký effect và lưu hàm dispose
_disposeEffect = userRef.effect((user) {
print('User changed: $user');
// Có thể thực hiện các side effect khác ở đây
// Ví dụ: lưu vào local storage, gọi API, v.v.
});
}

@override
void dispose() {
nameController.dispose();
ageController.dispose();
_disposeEffect(); // Hủy effect khi widget bị dispose
_disposeEffect();
super.dispose();
}

Expand All @@ -91,19 +87,18 @@ class _ImprovedExampleState extends State<ImprovedExample> {
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// Sử dụng RefProvider để cung cấp ref cho widget tree
RefProvider<User>(
ref: userRef,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text('User Info (using RefConsumer):'),
// Sử dụng RefConsumer để tiêu thụ ref
RefConsumer<User>(
builder: (context, user) {
return Text(
'Name: ${user.name}, Age: ${user.age}',
style: TextStyle(fontWeight: FontWeight.bold),
style:
const TextStyle(fontWeight: FontWeight.bold),
);
},
),
Expand All @@ -112,7 +107,6 @@ class _ImprovedExampleState extends State<ImprovedExample> {
),
),
const Text('Name (using ReactiveWidget with select):'),
// Sử dụng ReactiveWidget với select
ReactiveWidget(
ref: userNameRef,
builder: (context, name) {
Expand Down Expand Up @@ -154,9 +148,9 @@ class _ImprovedExampleState extends State<ImprovedExample> {
const SizedBox(height: 20),
ElevatedButton(
onPressed: () {
// Cập nhật trực tiếp userRef
userRef.state = User(name: 'Alice', age: 25);
// Cập nhật controllers để phản ánh giá trị mới
// Cập nhật các ref nguồn — userRef sẽ tự động recompute
nameRef.state = 'Alice';
ageRef.state = 25;
nameController.text = 'Alice';
ageController.text = '25';
},
Expand Down
2 changes: 1 addition & 1 deletion lib/src/base/base.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export 'future_ref.dart';
export 'ref.dart';
export 'base_ref.dart';
export 'combine_ref.dart';
export 'computed_ref.dart';
export 'transform_ref.dart';
export 'family_ref.dart';
export 'readonly_ref.dart';
Expand Down
9 changes: 8 additions & 1 deletion lib/src/base/base_ref.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:flutter/foundation.dart';
import 'package:ref/ref.dart';
import 'package:ref/src/base/tracking_context.dart';
import 'package:ref/src/listener_manager/listener_manager.dart';
import 'package:ref/src/observer/global_observer_manager.dart';
import 'package:ref/src/observer/ref_observer_mixin.dart';
Expand Down Expand Up @@ -62,7 +63,13 @@ abstract class BaseRef<T> with RefObserverMixin {
bool shouldUpdate(T oldState, T newState) => oldState != newState;

/// Lấy state hiện tại
T get state => _state;
///
/// Nếu đang trong quá trình tracking (ComputedRef đang chạy compute),
/// ref này sẽ tự động được đăng ký là dependency.
T get state {
RefTrackingContext.onAccess?.call(this);
return _state;
}

/// Cập nhật state và thông báo cho các listener
///
Expand Down
106 changes: 0 additions & 106 deletions lib/src/base/combine_ref.dart

This file was deleted.

Loading