How do i use if/else statement inside a child in flutter?

295

the default value is MainAxisAlignment mainAxisAlignment = MainAxisAlignment.start,, therefore you can do as below;

child: Column(
  mainAxisAlignment: orientation == Orientation.landscape? 
      MainAxisAlignment.spaceEvenly : MainAxisAlignment.start, 
  children: AppDrawer.getDrawerOptions(),
),

which will also use the default value for portrait

Share:
295
Amin Zaidi
Author by

Amin Zaidi

Updated on December 24, 2022

Comments

  • Amin Zaidi
    Amin Zaidi over 1 year

    I want to use if/else statement

    So that i can have MainAxisAlignment.spaceEvenly for column only if the orientation is landscape. So, it doesn't change my layout in portrait orientation.

    Here's the code

    class _MobileLayout extends StatelessWidget {
      const _MobileLayout({Key key}) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        var orientation = MediaQuery.of(context).orientation;
        return Container(
          width: orientation == Orientation.portrait ? 250 : 100,
          decoration: BoxDecoration(
            color: Colors.white,
            boxShadow: [
              BoxShadow(
                blurRadius: 16,
                color: Colors.black12,
              )
            ],
          ),
          child: Column(
            children: AppDrawer.getDrawerOptions(),
          ),
        );
      }
    }
    
    

    I've tried it multiple ways around