Flutter Flex () как поменять положение детей? - PullRequest
0 голосов
/ 15 апреля 2020

Я использую Flex() виджет для достижения разной компоновки между ориентациями экрана, как показано ниже:

final _isPortrait = MediaQuery.of(context).orientation == Orientation.portrait;

Flex(
  direction: _isPortrait ? Axis.horizontal : Axis.vertical,
  children: <Widget>[
    Expanded(flex: _isPortrait ? 2 : 1, child: BottomButton1()),
    Expanded(flex: _isPortrait ? 1 : 0, child: BottomButton2()),
  ],
),

Когда ориентация портретная, конечно, BottomButton1() будет слева от первого ребенка.

Однако, когда ориентация в альбомной ориентации, я хочу поменять местами позиции BottomButton1() и BottomButton2(), поэтому позиция первого ребенка теперь ниже позиции второго ребенка.

1 Ответ

0 голосов
/ 15 апреля 2020

Я думаю, что это должно помочь вам

class _HelperState extends State<Helper> {
  RaisedButton button1 = RaisedButton(
    child: Text('Button 1'),
    onPressed: () {},
  );
  RaisedButton button2 = RaisedButton(
    child: Text('Button 2'),
    onPressed: () {},
  );
  @override
  Widget build(BuildContext context) {
    final _isPortrait = false;
    print(_isPortrait);
    return Scaffold(
      body: SafeArea(
        child: Center(
          child: Flex(
            direction: _isPortrait ? Axis.horizontal : Axis.vertical,
            children: <Widget>[
              Expanded(
                flex: _isPortrait ? 2 : 1,
                child: _isPortrait ? button1 : button2,
              ),
              Expanded(
                flex: _isPortrait ? 1 : 0,
                child: _isPortrait ? button2 : button1,
              ),
            ],
          ),
        ),
      ),
    );
  }
}

Очевидно, вам нужно заменить в ваших виджетах, дайте мне знать, если вам нужно что-то еще

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...