我在flutter中的UI有问题.任何有widget经验的人请帮助我.

Firstly, here's the target from figma :

enter image description here

日期和时间都使用下拉菜单.

This is what it become after my code :

enter image description here

This is my code :

Container(
   alignment: Alignment.centerLeft,
   child: Text('Schedule',
       style: TextStyle(
            color: Color.fromRGBO(78, 125, 150, 1),
            fontSize: 12,
            fontWeight: FontWeight.w600
))),
        
Row(
  mainAxisAlignment: MainAxisAlignment.spaceBetween,
  children: [
        Container(
            height: 40,
            width: MediaQuery.of(context).size.shortestSide * 0.5,
            color: Colors.white,
            child: DropdownButtonFormField(
                onChanged: (value) {},
                  items: _dropdownDate.map((value) =>
                      DropdownMenuItem(
                        child: Text(value),
                        value: value,
                      )).toList(),
                elevation: 4,
                dropdownColor: Colors.white,
                decoration: InputDecoration(
                  prefixIcon: const Icon(Icons.calendar_month, size: 16),
                  hintText: 'Select Date',
                  hintStyle: TextStyle(fontSize: 12),
                  focusedBorder: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(10),
                      borderSide: const BorderSide(
                        width: 0.8,
                        color: Colors.grey,
                      )),
                  enabledBorder: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(10),
                      borderSide: const BorderSide(
                        width: 0.8,
                        color: Colors.grey,
                      )),
                  contentPadding: const EdgeInsets.all(16),
                  fillColor: Colors.white
              ),
            ),
        ),
        Container(
            height: 40,
            width: MediaQuery.of(context).size.shortestSide * 0.3,
            color: Colors.white,
            child: DropdownButtonFormField(
              onChanged: (value) {},
              items: _dropdownTime.map((value) =>
                  DropdownMenuItem(
                    child: Text(value),
                    value: value,
                  )).toList(),
              elevation: 4,
              dropdownColor: Colors.white,
              decoration: InputDecoration(
                  prefixIcon: const Icon(Icons.watch_later_outlined, size: 16),
                  focusedBorder: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(10),
                      borderSide: const BorderSide(
                        width: 0.8,
                        color: Colors.grey,
                      )),
                  enabledBorder: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(10),
                      borderSide: const BorderSide(
                        width: 0.8,
                        color: Colors.grey,
                      )),
                  contentPadding: const EdgeInsets.all(16),
                  fillColor: Colors.white
              ),
            ),
        ),
   ],
),

I want to make the size in the row like this :

50%下拉日期,

and the problem are :

  1. 我不知道如何将其配置为行空间的百分比,而不是使用MediaQuery.of(context.size).宽度,因为我已经使用了边距.
  2. 如何使百分比自动适合用户使用的任何设备.

希望能在这里解决这个问题,谢谢大家~

Update,使用布局生成器后:

enter image description here

推荐答案

你应该使用LayoutBuilder而不是MediaQuery.这将给你包装好的小部件内的可用空间.

代码更改如下.结果如下:

Screenshot

        child: LayoutBuilder(
          builder: (context, constraints) {
            return Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Container(
                  // height: 40,
                  width: constraints.maxWidth * 0.5,
                  color: Colors.white,
                  child: DropdownButtonFormField(
                    onChanged: (value) {},
                    items: _dropdownDate
                        .map((value) => DropdownMenuItem(
                              child: Text(value),
                              value: value,
                            ))
                        .toList(),
                    elevation: 4,
                    dropdownColor: Colors.white,
                    decoration: InputDecoration(
                        prefixIcon: const Icon(Icons.calendar_month, size: 16),
                        hintText: 'Select Date',
                        hintStyle: TextStyle(fontSize: 12),
                        focusedBorder: OutlineInputBorder(
                            borderRadius: BorderRadius.circular(10),
                            borderSide: const BorderSide(
                              width: 0.8,
                              color: Colors.grey,
                            )),
                        enabledBorder: OutlineInputBorder(
                            borderRadius: BorderRadius.circular(10),
                            borderSide: const BorderSide(
                              width: 0.8,
                              color: Colors.grey,
                            )),
                        contentPadding: const EdgeInsets.all(16),
                        fillColor: Colors.white),
                  ),
                ),
                Container(
                  // height: 40,
                  width: constraints.maxWidth * 0.3,
                  color: Colors.white,
                  child: DropdownButtonFormField(
                    onChanged: (value) {},
                    items: _dropdownTime
                        .map((value) => DropdownMenuItem(
                              child: Text(value),
                              value: value,
                            ))
                        .toList(),
                    elevation: 4,
                    dropdownColor: Colors.white,
                    decoration: InputDecoration(
                        prefixIcon: const Icon(Icons.watch_later_outlined, size: 16),
                        focusedBorder: OutlineInputBorder(
                            borderRadius: BorderRadius.circular(10),
                            borderSide: const BorderSide(
                              width: 0.8,
                              color: Colors.grey,
                            )),
                        enabledBorder: OutlineInputBorder(
                            borderRadius: BorderRadius.circular(10),
                            borderSide: const BorderSide(
                              width: 0.8,
                              color: Colors.grey,
                            )),
                        contentPadding: const EdgeInsets.all(16),
                        fillColor: Colors.white),
                  ),
                ),
              ],
            );
          }
        ),

Flutter相关问答推荐

无法在Flatter中设计正确的布局

如何从外部控制有状态窗口小部件本身?

我可以在iOS设备上使用Ffltter实现Google Pay按钮吗?

允许冻结在初始化时使用工厂

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

Ffmpeg_kit_fltter:^6.0.3版权问题

如何处理平台游戏的对象冲突?

如何在 Dart/Flutter 中克隆 Future

Flutter 火焰相机抖动已弃用

如果有空间则居中CustomScrollView

Flutter屏幕适配 - 在模拟器和真实手机上文本尺寸不同

Flutter蓝牙 - 如何通过蓝牙从设备中获取文件?

Riverpod 2.3.6:AutoDisposeNotifier和ref.onDispose自动处理

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

Flutter ImagePicker - 在 onPressed 中异步获取图像显示 lint 警告

如何建立最近的文件列表?

无效的 depfile: C:\xxx\xxx\Flutter\project_name\.dart_tool\flutter_build\df4b308df1ee4bce22c56c71751554d1\kernel_snapshot.d 无效

根据索引/变量更改小部件的 colored颜色

如何将图像网络装箱到具有边界半径的容器中

解密 Modulr 安全令牌