-
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.
Easy to implement ViewModel and example improved
- Loading branch information
Showing
13 changed files
with
221 additions
and
26 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
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
43 changes: 43 additions & 0 deletions
43
example/lib/post_frame_callback_example/post_frame_callback_example.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,43 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:view_model_x/view_model_x.dart'; | ||
|
||
import 'view_model/my_view_model_with_post_frame_callback.dart'; | ||
|
||
class PostFrameCallbackExample extends StatelessWidget { | ||
const PostFrameCallbackExample({super.key}); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return ViewModelProvider( | ||
create: (context) => MyViewModelWithPostFrameCallback(), | ||
// child: MaterialApp(home: const ContentPage()), | ||
child: const ContentPage(), | ||
); | ||
} | ||
} | ||
|
||
class ContentPage extends StatelessWidget { | ||
const ContentPage({super.key}); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Scaffold( | ||
appBar: AppBar( | ||
leading: const CloseButton(), | ||
titleSpacing: 0, | ||
title: const Text('ViewModel with PostFrameCallback Example')), | ||
body: SharedFlowListener( | ||
sharedFlow: | ||
context.vm<MyViewModelWithPostFrameCallback>().messageSharedFlow, | ||
listener: (context, value) { | ||
// show SnackBar | ||
ScaffoldMessenger.of(context) | ||
.showSnackBar(SnackBar(content: Text(value))); | ||
}, | ||
child: const Center( | ||
child: Text('SnackBar will appear on PostFrameCallback'), | ||
), | ||
), | ||
); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...le/lib/post_frame_callback_example/view_model/my_view_model_with_post_frame_callback.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,31 @@ | ||
import 'package:flutter/foundation.dart'; | ||
import 'package:view_model_x/view_model_x.dart'; | ||
|
||
class MyViewModelWithPostFrameCallback extends ViewModel | ||
with PostFrameCallback { | ||
// initialize SharedFlow | ||
final _messageSharedFlow = MutableSharedFlow<String>(); | ||
|
||
SharedFlow<String> get messageSharedFlow => _messageSharedFlow; | ||
|
||
@override | ||
void init() { | ||
// do stuff on create of view model, (equivalent to constructor) | ||
debugPrint("on init"); | ||
|
||
_messageSharedFlow.emit( | ||
"on init"); // this emit will not received by listener because it emitted before ui build. | ||
} | ||
|
||
@override | ||
void onPostFrameCallback(Duration timestamp) { | ||
_messageSharedFlow.emit( | ||
"onPostFrameCallback"); // this emit will be received because it is emitted after the ui build completed. | ||
} | ||
|
||
@override | ||
void dispose() { | ||
_messageSharedFlow.dispose(); | ||
debugPrint("MyViewModelWithPostFrameCallback disposed"); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
example/lib/view_model_stateless_widget_example/view_model/counter_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,19 @@ | ||
import 'package:view_model_x/view_model_x.dart'; | ||
|
||
class CounterViewModel 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(); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
example/lib/view_model_stateless_widget_example/view_model_stateless_example.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,43 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:view_model_x/view_model_x.dart'; | ||
|
||
import 'view_model/counter_view_model.dart'; | ||
|
||
class ViewModelStatelessWidgetExample | ||
extends ViewModelStatelessWidget<CounterViewModel> { | ||
const ViewModelStatelessWidgetExample({Key? key}) : super(key: key); | ||
|
||
@override | ||
CounterViewModel createViewModel(BuildContext context) => CounterViewModel(); | ||
|
||
@override | ||
Widget buildWithViewModel(BuildContext context, CounterViewModel viewModel) { | ||
return Scaffold( | ||
appBar: AppBar( | ||
leading: const CloseButton(), | ||
titleSpacing: 0, | ||
title: const Text('ViewModel Stateless Widget Example')), | ||
// implement SharedFlowListener anywhere in code to listen for emits from sharedFlow | ||
body: Center( | ||
// implement ViewModelBuilder to rebuild Text on StateFlow value changed/updated | ||
child: StateFlowBuilder( | ||
// pass your StateFlow | ||
stateFlow: viewModel.counterStateFlow, | ||
builder: (context, value) { | ||
return Text( | ||
"$value", | ||
style: const TextStyle(fontSize: 30), | ||
); | ||
}), | ||
), | ||
floatingActionButton: FloatingActionButton( | ||
heroTag: "view_model_stateless_widget_example", | ||
onPressed: () { | ||
// call the increment function which is inside CounterViewModel | ||
viewModel.increment(); | ||
}, | ||
child: const Icon(Icons.add), | ||
), | ||
); | ||
} | ||
} |
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,21 @@ | ||
import 'package:flutter/widgets.dart'; | ||
import 'view_model_provider.dart'; | ||
import 'view_model.dart'; | ||
|
||
/// [ViewModelStatelessWidget] helps to integrate [ViewModel] into [StatelessWidget] easily. | ||
abstract class ViewModelStatelessWidget<T extends ViewModel> | ||
extends StatelessWidget { | ||
const ViewModelStatelessWidget({Key? key}) : super(key: key); | ||
|
||
T createViewModel(BuildContext context); | ||
|
||
Widget buildWithViewModel(BuildContext context, T viewModel); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return ViewModelProvider( | ||
create: createViewModel, | ||
builder: (context, w) => buildWithViewModel(context, context.vm<T>()), | ||
); | ||
} | ||
} |
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