【问题标题】:Flutter nested Rows MainAxisAlignment颤振嵌套行 MainAxisAlignment
【发布时间】:2019-09-10 14:34:30
【问题描述】:

我想这样做:

但这是我实际得到的:

这是我的代码:

Row itemTransaction(BuildContext context, Transaction transaction) {
  /// This is the function that will build each item of our transaction list.
  return new Row(
    children: <Widget>[
      new Container(
        width: MediaQuery.of(context).size.width / 6,
        height: MediaQuery.of(context).size.width / 6,
        child: new Image.asset(
          (transaction.sent) ? "images/tx_output.png" : "images/tx_input.png",
        ),
      ),
      new Column(
        children: <Widget>[
          new Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: <Widget>[
              discoderyText("test"),
              discoderyText("test 2"),
            ],
          ),
          discoderyText("SOME HASH KEY"),
        ],
      ),
    ],
  );
}

discoderyText 基本上是一个自定义文本(即带有颜色)。

如您所见,我的行设置了mainAxisAlignment: MainAxisAlignment.spaceBetween,因此testtest2应该位于对面

当我删除图像并且只返回一个包含两个TextRow 并设置mainAxisAlignment 时,它可以工作。似乎嵌套行不能使用此变量

这基本上是我的看法:

【问题讨论】:

    标签: flutter dart flutter-layout


    【解决方案1】:

    截图

    Row(
      children: <Widget>[
        Container(
          width: MediaQuery.of(context).size.width / 6,
          height: MediaQuery.of(context).size.width / 6,
          child: CircleAvatar(backgroundImage: AssetImage(chocolateImage),),
        ),
        Expanded( // add this
          child: Padding(
            padding: const EdgeInsets.all(8.0), // give some padding
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              mainAxisSize: MainAxisSize.min, // set it to min
              children: <Widget>[
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: <Widget>[
                    Text("test"),
                    Text("test 2"),
                  ],
                ),
                Text("SOME HASH KEY HERE"),
              ],
            ),
          ),
        ),
      ],
    )
    

    【讨论】:

    • 感谢扩展小部件,这非常有效,非常感谢!但是,似乎 mainAxisSize 和列 MainAxisAlignment 是无用的(如果我将其设置为不同的值,或者如果我只是删除它们,它不会对我的视图提供任何更改)。那么他们在这里的目的是什么?
    • mainAxisSize 仅在您不向父级 Row 提供固定约束时才有意义。而mainAxisAlignment 制作了所有的图片,文字就在Row 的中间。
    【解决方案2】:

    布局框架允许您根据需要将行和列嵌套在行和列中。让我们看一下以下布局的概述部分的代码:

    完整示例

    import 'package:flutter/material.dart';
    import 'package:flutter/rendering.dart' show debugPaintSizeEnabled;
    
    void main() {
      debugPaintSizeEnabled = false; // Set to true for visual layout
      runApp(MyApp());
    }
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter layout demo',
          home: buildHomePage('Strawberry Pavlova Recipe'),
        );
      }
    
      Widget buildHomePage(String title) {
        final titleText = Container(
          padding: EdgeInsets.all(20),
          child: Text(
            'Strawberry Pavlova',
            style: TextStyle(
              fontWeight: FontWeight.w800,
              letterSpacing: 0.5,
              fontSize: 30,
            ),
          ),
        );
    
        final subTitle = Text(
          'Pavlova is a meringue-based dessert named after the Russian ballerina '
          'Anna Pavlova. Pavlova features a crisp crust and soft, light inside, '
          'topped with fruit and whipped cream.',
          textAlign: TextAlign.center,
          style: TextStyle(
            fontFamily: 'Georgia',
            fontSize: 25,
          ),
        );
    
        // #docregion ratings, stars
        var stars = Row(
          mainAxisSize: MainAxisSize.min,
          children: [
            Icon(Icons.star, color: Colors.green[500]),
            Icon(Icons.star, color: Colors.green[500]),
            Icon(Icons.star, color: Colors.green[500]),
            Icon(Icons.star, color: Colors.black),
            Icon(Icons.star, color: Colors.black),
          ],
        );
        // #enddocregion stars
    
        final ratings = Container(
          padding: EdgeInsets.all(20),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: [
              stars,
              Text(
                '170 Reviews',
                style: TextStyle(
                  color: Colors.black,
                  fontWeight: FontWeight.w800,
                  fontFamily: 'Roboto',
                  letterSpacing: 0.5,
                  fontSize: 20,
                ),
              ),
            ],
          ),
        );
        // #enddocregion ratings
    
        // #docregion iconList
        final descTextStyle = TextStyle(
          color: Colors.black,
          fontWeight: FontWeight.w800,
          fontFamily: 'Roboto',
          letterSpacing: 0.5,
          fontSize: 18,
          height: 2,
        );
    
        // DefaultTextStyle.merge() allows you to create a default text
        // style that is inherited by its child and all subsequent children.
        final iconList = DefaultTextStyle.merge(
          style: descTextStyle,
          child: Container(
            padding: EdgeInsets.all(20),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: [
                Column(
                  children: [
                    Icon(Icons.kitchen, color: Colors.green[500]),
                    Text('PREP:'),
                    Text('25 min'),
                  ],
                ),
                Column(
                  children: [
                    Icon(Icons.timer, color: Colors.green[500]),
                    Text('COOK:'),
                    Text('1 hr'),
                  ],
                ),
                Column(
                  children: [
                    Icon(Icons.restaurant, color: Colors.green[500]),
                    Text('FEEDS:'),
                    Text('4-6'),
                  ],
                ),
              ],
            ),
          ),
        );
        // #enddocregion iconList
    
        // #docregion leftColumn
        final leftColumn = Container(
          padding: EdgeInsets.fromLTRB(20, 30, 20, 20),
          child: Column(
            children: [
              titleText,
              subTitle,
              ratings,
              iconList,
            ],
          ),
        );
        // #enddocregion leftColumn
    
        final mainImage = Image.asset(
          'images/pavlova.jpg',
          fit: BoxFit.cover,
        );
    
        return Scaffold(
          appBar: AppBar(
            title: Text(title),
          ),
          // #docregion body
          body: Center(
            child: Container(
              margin: EdgeInsets.fromLTRB(0, 40, 0, 30),
              height: 600,
              child: Card(
                child: Row(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Container(
                      width: 440,
                      child: leftColumn,
                    ),
                    mainImage,
                  ],
                ),
              ),
            ),
          ),
          // #enddocregion body
        );
      }
    }
    

    代码来源:https://flutter.dev/docs/development/ui/layout#packing-widgets

    【讨论】:

      猜你喜欢
      • 2019-05-19
      • 1970-01-01
      • 2018-06-14
      • 2021-05-02
      • 2021-07-03
      • 2022-01-05
      • 1970-01-01
      • 1970-01-01
      • 2019-12-10
      相关资源
      最近更新 更多