android - Flutter:为 ButtonBar 中的两个按钮提供相等的宽度,并为长文本提供动态高度

标签 android flutter flutter-layout

下面是我的简单用例:我想要两个水平相邻的按钮。在 native android(这是我来自的地方)中,我会把它们放在 LinearLayout 中,并给它们每个权重 1,并将它们的高度设置为 wrap_content

现在,我在 ButtonBar 小部件中放置了两个 RaisedButton,但是在我运行该应用程序的地方,我可以看到第二个被剪裁了。我希望它们的间距相等,并根据文本具有动态高度。我怎样才能在 flutter 中实现同样的目标?以下是我到目前为止所做的尝试:

import 'package:flutter/material.dart';

class NewScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
            automaticallyImplyLeading: true,
            title: Text("Building layouts"),
            leading: IconButton(
              icon: Icon(Icons.arrow_back),
              onPressed: () => Navigator.pop(context, false),
            )),
        body: myLayoutWidget(),
      ),
    );
  }
}

// replace this method with code in the examples below
Widget myLayoutWidget() {
  return Container(
    child: Column(
      children: <Widget>[
        ButtonBar(
          children: <Widget>[
            RaisedButton(
              onPressed: () {},
              child: Text("Very long text button",),
            ),
            RaisedButton(
              child: Text("Very very very very long text button"),
              color: Colors.red,
              onPressed: () {},
            )
          ],
        ),
      ],
    ),
  );
}

现在是这样的: Screenshot

最佳答案

尝试使用 Row 而不是 Button Bar 并将按钮添加到 Expanded parent

Widget myLayoutWidget() {
  return Container(
    child: Row(

      children: <Widget>[
        Expanded(
          child: RaisedButton(
            onPressed: () {},
            child: Text("Very long text button",),
          ),
        ),
        Expanded(
          child: RaisedButton(
            child: Text("Very very very very long text button"),
            color: Colors.red,
            onPressed: () {},
          ),
        )
      ],
    ),
  );
}

关于android - Flutter:为 ButtonBar 中的两个按钮提供相等的宽度,并为长文本提供动态高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57271292/

相关文章:

Flutter - 卡住包 - 如何正确组合类

sqlite - 如何在 Flutter 中使用 SQFlite 进行数据库查询

dart - flutter 底片 : How to change height when user interacts with the sheet

dart - 在 _MaterialAppState 中找不到路由 "home-page"的生成器

android - 如何与嵌入式 Activity 通信

android - 缓慢向上滚动一个 ScrollView

android - 为什么 TextView 只在 Galaxy Nexus 中显得更大?

android - 如何获取android中所有应用程序在给定时间段(例如,上个月)内使用的移动数据总量的统计信息

flutter - 如何在flutter中拥有相同数据类型的多个流提供者?

iPhone 上的 Flutter 底部填充颜色