如果要进行条件渲染,则可能会发生以下情况:
Column(
children: <Widget>[
if (isCondition == true)
Text('The condition is true'),
],
);
但是,如果您想使用第三级(如果不是)条件,该怎么办?当子窗口小部件是多层时。
您可以将其用于解决方案flutter_conditional_rendering flutter包,该包可以增强条件渲染,支持if-else和switch条件。
如果-其他条件:
Column(
children: <Widget>[
Conditional.single(
context: context,
conditionBuilder: (BuildContext context) => someCondition == true,
widgetBuilder: (BuildContext context) => Text('The condition is true!'),
fallbackBuilder: (BuildContext context) => Text('The condition is false!'),
),
],
);
切换条件:
Column(
children: <Widget>[
ConditionalSwitch.single<String>(
context: context,
valueBuilder: (BuildContext context) => 'A',
caseBuilders: {
'A': (BuildContext context) => Text('The value is A!'),
'B': (BuildContext context) => Text('The value is B!'),
},
fallbackBuilder: (BuildContext context) => Text('None of the cases matched!'),
),
],
);
如果要有条件地呈现窗口小部件列表,(List<Widget>)
而不是单个窗口小部件。使用Conditional.list()
和ConditionalSwitch.list()!