如何在 Flutter 中创建手机/平板电脑布局(MediaQuery 和 LayoutBuilders)教程 - 汇站网

如何在 Flutter 中创建手机/平板电脑布局(MediaQuery 和 LayoutBuilders)教程

2023-11-23 0 568

正文:

使用 Flutter,我们可以使用 MediaQueryLayoutBuilders 来创建适用于手机和平板电脑的布局。这些工具可以帮助我们根据设备的屏幕尺寸和方向来调整布局。通过使用 MediaQuery,我们可以获取设备的屏幕尺寸和方向信息,并根据这些信息来调整布局的大小和位置。而 LayoutBuilders 则可以帮助我们根据不同的约束条件来构建不同的布局。这些工具的结合使用,可以让我们的应用在不同的设备上都能够呈现出最佳的布局效果。
如何在 Flutter 中创建手机/平板电脑布局(MediaQuery 和 LayoutBuilders)教程
首先,我们需要创建一个函数来返回两个 GridView。

// https://www.huizhanii.com
gridviewForPhone() {
   return Padding(
     padding: EdgeInsets.all(5.0),
     child: GridView.count(
       crossAxisCount: 2,
       childAspectRatio: 1.0,
       mainAxisSpacing: 4.0,
       crossAxisSpacing: 4.0,
       children: List.generate(100, (index) {
         return Card(
           child: Container(
             alignment: Alignment.center,
             color: Colors.red[100 * (index % 9)],
             child: Text('$index'),
           ),
         );
       }),
     ),
   );
 }
 
 gridviewForTablet() {
   return Padding(
     padding: EdgeInsets.all(5.0),
     child: GridView.count(
       crossAxisCount: 4,
       childAspectRatio: 1.0,
       mainAxisSpacing: 4.0,
       crossAxisSpacing: 4.0,
       children: List.generate(100, (index) {
         return Card(
           child: Container(
             alignment: Alignment.center,
             color: Colors.green[100 * (index % 9)],
             child: Text('$index'),
           ),
         );
       }),
     ),
   );
 }

在这个函数中,Phone GridView 将每行显示 2 个单元格,而平板电脑将每行显示 4 个。

使用 MediaQuery

为了确定布局,我们将使用 MediaQuery。为此,我们将声明三个变量
我们假设 600.0 作为平板电脑的边界。

// https://www.huizhanii.com
final double shortestSide = MediaQuery.of(context).size.shortestSide; // get the shortest side of device
final bool useMobileLayout = shortestSide < 600.0; // check for tablet
final Orientation orientation = MediaQuery.of(context).orientation; // get the orientation

现在我们的构建方法将更改为

// https://www.huizhanii.com
@override
Widget build(BuildContext context) {
   
  final double shortestSide = MediaQuery.of(context).size.shortestSide; // get the shortest side of device
  final bool useMobileLayout = shortestSide < 600.0; // check for tablet
  final Orientation orientation = MediaQuery.of(context).orientation; // get the orientation
 
  return Scaffold(
    appBar: AppBar(
      title: Text(widget.title),
    ),
    body: useMobileLayout
        ? gridviewForPhone(orientation)
        : gridviewForTablet(orientation),
    ...

现在我们将方法更改为

// https://www.huizhanii.com
gridviewForPhone(Orientation orientation) {
  ...
  GridView.count(
    crossAxisCount: orientation.portrait ? 2 : 4
  ...
}
 
gridviewForTablet(Orientation orientation) {
...
 GridView.count(
   crossAxisCount: orientation.portrait ? 4 : 6
}

使用 LayoutBuilder

这是进行比较的示例

// https://www.huizhanii.com
LayoutBuilder(
  builder: (BuildContext context, BoxConstraints constraints) {
      if (constraints.maxWidth < 600.0) {
          return gridviewForPhone();
      } else {
          return gridviewForTablet();
      }
},

为了简单起见,我们只是使用构建器约束检查设备的最大宽度。
因此,一旦您的设备超过 600.0 的最大宽度,可能在横向模式下它最终会显示平板电脑的 GridView。

转载请注明:汇站网 » 如何在 Flutter 中创建手机/平板电脑布局(MediaQuery 和 LayoutBuilders)教程

收藏 (0)

微信扫一扫

支付宝扫一扫

点赞 (0)

感谢您的来访,获取更多精彩资源请收藏本站。

本站声明

本资源仅用于个人学习和研究使用,禁止用于任何商业环境!

 1.  本网站名称:汇站网
 2.  本站永久网址:https://www.huizhanii.com/
 3.  本站所有资源来源于网友投稿和高价购买,所有资源仅对编程人员及源代码爱好者开放下载做参考和研究及学习,本站不提供任何技术服务!
 4.  未经原版权作者许可,禁止用于任何商业环境,任何人不得擅作它用,下载者不得用于违反国家法律,否则发生的一切法律后果自行承担!
 5.  为尊重作者版权,请在下载24小时内删除!请购买原版授权作品,支持你喜欢的作者,谢谢!
 6.  若资源侵犯了您的合法权益, 请持您的版权证书和相关原作品信息来信通知我们请来信     通知我们我们会及时删除,给您带来的不便,我们深表歉意!
 7.  如下载链接失效、广告或者压缩包问题请联系站长处理!
 8.  如果你也有好源码或者教程,可以发布到网站,分享有金币奖励和额外收入!
 9.  本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
 10.  因源码具有可复制性,一经赞助 ,不得以任何形式退款。
 11.  更多详情请点击查看

汇站网 flutter 如何在 Flutter 中创建手机/平板电脑布局(MediaQuery 和 LayoutBuilders)教程 https://www.huizhanii.com/33897.html

汇站

站长资源下载中心-找源码上汇站

常见问题
  • 如果付款后没有弹出下载页面,多刷新几下,有问题联系客服!
查看详情
  • 本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。
查看详情

相关文章

发表评论
暂无评论
  随机评论 表情开关按钮图片
表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情表情
登录后评论
联系官方客服

为您解决烦忧 - 24小时在线 专业服务

(汇站网)一个专注站长资源的平台网站,提供最新的网站模板和整站源码,内容包含各类精品网页模板,企业网站模板,网站模板,DIV+CSS模板,织梦模板,帝国cms模板,discuz模板,wordpress模板,个人博客论坛模板,上千种免费网页模板下载尽在汇站网.找源码上汇站.huizhanii.com