datetime - Flutter DateTime , 打开关闭 if
问题描述
我想在上午 09:00 - 下午 20:00 之间显示小部件,其他时间显示另一个小部件,例如现在 13:00 我的商店开张我想显示我的商店开张。
谢谢
openSaat() {
return Container(
width: 170,
height: 40,
decoration: BoxDecoration(color: Colors.black),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
CircleAvatar(
backgroundColor: Colors.green,
radius: 12,
),
SizedBox(width: 5,),
Text("Open Now",style: TextStyle(fontSize: 20,fontWeight: FontWeight.bold,color: Colors.white),)
],
),
);
}
closeSaat() {
return Container(
width: 170,
height: 40,
decoration: BoxDecoration(color: Colors.black),
child: Center(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
CircleAvatar(
backgroundColor: Colors.red,
radius: 12,
),
SizedBox(width: 5,),
Text("Close Now",style: TextStyle(fontSize: 20,fontWeight: FontWeight.bold,color: Colors.white),)
],
),
),
);
}
解决方案
@override
Widget build(BuildContext context) {
return new Scaffold(
body: currentWidget()
);
}
static DateTime now = DateTime.now();
Widget currentWidget() {
var hours = now.hour;
if (hours >= 09 && hours < 21) {
return _openSaat();
} else
return _closeSaat();
}
Widget _openSaat() {
return Container(
width: 170,
height: 40,
decoration: BoxDecoration(color: Colors.black),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
CircleAvatar(
backgroundColor: Colors.green,
radius: 12,
),
SizedBox(
width: 5,
),
Text(
"Open Now",
style: TextStyle(
fontSize: 20, fontWeight: FontWeight.bold, color: Colors.white),
)
],
),
);
}
Widget _closeSaat() {
return Container(
width: 170,
height: 40,
decoration: BoxDecoration(color: Colors.black),
child: Center(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
CircleAvatar(
backgroundColor: Colors.red,
radius: 12,
),
SizedBox(
width: 5,
),
Text(
"Close Now",
style: TextStyle(
fontSize: 20, fontWeight: FontWeight.bold, color: Colors.white),
)
],
),
),
);
}
推荐阅读
- javascript - pdf.js 是否执行包含在 pdf 文件中的 javascript?
- c++ - 将 SDL2_net 与 CMake 链接
- flutter - Flutter:使用自定义 ErrorWidget
- microsoft-graph-api - 在 microsoft graph 中递归列出所有文件
- java - java.lang.ClassNotFoundException:EnvironmentLoaderListener shiro apache
- c++ - 为 .json 数据分配数字以编辑 C++
- html - 选定文本区域的自定义滚动条设计
- sql - 对数据进行排序和标记
- video - H.264 I 帧替换
- pgp - 无法将 pgp 密钥发送/上传到密钥服务器