首页 文章

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

提问于
浏览
0

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

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

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')
      )
    ),
  );
};

1 回答

  • 4

    我认为这是你的问题实际上是什么?

    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
          ),
        );
      }
    }
    

相关问题