Skip to content

Commit

Permalink
Merge pull request #9
Browse files Browse the repository at this point in the history
improve_view_model
  • Loading branch information
shubham16g authored Jan 19, 2023
2 parents a8c8a16 + 968b347 commit e6a0710
Show file tree
Hide file tree
Showing 11 changed files with 19 additions and 134 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.5.2
Fixed Bugs and make package simpler
- ViewModelStatelessWidget removed
- Fixed StateFlow notifyOnSameValue

## 0.5.1
Easy to implement ViewModel and example improved
- ViewModelStatelessWidget added
Expand Down
34 changes: 0 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ An Android similar state management package which helps to implement MVVM patter
- SharedFlow, MutableSharedFlow 🌊
- ViewModel (to separate the view logic from UI like Cubit)
- ViewModelProvider
- ViewModelStatelessWidget
- StateFlowBuilder
- StateFlowConsumer
- StateFlowListener
Expand Down Expand Up @@ -213,39 +212,6 @@ OR
context.vm<CustomViewModel>()
```

### ViewModelStatelessWidget
**ViewModelStatelessWidget** helps to integrate `ViewModel` into `StatelessWidget` easily. Using this, we can go from:
```dart
class MyPage extends StatelessWidget {
const CounterPage({super.key});
@override
Widget build(BuildContext context) {
return ViewModelProvider(
create: (context) => MyViewModel(), // provide your custom viewModel
child: Scaffold(
// content here
),
);
}
}
```
to:
```dart
class MyPage extends ViewModelStatelessWidget<MyViewModel> {
const CounterPage({super.key});
@override
MyViewModel createViewModel(BuildContext context) => MyViewModel();
@override
Widget buildWithViewModel(BuildContext context, MyViewModel viewModel) {
return Scaffold(
// content here. For ViewModel instance, use `viewModel`
);
}
}
```
## Builder, Listener, and Consumer Flutter Widgets

### StateFlowBuilder
Expand Down
22 changes: 11 additions & 11 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,25 +51,23 @@ class MyApp extends StatelessWidget {
useMaterial3: true,
primarySwatch: Colors.blue,
),
home: const HomePage(),
home: ViewModelProvider(
create: (context) => CounterViewModel(), child: const HomePage()),
);
}
}

class HomePage extends ViewModelStatelessWidget<CounterViewModel> {
const HomePage({Key? key}) : super(key: key);
class HomePage extends StatelessWidget {
const HomePage({super.key});

@override
CounterViewModel createViewModel(BuildContext context) => CounterViewModel();

@override
Widget buildWithViewModel(BuildContext context, CounterViewModel viewModel) {
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('ViewModel Example')),
// implement SharedFlowListener anywhere in code to listen for emits from sharedFlow
body: SharedFlowListener(
// pass your SharedFlow
sharedFlow: viewModel.messageSharedFlow,
sharedFlow: context.vm<CounterViewModel>().messageSharedFlow,
listener: (context, value) {
// get the emitted value. in this case <String>"Hello from ViewModel!"
ScaffoldMessenger.of(context)
Expand All @@ -84,7 +82,8 @@ class HomePage extends ViewModelStatelessWidget<CounterViewModel> {
// implement ViewModelBuilder to rebuild Text on StateFlow value changed/updated
child: StateFlowBuilder(
// pass your StateFlow
stateFlow: viewModel.counterStateFlow,
stateFlow: ViewModelProvider.of<CounterViewModel>(context)
.counterStateFlow,
builder: (context, value) {
return Text(
"$value",
Expand All @@ -103,15 +102,16 @@ class HomePage extends ViewModelStatelessWidget<CounterViewModel> {
color: Theme.of(context).colorScheme.primary,
onPressed: () {
// call the showPopupMessage function which is inside CounterViewModel
viewModel.showPopupMessage();
ViewModelProvider.of<CounterViewModel>(context)
.showPopupMessage();
},
icon: const Icon(Icons.mail_outline),
),
const SizedBox(width: 12),
FloatingActionButton(
onPressed: () {
// call the increment function which is inside CounterViewModel
viewModel.increment();
context.vm<CounterViewModel>().increment();
},
child: const Icon(Icons.add),
),
Expand Down
2 changes: 0 additions & 2 deletions example/lib/more_examples_section.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import 'package:example/multiple_view_models_example/multiple_view_models_example.dart';
import 'package:example/post_frame_callback_example/post_frame_callback_example.dart';
import 'package:example/view_model_stateless_widget_example/view_model_stateless_example.dart';
import 'package:flutter/material.dart';

final _moreExamples = {
"Multiple ViewModels Example": const MultipleViewModelsExample(),
"PostFrameCallback Example": const PostFrameCallbackExample(),
"ViewModelStatelessWidget Example": const ViewModelStatelessWidgetExample(),
};

class MoreExamplesSection extends StatelessWidget {
Expand Down

This file was deleted.

This file was deleted.

2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ packages:
path: ".."
relative: true
source: path
version: "0.5.0"
version: "0.5.2"
sdks:
dart: ">=2.18.6 <3.0.0"
flutter: ">=1.17.0"
2 changes: 1 addition & 1 deletion lib/src/flow.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class StateFlow<T> extends ChangeNotifier {

/// [MutableStateFlow] is inherited from [StateFlow]. It can change/update the value.
class MutableStateFlow<T> extends StateFlow<T> {
MutableStateFlow(super.value);
MutableStateFlow(super.value, {super.notifyOnSameValue});

/// change the value and notify listeners
set value(T value) {
Expand Down
21 changes: 0 additions & 21 deletions lib/src/view_model_stateless_widget.dart

This file was deleted.

1 change: 0 additions & 1 deletion lib/view_model_x.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,3 @@ export './src/state_flow_listener.dart';
export './src/shared_flow_listener.dart';
export './src/view_model_provider.dart';
export './src/multi_flow_listener.dart';
export './src/view_model_stateless_widget.dart';
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: view_model_x
description: An Android similar state management package (StateFlow and SharedFlow with ViewModel) which helps to implement MVVM pattern easily.
version: 0.5.1
version: 0.5.2
homepage: https://github.com/shubham-gupta-16/view_model_x
repository: https://github.com/shubham-gupta-16/view_model_x
issue_tracker: https://github.com/shubham-gupta-16/view_model_x/issues
Expand Down

0 comments on commit e6a0710

Please sign in to comment.