带钩子的 Riverpods Flutter - 使用 Provider
Riverpods with hooks Flutter - using Provider
有人可以给出一个在 Riverpods 中进行额外状态修改的示例,最好使用 riverpod_hooks - 我希望能够在不制作额外小部件的情况下使用它,例如:
onPress: () {
useProvider(genderProvider).state = Gender.female;
},
我在理解 Riverpods 文档中有关在实施后使用该提供程序方面的内容时遇到一些困难。我理解增量部分.state++,但找不到在初始化后更改状态值的明确示例。如 x 初始化为零,但您只想将其设置为 42 或输入值。
例如,我有一个 Gender {male, female, non-binary} 枚举,我制作了一个 GenderProvider,但我无法找到使用按钮。我也不完全确定 Provider 和 RiverPods 有多少相似之处,似乎有很多语义差异?
目前我的代码中是这样声明提供者的:
enum Gender {
male,
female,
nonBinary
}
final genderProvider = StateProvider((_) => Gender.female);
提前致谢,感谢任何建议。
onPress: () => context.read(genderProvider).state = Gender.female,
完整源代码:
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
void main() {
runApp(
ProviderScope(
child: MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter Demo',
home: HomePage(),
),
),
);
}
class HomePage extends HookWidget {
@override
Widget build(BuildContext context) {
final gender = useProvider(genderProvider).state;
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('Gender: ${describeEnum(gender)}'),
const SizedBox(height: 16.0),
ElevatedButton(
child: Text('MALE'),
onPressed: () => context.read(genderProvider).state = Gender.male,
),
const SizedBox(height: 8.0),
ElevatedButton(
child: Text('FEMALE'),
onPressed: () =>
context.read(genderProvider).state = Gender.female,
),
],
),
),
);
}
}
enum Gender { male, female, nonBinary }
final genderProvider = StateProvider((_) => Gender.female);
有人可以给出一个在 Riverpods 中进行额外状态修改的示例,最好使用 riverpod_hooks - 我希望能够在不制作额外小部件的情况下使用它,例如:
onPress: () {
useProvider(genderProvider).state = Gender.female;
},
我在理解 Riverpods 文档中有关在实施后使用该提供程序方面的内容时遇到一些困难。我理解增量部分.state++,但找不到在初始化后更改状态值的明确示例。如 x 初始化为零,但您只想将其设置为 42 或输入值。
例如,我有一个 Gender {male, female, non-binary} 枚举,我制作了一个 GenderProvider,但我无法找到使用按钮。我也不完全确定 Provider 和 RiverPods 有多少相似之处,似乎有很多语义差异?
目前我的代码中是这样声明提供者的:
enum Gender {
male,
female,
nonBinary
}
final genderProvider = StateProvider((_) => Gender.female);
提前致谢,感谢任何建议。
onPress: () => context.read(genderProvider).state = Gender.female,
完整源代码:
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
void main() {
runApp(
ProviderScope(
child: MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter Demo',
home: HomePage(),
),
),
);
}
class HomePage extends HookWidget {
@override
Widget build(BuildContext context) {
final gender = useProvider(genderProvider).state;
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('Gender: ${describeEnum(gender)}'),
const SizedBox(height: 16.0),
ElevatedButton(
child: Text('MALE'),
onPressed: () => context.read(genderProvider).state = Gender.male,
),
const SizedBox(height: 8.0),
ElevatedButton(
child: Text('FEMALE'),
onPressed: () =>
context.read(genderProvider).state = Gender.female,
),
],
),
),
);
}
}
enum Gender { male, female, nonBinary }
final genderProvider = StateProvider((_) => Gender.female);