如何让 Flutter 按钮尽可能宽到最大宽度?

Mat*_*t R 5 button flutter flutter-layout

在下面的布局中,我希望按钮能够在屏幕上增长到尽可能宽,直至达到最大宽度。我尝试了以下方法,但不起作用(按钮始终尽可能宽):

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: SafeArea(
          child: Center(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: [
                TextFormField(),
                TextFormField(),
                ConstrainedBox(
                  constraints: BoxConstraints(maxWidth: 200),
                  child: RaisedButton(child: Text("button"), onPressed: () {}),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

为了扩展我正在寻找的布局:按钮的宽度必须与以下两个数量中较小的一个相同:1)屏幕的宽度,2)给定的固定最大宽度。

示例场景:

A) 屏幕宽度为 1000 像素,给定的固定最大宽度为 600 像素,则按钮宽度为 600 像素。

B) 屏幕宽度为 400 像素,并且给定的固定最大宽度为 600 像素,则按钮宽度将为 400 像素。

ook*_*.kb 1

您可以删除crossAxisAlignment: CrossAxisAlignment.stretch,因为TextFormField无论如何都会拉伸。此代码将使按钮宽度达到最大宽度尺寸,但不大于屏幕宽度:

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) => MaterialApp(
        home: Scaffold(
          body: SafeArea(
            child: Center(
              child: Column(
                children: [
                  TextFormField(),
                  TextFormField(),
                  Container(
                    width: 200,
                    child: RaisedButton(
                      onPressed: () {},
                      child: Text('button'),
                    ),
                  ),
                  Container(
                    width: 200,
                    child: RaisedButton(
                      onPressed: () {},
                      child: Text(
                        'Button with long text asf sadf sdf as df s df as '
                        'dfas dfsd f asfauhiusg isdugfisudgfi asudgk usgkdu'
                        'ksdfhk sudfhk sudhfk',
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ),
        ),
      );
}
Run Code Online (Sandbox Code Playgroud)