Created
January 12, 2022 20:27
-
-
Save MCarlomagno/55a3001e40c8a97e3c70d4ae43b8e052 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import 'package:flutter/material.dart'; | |
import 'package:stacked/stacked.dart'; | |
class MyView extends StatelessWidget { | |
const MyView({Key? key}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
// we have to wrap our widget inside a ViewModelBuilder | |
// to react to our viewmodel this implementation utilises | |
// Provider library behind the scenes | |
return ViewModelBuilder<MyViewModel>.reactive( | |
onModelReady: (model) => model.onModelReady(), | |
builder: (context, model, child) => Scaffold( | |
// add your content here, | |
// thanks to the model valriable | |
// received as parameter, you can | |
// interact with the viewmodel calling | |
// functions and listenting its variables | |
body: Text(model.someText), | |
), | |
viewModelBuilder: () => MyViewModel(), | |
); | |
} | |
} | |
import 'package:stacked/stacked.dart'; | |
class MyViewModel extends BaseViewModel { | |
String _someText = 'Hello World'; | |
String getSomeText => _someText; | |
onModelReady() { | |
// Do something here as initState | |
} | |
someRandomFunctionToUpdateUI() { | |
// you can also update the UI just like calling | |
// setState(...) by calling notifyListeners() | |
// as we do with Provider library | |
_someText = 'Updates the UI!'; | |
notifyListeners(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment