4

I need to know is it possible to use ternary/if else inside flutter widgets.

I'm trying to create a buttonCreator widget which will take few parameters, one of which will be the background. I need to check whether the widget has background enabled or not. What i have is this but i have no idea how to use it in real dart code.

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

3 Answers 3

7

I think this is what your question is actually about ?

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
      ),
    );
  }
}
Sign up to request clarification or add additional context in comments.

Comments

2
Container buildButton(String text, bool bg){
  return new Container(
    decoration: new BoxDecoration(
      border: new Border.all(color: Colors.white),
      color: bg ? Colors.white : null, // you can use ternary operator here
    ),
    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')
      )
    ),
  );
};

Comments

1
decoration: new BoxDecoration(
    border: new Border.all(color: Colors.white),
    color: bg ? Colors.transparent : Colors.red, // you can use ternary operator here
),

Modified from AleksTi's answer. color property cannot be null.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.