我的目标是创建一个应用程序,用户可以在其中选择他喜欢的主题。我使用共享首选项保存用户的选择,以便我可以在下次应用程序启动时加载它。用户可以选择: - 深色模式(独立于操作系统设置) - 浅色模式(独立于操作系统设置) - 系统(根据操作系统设置在深色模式和浅色模式之间进行更改) 在 BLoC 的帮助下,我几乎达到了我想要的。但问题是我需要在 Bloc 事件中传递亮度。为了获得系统(操作系统)亮度,我需要利用
MediaQuery.of(context).platformBrightness
Run Code Online (Sandbox Code Playgroud)
但 Bloc 在 MaterialApp 之前启动,因此 MediaQuery 不可用。当然,我可以稍后传递亮度(从 MaterialApp 的子小部件),但随后(例如,如果用户激活了黑暗模式)它会从亮到暗,但对用户来说可见的时间很短(因为在 InitialState 内)我在灯光模式下通过了)。
class MyApp extends StatelessWidget {
final RecipeRepository recipeRepository;
MyApp({Key key, @required this.recipeRepository})
: assert(recipeRepository != null),
super(key: key);
@override
Widget build(BuildContext context) {
return MultiBlocProvider(
providers: [
BlocProvider<ThemeBloc>(create: (context) =>
ThemeBloc(),),
],
child: BlocBuilder<ThemeBloc, ThemeState>(
builder: (context, state){
return MaterialApp(
theme: state.themeData,
title: 'Flutter Weather',
localizationsDelegates: [
FlutterI18nDelegate(fallbackFile: 'en',),
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate
],
supportedLocales: [
const Locale("en"), …Run Code Online (Sandbox Code Playgroud) 在我的存储库方法中使用 List 类型的参数时,我遇到不稳定的错误。该问题似乎与 Kotlin 列表和 Java 之间的互操作性有关。当我运行单元测试时,它随机失败,我怀疑这与 Kotlin 中 listOf() 返回的 MutableList 有关。更多信息可以在 StackOverflow 帖子中找到:listOf() returns MutableList。
这是我的单元测试和存储库方法的示例:
@Test
fun `should return recipes with the specified author, locale and categories`() {
println("AuthorID: " + author.id)
recipeRepo.findRecipesBy(
locale = LanguageSelection.ENGLISH,
authorIds = arrayListOf(44),
)
// it throws before reaching asserts
}
Run Code Online (Sandbox Code Playgroud)
存储库:
interface RecipeRepository : JpaRepository<Recipe, Long> {
@Query("SELECT r FROM Recipe r WHERE "
+ "(:authorIds is null or r.author.id in (:authorIds)) "
+ "and (:recipeIds is null …Run Code Online (Sandbox Code Playgroud) bloc ×1
dart ×1
flutter ×1
flutter-bloc ×1
hibernate ×1
ios-darkmode ×1
java ×1
kotlin ×1
spring-boot ×1
swiftui ×1