gpt4 book ai didi

dart - 如何在 flutter 小部件中使用条件语句/三元

转载 作者:IT王子 更新时间:2023-10-29 06:48:28 29 4
gpt4 key购买 nike

我需要知道是否可以在 flutter 小部件中使用三元/if else。

我正在尝试创建一个 buttonCreator 小部件,它将采用几个参数,其中之一将是背景。我需要检查小部件是否启用了背景。我有的是这个,但我不知道如何在真正的 Dart 代码中使用它。

Container buildButton(String text, bool bg){
return new Container(
decoration: new BoxDecoration(
border: new Border.all(color: Colors.white),
if (bg != true ? color: Colors.white : '')
),
padding: new EdgeInsets.all(15.0),
margin: new EdgeInsets.symmetric(horizontal: 10.0, vertical: 5.0),
child: new Center(
child: new Text(
text, style: new TextStyle(color: Colors.white, fontFamily: 'Monsterrat')
)
),
);
};

最佳答案

我想这就是您的问题所在?

enter image description here

import 'package:flutter/material.dart';

void main() => runApp(new MaterialApp(
home: new Home(),
));


class Home extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new Scaffold(
body: new Center(
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Flexible(
child: new MyContainer(
color: Colors.red,
),
),
new Flexible(
child: new MyContainer(
img: "http://www.clker.com/cliparts/F/v/O/6/E/c/cartoon-rubber-duck-hi.png",
),
)
],
),
),
);
}
}

class MyContainer extends StatelessWidget {
String img;
Color color;
MyContainer ({this.img,this.color});
@override
Widget build(BuildContext context) {
return this.img!=null? new Container(
decoration: new BoxDecoration(
image:new DecorationImage(
image: new NetworkImage(this.img)
)
),
): new Container(
decoration: new BoxDecoration(
color: this.color
),
);
}
}

关于dart - 如何在 flutter 小部件中使用条件语句/三元,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49720332/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com