File tree Expand file tree Collapse file tree 10 files changed +142
-4
lines changed
multiple_view_models_example/view_model Expand file tree Collapse file tree 10 files changed +142
-4
lines changed Original file line number Diff line number Diff line change
1
+ name : Publish to pub.dev
2
+
3
+ on :
4
+ push :
5
+ tags :
6
+ - ' [0-9]+.[0-9]+.[0-9]+*'
7
+
8
+ jobs :
9
+ publish :
10
+ uses : dart-lang/setup-dart/blob/main/.github/workflows/publish.yml
11
+ # with:
12
+ # working-directory: path/to/package/within/repository
Original file line number Diff line number Diff line change
1
+ ## 0.5.3
2
+ Fixed ViewModel Dispose major bug
3
+ - ViewModel not disposing fixed
4
+ - Responsive Example added
5
+
1
6
## 0.5.2
2
7
Fixed Bugs and make package simpler
3
8
- ViewModelStatelessWidget removed
Original file line number Diff line number Diff line change 1
1
import 'package:example/multiple_view_models_example/multiple_view_models_example.dart' ;
2
2
import 'package:example/post_frame_callback_example/post_frame_callback_example.dart' ;
3
+ import 'package:example/responsive_example/responsive_example.dart' ;
3
4
import 'package:flutter/material.dart' ;
4
5
5
6
final _moreExamples = {
6
7
"Multiple ViewModels Example" : const MultipleViewModelsExample (),
7
8
"PostFrameCallback Example" : const PostFrameCallbackExample (),
9
+ "Responsive Example" : const ResponsiveExample (),
8
10
};
9
11
10
12
class MoreExamplesSection extends StatelessWidget {
Original file line number Diff line number Diff line change @@ -21,6 +21,6 @@ class SecondViewModel extends ViewModel {
21
21
@override
22
22
void dispose () {
23
23
_messageSharedFlow.dispose ();
24
- debugPrint ("FirstViewModel disposed" );
24
+ debugPrint ("SecondViewModel disposed" );
25
25
}
26
26
}
Original file line number Diff line number Diff line change
1
+ import 'package:flutter/material.dart' ;
2
+ import 'package:view_model_x/view_model_x.dart' ;
3
+
4
+ import 'view_model/first_view_model.dart' ;
5
+ import 'view_model/second_view_model.dart' ;
6
+
7
+ class ResponsiveExample extends StatelessWidget {
8
+ const ResponsiveExample ({super .key});
9
+
10
+ @override
11
+ Widget build (BuildContext context) {
12
+ final isDesktop = MediaQuery .of (context).size.width > 600 ;
13
+ return Scaffold (
14
+ appBar: AppBar (
15
+ title: const Text ("Responsive" ),
16
+ ),
17
+ body: isDesktop
18
+ ? const FirstSection ()
19
+ : ViewModelProvider (
20
+ create: (context) => SecondViewModel (),
21
+ child: const SecondSection ()),
22
+ );
23
+ }
24
+ }
25
+
26
+ class FirstSection extends StatelessWidget {
27
+ const FirstSection ({Key ? key}) : super (key: key);
28
+
29
+ @override
30
+ Widget build (BuildContext context) {
31
+ return ViewModelProvider (
32
+ create: (c) => FirstViewModel (),
33
+ builder: (ctx, v) => Container (
34
+ color: Colors .green,
35
+ child: Center (
36
+ child: Text (ViewModelProvider .of <FirstViewModel >(ctx)
37
+ .counterStateFlow
38
+ .value
39
+ .toString ()),
40
+ ),
41
+ ),
42
+ );
43
+ }
44
+ }
45
+
46
+ class SecondSection extends StatelessWidget {
47
+ const SecondSection ({Key ? key}) : super (key: key);
48
+
49
+ @override
50
+ Widget build (BuildContext context) {
51
+ return Container (
52
+ color: Colors .yellow,
53
+ );
54
+ }
55
+ }
Original file line number Diff line number Diff line change
1
+ import 'package:flutter/cupertino.dart' ;
2
+ import 'package:view_model_x/view_model_x.dart' ;
3
+
4
+ class FirstViewModel extends ViewModel {
5
+ // initialize StateFlow
6
+ final _counterStateFlow = MutableStateFlow <int >(1 );
7
+
8
+ StateFlow <int > get counterStateFlow => _counterStateFlow;
9
+
10
+ void increment () {
11
+ // by changing the value, listeners were notified
12
+ _counterStateFlow.value = _counterStateFlow.value + 1 ;
13
+ }
14
+
15
+ @override
16
+ void dispose () {
17
+ // must dispose all flows
18
+ _counterStateFlow.dispose ();
19
+ debugPrint ("FirstViewModel disposed" );
20
+ }
21
+ }
Original file line number Diff line number Diff line change
1
+ import 'package:flutter/foundation.dart' ;
2
+ import 'package:view_model_x/view_model_x.dart' ;
3
+
4
+ class SecondViewModel extends ViewModel {
5
+ // initialize SharedFlow
6
+ final _messageSharedFlow = MutableSharedFlow <String >();
7
+
8
+ SharedFlow <String > get messageSharedFlow => _messageSharedFlow;
9
+
10
+ void showPopupMessage () {
11
+ // by emitting the value, listeners were notified
12
+ debugPrint ("hi" );
13
+ _messageSharedFlow.emit ("Hello from MyViewModel!" );
14
+ }
15
+
16
+ @override
17
+ void init () {
18
+ debugPrint ("init inside vm" );
19
+ }
20
+
21
+ @override
22
+ void dispose () {
23
+ _messageSharedFlow.dispose ();
24
+ debugPrint ("SecondViewModel disposed" );
25
+ }
26
+ }
Original file line number Diff line number Diff line change @@ -176,7 +176,7 @@ packages:
176
176
path: ".."
177
177
relative: true
178
178
source: path
179
- version: "0.5.2 "
179
+ version: "0.5.3 "
180
180
sdks:
181
181
dart: ">=2.18.6 <3.0.0"
182
182
flutter: ">=1.17.0"
Original file line number Diff line number Diff line change 1
1
import 'package:flutter/widgets.dart' ;
2
2
import 'package:provider/provider.dart' ;
3
+ import 'package:provider/single_child_widget.dart' ;
3
4
4
5
import 'provider_single_child_widget.dart' ;
5
6
import 'view_model.dart' ;
6
7
7
8
/// [ViewModelProvider] is used to wrap the widget with your custom [ViewModel] .
8
9
/// This requires [create] which accepts custom [ViewModel] and [child] Widget.
10
+ class VMP <T extends ViewModel > extends SingleChildStatelessWidget {
11
+ const VMP ({super .key, super .child});
12
+
13
+ @override
14
+ Widget buildWithChild (BuildContext context, Widget ? child) {
15
+ // TODO: implement buildWithChild
16
+ throw UnimplementedError ();
17
+ }
18
+ }
19
+
9
20
class ViewModelProvider <T extends ViewModel > extends Provider <T >
10
21
with ProviderSingleChildWidget {
11
22
ViewModelProvider (
12
23
{super .key,
13
24
required super .create,
14
25
super .lazy,
15
26
super .builder,
16
- super .child});
27
+ super .child})
28
+ : super (dispose: _dispose);
29
+
30
+ static void _dispose <T extends ViewModel >(BuildContext context, T viewModel) {
31
+ debugPrint ('provider dispose' );
32
+ viewModel.dispose ();
33
+ }
17
34
18
35
/// [ViewModelProvider] .[of] method allows to get the custom [ViewModel] from anywhere nested inside [ViewModelProvider] 's [child]
19
36
static F of <F extends ViewModel >(BuildContext context) {
Original file line number Diff line number Diff line change 1
1
name : view_model_x
2
2
description : An Android similar state management package (StateFlow and SharedFlow with ViewModel) which helps to implement MVVM pattern easily.
3
- version : 0.5.2
3
+ version : 0.5.3
4
4
homepage : https://github.com/shubham-gupta-16/view_model_x
5
5
repository : https://github.com/shubham-gupta-16/view_model_x
6
6
issue_tracker : https://github.com/shubham-gupta-16/view_model_x/issues
You can’t perform that action at this time.
0 commit comments