让 Flutter Flow 更轻松!
用法
定义 Flow 状态
Flow 状态将是驱动 Flow 的状态。每次此状态更改时,都将根据新的 Flow 状态生成新的导航堆栈。
class Profile {
const Profile({this.name, this.age, this.weight});
final String? name;
final int? age;
final int? weight;
Profile copyWith({String? name, int? age, int? weight}) {
return Profile(
name: name ?? this.name,
age: age ?? this.age,
weight: weight ?? this.weight,
);
}
}
创建 FlowBuilder
FlowBuilder 是一个根据 Flow 状态的变化来构建导航堆栈的 Widget。onGeneratePages 将为每次状态更改调用,并必须将新的导航堆栈作为 Page 列表返回。
FlowBuilder<Profile>(
state: const Profile(),
onGeneratePages: (profile, pages) {
return [
MaterialPage(child: NameForm()),
if (profile.name != null) MaterialPage(child: AgeForm()),
];
},
);
更新 Flow 状态
可以通过 context.flow<T>().update 来更新 Flow 的状态。
class NameForm extends StatefulWidget {
@override
_NameFormState createState() => _NameFormState();
}
class _NameFormState extends State<NameForm> {
var _name = '';
void _continuePressed() {
context.flow<Profile>().update((profile) => profile.copyWith(name: _name));
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Name')),
body: Center(
child: Column(
children: <Widget>[
TextField(
onChanged: (value) => setState(() => _name = value),
decoration: InputDecoration(
labelText: 'Name',
hintText: 'John Doe',
),
),
RaisedButton(
child: const Text('Continue'),
onPressed: _name.isNotEmpty ? _continuePressed : null,
)
],
),
),
);
}
}
完成 Flow
可以通过 context.flow<T>().complete 来完成 Flow。
class AgeForm extends StatefulWidget {
@override
_AgeFormState createState() => _AgeFormState();
}
class _AgeFormState extends State<AgeForm> {
int? _age;
void _continuePressed() {
context
.flow<Profile>()
.complete((profile) => profile.copyWith(age: _age));
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Age')),
body: Center(
child: Column(
children: <Widget>[
TextField(
onChanged: (value) => setState(() => _age = int.parse(value)),
decoration: InputDecoration(
labelText: 'Age',
hintText: '42',
),
keyboardType: TextInputType.number,
),
RaisedButton(
child: const Text('Continue'),
onPressed: _age != null ? _continuePressed : null,
)
],
),
),
);
}
}
FlowController
FlowBuilder 也可以使用自定义的 FlowController 创建,以应对 Flow 可以被子树之外的元素操纵的情况。
class MyFlow extends StatefulWidget {
@override
State<MyFlow> createState() => _MyFlowState();
}
class _MyFlowState extends State<MyFlow> {
late FlowController<Profile> _controller;
@override
void initState() {
super.initState();
_controller = FlowController(const Profile());
}
@override
Widget build(BuildContext context) {
return FlowBuilder(
controller: _controller,
onGeneratePages: ...,
);
}
@override dispose() {
_controller.dispose();
super.dispose();
}
}
