Open officeprj opened 10 months ago
HomeView
to false
.StatelessWidget
to ViewModelWidget<HomeViewModel>
Create a new stateless widget for the UI you want to rebuild
this step needs some additional work, is there any simple widget wrapper or builder available, like Consumer or Obx?
Describe the bug
When the viewmodel value changes is the entire UI rebuild? If so how we can prevent that? In the example below if counter values changes then the widget which is using the counter value should only rebuild instead of entire UI
To reproduce
import 'package:flutter/material.dart'; import 'package:stacked/stacked.dart'; import 'package:counter_app/ui/common/app_colors.dart'; import 'package:counter_app/ui/common/ui_helpers.dart';
import 'home_viewmodel.dart';
class HomeView extends StackedView {
const HomeView({Key? key}) : super(key: key);
@override Widget builder( BuildContext context, HomeViewModel viewModel, Widget? child, ) {
}
@override HomeViewModel viewModelBuilder( BuildContext context, ) => HomeViewModel(); }
HomeViewModel
import 'package:counter_app/app/app.bottomsheets.dart'; import 'package:counter_app/app/app.dialogs.dart'; import 'package:counter_app/app/app.locator.dart'; import 'package:counter_app/ui/common/app_strings.dart'; import 'package:stacked/stacked.dart'; import 'package:stacked_services/stacked_services.dart';
class HomeViewModel extends BaseViewModel { final _dialogService = locator();
final _bottomSheetService = locator();
String get counterLabel => 'Counter is: $_counter';
int _counter = 0;
void incrementCounter() { _counter++; rebuildUi(); }
void showDialog() { _dialogService.showCustomDialog( variant: DialogType.infoAlert, title: 'Stacked Rocks!', description: 'Give stacked $_counter stars on Github', ); }
void showBottomSheet() { _bottomSheetService.showCustomSheet( variant: BottomSheetType.notice, title: ksHomeBottomSheetTitle, description: ksHomeBottomSheetDescription, ); } }
Expected behavior
No response
Screenshots
No response
Additional Context
No response