flutter - Flutter 中基于当前状态显示 Widget
问题描述
我的基本意图是根据我当前的状态显示一个小部件。假设,当用户单击一个按钮时,我使用该setState()
方法将 a 的值设置currentPage
为2
。
onTabChangedListener: (position, title, color) {
setState(() {
currentPage = position;
...
当它是时2
,我想显示以下小部件而不是当前显示的小部件。我已经尝试过if/else
案例,但似乎不是这样。我也讨论过这个和这个,但我不清楚实施。我了解有状态小部件的工作原理 - 只需要一个与我的问题类似的代码示例。
// Widget I want to display when currentPage state is changed to 2.
Widget _buildContent() {
return Positioned(
bottom: 50,
left: 25,
right: 25,
child: Container(
color: Theme.of(context).backgroundColor,
child: Column(
children: <Widget>[
// _buildTextField("Some Text"),
_buildLoginButton(),
SizedBox(height: 20),
RichText(
text: TextSpan(
text: "Some Text",
style: TextStyle(
fontSize: 10.5,
fontWeight: FontWeight.w300,
letterSpacing: 1,
),
children: [
TextSpan(
text: "Another Text",
style: TextStyle(
decoration: TextDecoration.underline,
),
),
],
),
)
],
),
),
);
}
注意:我的当前代码的完整页面可在此处获得
解决方案
这是我在我的代码中使用的东西。将您要显示的所有页面放在一个Stack
带有Offstage
Widget 中。Offstage
如果 offstage 条件为真,小部件将隐藏(使其“offstage”)。
Stack(
children: <Widget>[
Offstage(
offstage: 0 != _currentBottomBarIndex,
child: PageOne(),
),
Offstage(
offstage: 1 != _currentBottomBarIndex,
child: PageTwo(),
),
Offstage(
offstage: 2 != _currentBottomBarIndex,
child: PageThree(),
),
Offstage(
offstage: 3 != _currentBottomBarIndex,
child: PageFour(),
),
],
),
推荐阅读
- python - 精确分数警告导致分数 = 0 sklearn
- python - 如何在odoo12中对many2many模型进行搜索?
- javascript - 如何在生产中混淆 Tailwindcss HTML 类名?
- javascript - 如何让机器人在 Discord 上编辑自己的消息
- flutter - Flutter Session 存储数据但不能基于它做出决策
- python-3.x - 如何可视化每个用户 ID 随着时间的推移登录到系统的计数列表,并且用户 ID 组登录 1、2、3、...n 次?
- python - 梯度计算所需的变量之一已被就地操作修改 [torch.cuda.FloatTensor [1, 64, 3, 3, 3]]
- c# - Kafka .Net Produce 与 ProduceAsync
- android - 构建Android时如何修复警告缺少类:org.conscrypt.ConscryptHostnameVerifier
- google-apps-script - 使用 e.parameters 解析数据