mirror of
https://github.com/flutter/samples.git
synced 2026-06-25 15:49:43 +00:00
## Description **Problem:** `LogoutViewModel` was built inside `HomeHeader.build()`, so each rebuild could create a new instance and a new `logout` command. That does not match how other Compass view models are scoped (created in the GoRouter route `builder` and passed in), and it can affect an in-flight logout when the home screen rebuilds. **Change:** Create `LogoutViewModel` once in the `/home` route next to `HomeViewModel`, pass it through `HomeScreen` → `HomeHeader` → `LogoutButton`, and remove inline construction in `home_title.dart`. Update `home_screen_test.dart` to build `LogoutViewModel` with fakes and remove `Provider` wrappers that only supported `context.read()` in the header. **Result:** Logout view model lifetime aligns with the home route; no visual or copy changes (screenshots not needed). Fixes https://github.com/flutter/samples/issues/2604 --- ## Pre-launch Checklist - [x] I read the [Flutter Style Guide] _recently_, and have followed its advice. - [x] I signed the [CLA]. - [x] I read the [Contributors Guide]. - [x] I have added sample code updates to the [changelog]. - [x] I updated/added relevant documentation (doc comments with `///`). If you need help, consider asking for advice on the #hackers-devrel channel on [Discord]. <!-- Links --> [Flutter Style Guide]: https://github.com/flutter/flutter/blob/master/docs/contributing/Style-guide-for-Flutter-repo.md [CLA]: https://cla.developers.google.com/ [Discord]: https://github.com/flutter/flutter/blob/master/docs/contributing/Chat.md [Contributors Guide]: https://github.com/flutter/samples/blob/main/CONTRIBUTING.md [changelog]: ../CHANGELOG.md --------- Co-authored-by: Eric Windmill <eric@ericwindmill.com>
This commit is contained in:
@@ -7,6 +7,7 @@ import 'package:go_router/go_router.dart';
|
||||
|
||||
import '../../../domain/models/booking/booking_summary.dart';
|
||||
import '../../../routing/routes.dart';
|
||||
import '../../auth/logout/view_models/logout_viewmodel.dart';
|
||||
import '../../core/localization/applocalization.dart';
|
||||
import '../../core/themes/colors.dart';
|
||||
import '../../core/themes/dimens.dart';
|
||||
@@ -18,9 +19,14 @@ import 'home_title.dart';
|
||||
const String bookingButtonKey = 'booking-button';
|
||||
|
||||
class HomeScreen extends StatefulWidget {
|
||||
const HomeScreen({super.key, required this.viewModel});
|
||||
const HomeScreen({
|
||||
super.key,
|
||||
required this.viewModel,
|
||||
required this.logoutViewModel,
|
||||
});
|
||||
|
||||
final HomeViewModel viewModel;
|
||||
final LogoutViewModel logoutViewModel;
|
||||
|
||||
@override
|
||||
State<HomeScreen> createState() => _HomeScreenState();
|
||||
@@ -88,7 +94,10 @@ class _HomeScreenState extends State<HomeScreen> {
|
||||
vertical: Dimens.of(context).paddingScreenVertical,
|
||||
horizontal: Dimens.of(context).paddingScreenHorizontal,
|
||||
),
|
||||
child: HomeHeader(viewModel: widget.viewModel),
|
||||
child: HomeHeader(
|
||||
viewModel: widget.viewModel,
|
||||
logoutViewModel: widget.logoutViewModel,
|
||||
),
|
||||
),
|
||||
),
|
||||
SliverList.builder(
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:google_fonts/google_fonts.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import '../../auth/logout/view_models/logout_viewmodel.dart';
|
||||
import '../../auth/logout/widgets/logout_button.dart';
|
||||
@@ -13,9 +12,14 @@ import '../../core/themes/dimens.dart';
|
||||
import '../view_models/home_viewmodel.dart';
|
||||
|
||||
class HomeHeader extends StatelessWidget {
|
||||
const HomeHeader({super.key, required this.viewModel});
|
||||
const HomeHeader({
|
||||
super.key,
|
||||
required this.viewModel,
|
||||
required this.logoutViewModel,
|
||||
});
|
||||
|
||||
final HomeViewModel viewModel;
|
||||
final LogoutViewModel logoutViewModel;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
@@ -37,12 +41,7 @@ class HomeHeader extends StatelessWidget {
|
||||
height: Dimens.of(context).profilePictureSize,
|
||||
),
|
||||
),
|
||||
LogoutButton(
|
||||
viewModel: LogoutViewModel(
|
||||
authRepository: context.read(),
|
||||
itineraryConfigRepository: context.read(),
|
||||
),
|
||||
),
|
||||
LogoutButton(viewModel: logoutViewModel),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: Dimens.paddingVertical),
|
||||
|
||||
Reference in New Issue
Block a user