|
1 |
| -# New Package for Stacked |
2 |
| - |
3 |
| -Created from the `package-template`. |
4 |
| - |
5 |
| -After creating the repository, proceed with the following instructions: |
6 |
| - |
7 |
| -- Update the repository settings to adhere to the conventions: |
8 |
| - - General: |
9 |
| - - No Wikis |
10 |
| - - No Issues |
11 |
| - - No Sponsorships |
12 |
| - - Preserve this repository |
13 |
| - - No Discussions |
14 |
| - - No Projects |
15 |
| - - Don't allow merge commits |
16 |
| - - Allow squash merging with default commit message set to "Default to pull request title and commit details" |
17 |
| - - Don't allow rebase merging |
18 |
| - - Always suggest updating pull requests |
19 |
| - - Allow auto-merge |
20 |
| - - Automatically delete head branches |
21 |
| - - Branch protection rule (`main`): |
22 |
| - - Require a pull request before merging |
23 |
| - - Dismiss stale pull request approvals when new commits are pushed |
24 |
| - - Allow specified actors to bypass required pull requests -> `Dane Mackier` (or whoever is the current owner of the personal access token in the organization secrets `REPO_DEPLOYMENT_TOKEN`) |
25 |
| - - Require status checks to pass before merging |
26 |
| - - Require branches to be up to date before merging |
27 |
| - - Add status check `Linting and Testing` (to select this, the workflow must have been run at least once. This can be done manually since the workflow has "workflow_dispatch" as a trigger) |
28 |
| - - Require conversation resolution before merging |
29 |
| - - Require linear history |
30 |
| -- Create the flutter package with `flutter create -t package --project-name NAME .` |
31 |
| -- Update the content in the `README` file. |
| 1 | +# Stacked Hooks |
| 2 | + |
| 3 | +This package contains widgets that allow you to use the Flutter Hooks package with the `StackedView` in the Stacked architecture. |
| 4 | + |
| 5 | +## StackedHookView |
| 6 | + |
| 7 | +The `StackedView` is an implementation of a widget class that returns a value provided by `Provider` as a parameter in the build function of the widget. This allows for easier consumption and use of ViewModel without boilerplate. The `StackedHookView` allows you to use this widget and make use of Flutter Hooks inside the build function. This is very useful when you want to use `TextEditing` controllers and you're implementing this architecture. |
| 8 | + |
| 9 | +```dart |
| 10 | +// View that creates and provides the viewmodel |
| 11 | +class StackedHookViewExample extends StackedView<HomeViewModel> { |
| 12 | + const StackedHookViewExample({Key key}) : super(key: key); |
| 13 | +
|
| 14 | + @override |
| 15 | + Widget builder(BuildContext context, HomeViewModel model, Widget? child) { |
| 16 | + return Scaffold( |
| 17 | + body: Center(child: _HookForm()), |
| 18 | + ); |
| 19 | + } |
| 20 | +
|
| 21 | + @override |
| 22 | + HomeViewModel modelBuilder(BuildContext context) { |
| 23 | + return HomeViewModel(); |
| 24 | + } |
| 25 | +} |
| 26 | +
|
| 27 | +// Form that makes use of the ViewModel provided above but also makes use of hooks |
| 28 | +class _HookForm extends StackedHookView<HomeViewModel> { |
| 29 | + @override |
| 30 | + Widget buildStackedView(BuildContext context, HomeViewModel model) { |
| 31 | + final title = useTextEditingController(); |
| 32 | + return Column( |
| 33 | + mainAxisSize: MainAxisSize.min, |
| 34 | + children: <Widget>[ |
| 35 | + Text(model.title), |
| 36 | + TextField(controller: title, onChanged: model.updateTitle), |
| 37 | + ], |
| 38 | + ); |
| 39 | + } |
| 40 | +} |
| 41 | +
|
| 42 | +// ViewModel |
| 43 | +class HomeViewModel extends BaseViewModel { |
| 44 | + String title = 'default'; |
| 45 | +
|
| 46 | + void updateTitle(String value) { |
| 47 | + title = value; |
| 48 | + notifyListeners(); |
| 49 | + } |
| 50 | +} |
| 51 | +``` |
0 commit comments