Dart 如何在小部件类中使用switch语句
我试图显示一个带有一些设计的容器,如果它从firestore获得一些数据,但如果firestore没有数据,我希望它显示一个文本,说Dart 如何在小部件类中使用switch语句,dart,flutter-layout,Dart,Flutter Layout,我试图显示一个带有一些设计的容器,如果它从firestore获得一些数据,但如果firestore没有数据,我希望它显示一个文本,说没有来自firestore的数据,但它不工作,我收到错误。 徖 下面是代码,是否正确getStoriesFunction: Widget _getStories() { Live users; return SingleChildScrollView( scrollDirection: Axis.horizontal, ch
没有来自firestore的数据
,但它不工作,我收到错误。
徖
下面是代码,是否正确<代码>getStoriesFunction:
Widget _getStories() {
Live users;
return SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: getStory(users),
);
}
Widget getStory(Live user) {
return Container(
margin: EdgeInsets.all(12),
child: Column(
children: <Widget>[
user.isLive == true
? Container(
height: 55,
width: 55,
child: GestureDetector(
onTap: () {
// Join function
onJoin(
channelName: user.displayName,
channelId: user.channelId,
displayName: displayName,
hostImage: user.image,
userImage: image);
},
child: Stack(
alignment: Alignment(0, 0),
children: <Widget>[
Container(
height: 60,
width: 60,
child: Container(
decoration: BoxDecoration(
shape: BoxShape.circle,
gradient: LinearGradient(
colors: [
Colors.red,
Colors.redAccent,
Colors.red
],
begin: Alignment.topLeft,
end: Alignment.bottomRight)),
),
),
Container(
height: 55.5,
width: 55.5,
child: CircleAvatar(
backgroundColor: Colors.black,
),
),
CachedNetworkImage(
imageUrl: user.image,
imageBuilder: (context, imageProvider) => Container(
width: 52.0,
height: 52.0,
decoration: BoxDecoration(
shape: BoxShape.circle,
image: DecorationImage(
image: imageProvider, fit: BoxFit.cover),
),
),
),
Container(
height: 70,
width: 70,
alignment: Alignment.bottomCenter,
child: Stack(
alignment: Alignment.center,
children: <Widget>[
Container(
height: 17,
width: 25,
decoration: new BoxDecoration(
shape: BoxShape.rectangle,
borderRadius: BorderRadius.all(
Radius.circular(
4.0) // <--- border radius here
),
gradient: LinearGradient(
colors: [Colors.black, Colors.black],
begin: Alignment.centerLeft,
end: Alignment.centerRight),
),
),
Container(
decoration: new BoxDecoration(
shape: BoxShape.rectangle,
borderRadius: BorderRadius.all(
Radius.circular(
2.0) // <--- border radius here
),
gradient: LinearGradient(
colors: [
Colors.red,
Colors.redAccent
],
begin: Alignment.centerLeft,
end: Alignment.centerRight),
),
child: Padding(
padding: const EdgeInsets.all(3.0),
child: Text(
'LIVE',
style: TextStyle(
fontSize: 7,
color: Colors.white,
fontWeight: FontWeight.bold),
),
)),
],
))
],
),
))
: Text('No data in firestore',
style: TextStyle(
color: Colors.black,
fontSize: 12,
))
],
));
}
Widget\u getStories(){
实时用户;
返回SingleChildScrollView(
滚动方向:轴水平,
子:getStory(用户),
);
}
小部件getStory(实时用户){
返回容器(
保证金:所有(12),
子:列(
儿童:[
user.isLive==true
?容器(
身高:55,
宽度:55,
儿童:手势检测器(
onTap:(){
//连接函数
加入(
channelName:user.displayName,
channelId:user.channelId,
displayName:displayName,
hostImage:user.image,
userImage:image);
},
子:堆栈(
对齐:对齐(0,0),
儿童:[
容器(
身高:60,
宽度:60,
子:容器(
装饰:盒子装饰(
形状:BoxShape.circle,
梯度:线性梯度(
颜色:[
颜色,红色,
颜色,红色口音,
颜色。红色
],
开始:Alignment.topLeft,
结束:对齐。右下角),
),
),
容器(
身高:55.5,
宽度:55.5,
孩子:圆环星(
背景颜色:Colors.black,
),
),
CachedNetworkImage(
imageUrl:user.image,
imageBuilder:(上下文,imageProvider)=>容器(
宽度:52.0,
身高:52.0,
装饰:盒子装饰(
形状:BoxShape.circle,
图像:装饰图像(
图像:imageProvider,尺寸:BoxFit.cover),
),
),
),
容器(
身高:70,
宽度:70,
对齐:对齐.bottomCenter,
子:堆栈(
对齐:对齐.center,
儿童:[
容器(
身高:17,
宽度:25,
装饰:新盒子装饰(
形状:BoxShape.rectangle,
borderRadius:borderRadius.all(
圆半径(
4.0)//我已经更正了您的代码并在中进行了测试,对我来说效果很好。您可以通过复制和pase进行测试。firebase中的用户数据出现了一些问题,看起来像是bool isLive
始终正确
导入“包装:颤振/材料.省道”;
void main()=>runApp(MyApp());
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“颤振演示”,
debugShowCheckedModeBanner:false,
主题:主题数据(
主样本:颜色。蓝色,
),
主页:MyHomePage(标题:“颤振演示主页”),
);
}
}
类MyHomePage扩展StatefulWidget{
MyHomePage({Key,this.title}):超级(Key:Key);
最后的字符串标题;
@凌驾
_MyHomePageState createState()=>\u MyHomePageState();
}
类_MyHomePageState扩展状态{
//伪造用户数据
bool userExist=false;
void _toggleUserState(){
setState(()=>userExist=!userExist);
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
标题:文本(widget.title),
),
正文:中(
子:列(
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
_getStories(userExist),
],
),
),
浮动操作按钮:浮动操作按钮(
onPressed:_toggleUserState,
工具提示:“增量”,
子:图标(Icons.add),
),
);
}
Widget\u getStories(bool用户){
返回SingleChildScrollView(
滚动方向:轴水平,
子级:getStory(用户),
);
}
小部件getStory(bool用户){
返回容器(
保证金:所有(12),
子:user==true
?立柱(
儿童:[
容器(
身高:55,
宽度:55,
儿童:手势检测器(
onTap:(){
//连接函数
打印(“连接函数”);
},