我怎么做才能让原material 按钮的大小与我的设备相对.

我想让主页中的按钮具有恒定的宽度,而不管其中的文本有多大,并且该宽度应该与屏幕大小相关.

Container(
                margin: EdgeInsets.all(10),
                child: FittedBox(
                  child: RawMaterialButton(
                    width: 150px,
                    fillColor: const Color(0xFF411F97),
                    padding: const EdgeInsets.fromLTRB(50, 10, 50, 10),
                    shape: const RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(20),)),
                    child: const Text("San Francisco Area", style: TextStyle(fontSize: 50, color: Colors.white,), textAlign: TextAlign.center,),
                    //Sends user to weather list view
                    onPressed: () {
                      Navigator.push(
                        context,
                        MaterialPageRoute(builder: (context) => WeatherList(title: "San Francisco Metro Area", cityNames: sfaNames,)),
                      );
                    },
                  ),
                ),
                //Box shadow is essentially made here for the botton
                decoration: BoxDecoration(
                  color: Colors.teal,
                  borderRadius: BorderRadius.circular(20),
                  boxShadow: const [
                    BoxShadow(
                      color: Colors.black26,
                      offset: Offset(10, 10), // Shadow position
                    ),
                  ],
                )
            ),

推荐答案

MediaQuery.of(context).size.width - Gets the screen size width for any device.

使用所需参数将按钮包裹在SizedBox英寸内,例如任何设备上的半个屏幕

SizedBox( width: MediaQuery.of(context).size.width / 2,

            child: FittedBox(
              child: RawMaterialButton(
                fillColor: const Color(0xFF411F97),
                padding: const EdgeInsets.fromLTRB(50, 10, 50, 10),
                shape: const RoundedRectangleBorder(
                    borderRadius: BorderRadius.all(
                  Radius.circular(20),
                )),
                child: const Text(
                  "San Francisco Area",
                  style: TextStyle(
                    fontSize: 50,
                    color: Colors.white,
                  ),
                  textAlign: TextAlign.center,
                ),
                //Sends user to weather list view
                onPressed: () {},
              ),
            ),
            //Box shadow is essentially made here for the botton
           ));

Flutter相关问答推荐

类型flatter doctor get zsh:未找到命令:macO中flatter

Android Emulator冻结,但在调整窗口大小时解冻

为什么Riverpod生成器在这种情况下不生成AsyncNotiator?

如何在2024年使用Ffltter将用户数据保存在云FireStore中?

无效参数(S):隔离消息中的非法参数:try 旋转图像时对象不可发送

当键盘在抖动中打开时,如何才能看到包装在SingleChildScrollView中的列中的最后一个小工具?

查询满足AND条件的文档的FiRestore

GitHub Actions工作流secret中的特殊字符未被保留

try 使用fl_Chart of Ffltter,但在导入它时遇到以下错误:Error:';TextScaler';is;t a type

升级到Ffltter 3.16后,应用程序栏、背景 colored颜色 、按钮大小和间距都会发生变化

Flutter GestureDetector单击问题-堆叠外面的按钮不可点击

了解多Provider 和流

尽管海拔设置为 0,如何删除 appBar 下的下划线

无法在 ListView (Flutter) 中限制 Card 的最大宽度

围绕父组件旋转位置组件

如何从另一个页面访问 Firebase 值?

Flutter Bloc 使用 Cubit 监听流和emits 状态

为什么 Flutter riverpod 直接分配不起作用但方法可以

将一个 Flutter 应用程序集成到另一个 Flutter 应用程序中

为什么这个循环只发生一次?Flutter /dart