Home > Software design >  Can I change the widget using if?
Can I change the widget using if?

Time:02-21

For example

if TEXT = "text1' than page = mainpage(),

else if TEXT = "text2' than page = mainpage2(),

and

after if the TEXT is other source code string various

how can I cording :<

plz help me

CodePudding user response:

You can write if condition like below

if(TEXT == "text1"){
    page = mainpage()
  }else if(TEXT == "text2"){
   page = mainpage2()
  }else{
    // set your code
  }

CodePudding user response:

Method 1

if(TEXT == "text1) 
{
page = mainpage();
}
else {
page = mainpage2(); 
}

Method 2 (using ? operator)

TEXT == "text" ?
page = mainpage() :
page = mainpage2();

CodePudding user response:

use switch case wrapped in builder method. Something like this


 enum Options { text1, text2 }
     Builder(
       builder: (context) {
         switch (option) {
            case Options.text1:
                  return Page1();
            case Options.text2:
                  return Page2();
            default:
                   return Page1();
                  }
                },
              ) 

CodePudding user response:

Container(
          padding: EdgeInsets.all(8.0),
          height: 100,
          child: Row(
            children: [
             
              true
                  ? Text("No button")
                  : ElevatedButton(onPressed: () {}, child: Text("button"))
            ],
          ),
        )

OR

Container(
          padding: EdgeInsets.all(8.0),
          height: 100,
          child: Row(
            children: [
              
              if (true)
                Text("No button")
              else
                ElevatedButton(onPressed: () {}, child: Text("button")),
            ],
          ),
        )

OR

Outside the build method declare mywidget method

Container(
          padding: EdgeInsets.all(8.0),
          height: 100,
          child: Row(
            children: [
              mywidget(),
             
            ],
          ),
        )

  mywidget() {
    if (true)
      return Text("No button");
    else
      return ElevatedButton(onPressed: () {}, child: Text("button"));
  }

OR

Outside the build method declare button variable

  var button = true
      ? Text("No button")
      : ElevatedButton(onPressed: () {}, child: Text("button"));

Row(
            children: [
              mywidget(),
              button,
              true
                  ? Text("No button")
                  : ElevatedButton(onPressed: () {}, child: Text("button")),
              if (true)
                Text("No button")
              else
                ElevatedButton(onPressed: () {}, child: Text("button")),
            ],
          )

FullCode

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized();

  runApp(MaterialApp(home: Mainwidget()));
}

class Mainwidget extends StatefulWidget {
  const Mainwidget({Key? key}) : super(key: key);

  @override
  _MainwidgetState createState() => _MainwidgetState();
}

class _MainwidgetState extends State<Mainwidget> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Container(
          padding: EdgeInsets.all(8.0),
          height: 100,
          child: Row(
            children: [
              mywidget(),
              button,
              true
                  ? Text("No button")
                  : ElevatedButton(onPressed: () {}, child: Text("button")),
              if (true)
                Text("No button")
              else
                ElevatedButton(onPressed: () {}, child: Text("button")),
            ],
          ),
        ),
      ),
    );
  }

 

  mywidget() {
    if (true)
      return Text("No button");
    else
      return ElevatedButton(onPressed: () {}, child: Text("button"));
  }

  var button = true
      ? Text("No button")
      : ElevatedButton(onPressed: () {}, child: Text("button"));
}
  • Related