поэтому я следил за учебник по входу в блок, и хотя мне удалось его завершить, я все еще новичок во Flutter & Dart.
Есть часть кода, где, в зависимости от состояния, код возвращает другой виджет вместо нового Scaffold. Поскольку он не использует маршруты, переход между страницами выглядит прерывистым и неуклюжим.
return BlocProvider<AuthenticationBloc>(
bloc: authenticationBloc,
child: MaterialApp(
debugShowCheckedModeBanner: false,
home: BlocBuilder<AuthenticationEvent, AuthenticationState>(
bloc: authenticationBloc,
builder: (BuildContext context, AuthenticationState state) {
if (state is AuthenticationUninitialized) {
return SplashPage();
}
if (state is AuthenticationAuthenticated) {
return HomePage();
}
if (state is AuthenticationUnauthenticated) {
return LoginPage(userRepository: userRepository);
}
if (state is AuthenticationLoading) {
return LoadingIndicator();
}
},
),
),
);
Я попытался добавить Navigation.push, обертывающий возвраты, например:
if (state is AuthenticationUninitialized) {
Navigation.push(
return SplashPage();
),
}
Но пока это не синтаксически неправильно, это приводит к сбою приложения. Кто-нибудь знает способ реализовать это при сохранении примера BLoC? Спасибо.





Вы можете обернуть страницы с помощью AnimatedSwitcher:
return BlocProvider<AuthenticationBloc>(
bloc: authenticationBloc,
child: MaterialApp(
home: BlocBuilder<AuthenticationEvent, AuthenticationState>(
bloc: authenticationBloc,
builder: (BuildContext context, AuthState state) {
return AnimatedSwitcher(
duration: Duration(milliseconds: 250),
child: _buildPage(context, state),
);
},
),
),
);
По умолчанию он использует переход затухания и анимирует старые и новые виджеты в обратном порядке.
Чтобы оставить старый виджет на месте во время анимации, перейдите к AnimatedSwitcher
switchOutCurve: Threshold(0),
Чтобы имитировать переход Navigator.push в Android, передайте его
transitionBuilder: (Widget child, Animation<double> animation) {
return SlideTransition(
position: Tween<Offset>(
begin: const Offset(0, 0.25),
end: Offset.zero,
).animate(animation),
child: child,
);
},
Чтобы использовать системные переходы, попробуйте что-то вроде
transitionBuilder: (Widget child, Animation<double> animation) {
final theme = Theme.of(context).pageTransitionsTheme;
final prev = MaterialPageRoute(builder: (_) => widget);
return theme.buildTransitions(prev, context, animation, null, child);
},
(последнее не проверено)
Кроме того, дочерний элемент AnimatedSwitcher должен иметь ключ для обозначения изменения. Если вы не хотите изменять свой дочерний виджет (будь то StatelessWidget или StatefulWidget с собственным ключом), оберните его контейнером и установите ключ контейнера на: UniqueKey().