我如何在颤抖的孩子体内使用if / else语句?

时间:2020-09-06 06:26:27

标签: flutter if-statement dart flutter-layout screen-orientation

我想使用if / else语句

只有在方向为横向的情况下,我才能为列设置 MainAxisAlignment.spaceEvenly 。因此,它不会改变我的纵向布局。

这是代码

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

我已经尝试了多种方法

1 个答案:

答案 0 :(得分:1)

默认值为MainAxisAlignment mainAxisAlignment = MainAxisAlignment.start,,因此您可以执行以下操作;

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

还将使用portrait的默认值