我正在try 根据我的图纸制作计算器的用户界面,

在这里,我把屏幕分成两个展开的容器,顶部一个用于输出,底部一个用于按钮…

在底部容器中,我采用了网格视图来显示所有按钮,

我想要适合所有按钮的底部区域,而不是网格视图的滚动效果.

在我的代码中...网格视图没有显示最后一行...我必须向下滚动才能查看它...

enter image description here here is my code

Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Column(
          children: [
            Expanded(child: Container(color: Colors.orange,)),
            Expanded(
                child: GridView.builder(
                  itemCount: buttons.length,
                    gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                        crossAxisCount: 4),
                    itemBuilder: (context, index) {
                      return ButtonWidget(color: Colors.grey,
                          ontap: () {},
                          caption: buttons[index].toString(),
                          textcolor: Colors.black);
                    })),
          ],
        ),
      ),
    );
  }

这是巴顿的班级


class ButtonWidget extends StatelessWidget {
  final color;
  final textcolor;
  final String caption;
  final VoidCallback ontap;

  const ButtonWidget(
      {Key? key,
      required this.color,
      required this.ontap,
      required this.caption,
      required this.textcolor})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.all(8.0),
      child: Container(
        decoration: BoxDecoration(
          color: Colors.grey[400],
          borderRadius: BorderRadius.circular(100),
        ),
        child: Center(
            child: Text(
          caption.toString(),
          style: GoogleFonts.actor(fontSize: 30,fontWeight: FontWeight.bold),
        )),
      ),
    );
  }
}


推荐答案

1.把你的Expanded换成GridView.

  1. 禁用GridView的滚动. 像这样.

       GridView.builder(
        /// Add these two lines
         shrinkWrap: true,
         physics: const NeverScrollableScrollPhysics(),
         /// Your code,
        ),
    

Tip:个 如果你不想在顶部有一个小的容器,你可以像这样调整按钮的长宽比.但它会让按钮看起来有点难看.

gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                        crossAxisCount: 4,
///Add this, tweak the value as you wish
childAspectRatio: 1.3,
),

Have Fun!

Flutter相关问答推荐

如何让GridView.builder用另外两个小部件来占据屏幕的剩余部分

当仅点击一行时,所有行都被选中

分页控制器在到达页面末尾之前或在初始化页面时对每个索引调用API

带有附加图像的ListTile

请求已在 flutter 中发送到 django 服务器,并且已返回响应,但条件仍然为 false

带按钮的 Riverpod future Provider

来自服务器的数据未分配给变量

Flutter url_launcher 插件抛出java.lang.IllegalArgumentException:接收器未注册:io.flutter.plugins.urllauncher.WebViewActivity

更改下拉按钮的背景 colored颜色 - flutter

Flutter blue plus 库,当try 从 esp32 驱动程序获取 PlatformException 时读取特性

Flutter/Riverpod 创建复杂对象的副本,其值在某处发生变化

如何在 flutter 中使用带有有效负载数据的重定向来执行 firebase 推送通知.?

将 String 与 List 元素进行比较时出现问题

为什么轮播加载图片很慢?

有没有办法为文本中的下划线创建自定义样式?

我无法从 Cloud firestore 访问文档字段并在 statefulwidget 类中显示它

如何将 void 函数放入单独的 dart 文件的单独页面中

Firebase Cloud Messaging (Flutter):订阅主题时出现错误消息

更新未知文档 ID firebase\flutter 中的字段

未处理的异常:FileSystemException:无法打开文件,路径 = 'assets/res.zip'(操作系统错误:没有这样的文件或目录,errno = 2)