Using if statement to show a container in flutter

Issue

I have this certain issue so there is a URL in my flutter app where,

String URL = "www.abcd/page=$number";
int number = 1;

And a button with onTap where,

Inkwell(child:Container(child: Text("Next")),
 onTap :()=> setState({
            number++)}

Sorry I typed those codes manually maybe there is some syntax error.
So basically I want the user to tap on the next button the page number in the link increases by 1, but I want to iterate an ‘if’ function, when number> 1 then show a button where the page decrements by one and if number<1 then I don’t want the container to show.

So is there any way to achieve it?

Solution

Use a ternary operator

condition ? if True : if False

In your case use it on the on the button that decrements by 1.

number>1 ? shows Button : shows empty Sizedbox

number>1 
       ? InkWell(
        onTap: (() {
          setState(() {
            number--;
          });}),
        child: Container(
          height: 30,
          color: Colors.red[100],
          child: Text('Previous'),
          )) 
        : SizedBox(),

enter image description here

enter image description here

Answered By – pupil gudang

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published