-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from shubham-gupta-16/improve_view_model
Fixed ViewModel Dispose major bug
- Loading branch information
Showing
10 changed files
with
142 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
name: Publish to pub.dev | ||
|
||
on: | ||
push: | ||
tags: | ||
- '[0-9]+.[0-9]+.[0-9]+*' | ||
|
||
jobs: | ||
publish: | ||
uses: dart-lang/setup-dart/blob/main/.github/workflows/publish.yml | ||
# with: | ||
# working-directory: path/to/package/within/repository |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:view_model_x/view_model_x.dart'; | ||
|
||
import 'view_model/first_view_model.dart'; | ||
import 'view_model/second_view_model.dart'; | ||
|
||
class ResponsiveExample extends StatelessWidget { | ||
const ResponsiveExample({super.key}); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
final isDesktop = MediaQuery.of(context).size.width > 600; | ||
return Scaffold( | ||
appBar: AppBar( | ||
title: const Text("Responsive"), | ||
), | ||
body: isDesktop | ||
? const FirstSection() | ||
: ViewModelProvider( | ||
create: (context) => SecondViewModel(), | ||
child: const SecondSection()), | ||
); | ||
} | ||
} | ||
|
||
class FirstSection extends StatelessWidget { | ||
const FirstSection({Key? key}) : super(key: key); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return ViewModelProvider( | ||
create: (c) => FirstViewModel(), | ||
builder: (ctx, v) => Container( | ||
color: Colors.green, | ||
child: Center( | ||
child: Text(ViewModelProvider.of<FirstViewModel>(ctx) | ||
.counterStateFlow | ||
.value | ||
.toString()), | ||
), | ||
), | ||
); | ||
} | ||
} | ||
|
||
class SecondSection extends StatelessWidget { | ||
const SecondSection({Key? key}) : super(key: key); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Container( | ||
color: Colors.yellow, | ||
); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
example/lib/responsive_example/view_model/first_view_model.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import 'package:flutter/cupertino.dart'; | ||
import 'package:view_model_x/view_model_x.dart'; | ||
|
||
class FirstViewModel extends ViewModel { | ||
// initialize StateFlow | ||
final _counterStateFlow = MutableStateFlow<int>(1); | ||
|
||
StateFlow<int> get counterStateFlow => _counterStateFlow; | ||
|
||
void increment() { | ||
// by changing the value, listeners were notified | ||
_counterStateFlow.value = _counterStateFlow.value + 1; | ||
} | ||
|
||
@override | ||
void dispose() { | ||
// must dispose all flows | ||
_counterStateFlow.dispose(); | ||
debugPrint("FirstViewModel disposed"); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
example/lib/responsive_example/view_model/second_view_model.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import 'package:flutter/foundation.dart'; | ||
import 'package:view_model_x/view_model_x.dart'; | ||
|
||
class SecondViewModel extends ViewModel { | ||
// initialize SharedFlow | ||
final _messageSharedFlow = MutableSharedFlow<String>(); | ||
|
||
SharedFlow<String> get messageSharedFlow => _messageSharedFlow; | ||
|
||
void showPopupMessage() { | ||
// by emitting the value, listeners were notified | ||
debugPrint("hi"); | ||
_messageSharedFlow.emit("Hello from MyViewModel!"); | ||
} | ||
|
||
@override | ||
void init() { | ||
debugPrint("init inside vm"); | ||
} | ||
|
||
@override | ||
void dispose() { | ||
_messageSharedFlow.dispose(); | ||
debugPrint("SecondViewModel disposed"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters