flutter 首次运行的一个错误,嗯,又是一个坑

错误信息是:

Exception in thread "main" java.net.ConnectException: Connection timed out: connect

这其实是因为无法下载gradle导致的,正确的做法是先运行flutter run -v查看究竟下载的是哪个版本的gradle,然后直接下载下来,放到:

C:\Users\xxxxxx\.gradle\wrapper\dists\gradle-6.x-all\xxxxxxxxxxxxxx

这里的各种xxx就是你自己的信息,然后重新flutter run,就可以了。

又都是眼泪~~~~~

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的 ScreenUtil 类,可以用于 Flutter 屏幕适配: ```dart import 'package:flutter/material.dart'; class ScreenUtil { static late double _screenWidth; static late double _screenHeight; static late double _pixelRatio; static late double _statusBarHeight; static late double _bottomBarHeight; static late double _textScaleFactor; static void init(BuildContext context) { MediaQueryData mediaQuery = MediaQuery.of(context); _screenWidth = mediaQuery.size.width; _screenHeight = mediaQuery.size.height; _pixelRatio = mediaQuery.devicePixelRatio; _statusBarHeight = mediaQuery.padding.top; _bottomBarHeight = mediaQuery.padding.bottom; _textScaleFactor = mediaQuery.textScaleFactor; } static double get screenWidth => _screenWidth; static double get screenHeight => _screenHeight; static double get pixelRatio => _pixelRatio; static double get statusBarHeight => _statusBarHeight; static double get bottomBarHeight => _bottomBarHeight; static double get textScaleFactor => _textScaleFactor; static double setWidth(double width) => width * _screenWidth / 375; static double setHeight(double height) => height * _screenHeight / 667; static double setSp(double fontSize) => fontSize * _textScaleFactor; static double setBorderRadius(double radius) => radius * _pixelRatio; } ``` 使用方法: 在 MaterialApp 的 build 方法中调用 ScreenUtil.init(context),初始化 ScreenUtil 类。 ```dart class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { ScreenUtil.init(context); return MaterialApp( title: 'Flutter Demo', theme: ThemeData( primarySwatch: Colors.blue, ), home: MyHomePage(), ); } } ``` 在布局中使用 ScreenUtil.setWidth、ScreenUtil.setHeight 和 ScreenUtil.setSp 方法来适配宽度、高度和字体大小。 ```dart Container( width: ScreenUtil.setWidth(100), height: ScreenUtil.setHeight(100), margin: EdgeInsets.only(top: ScreenUtil.setHeight(20)), padding: EdgeInsets.symmetric(horizontal: ScreenUtil.setWidth(20)), decoration: BoxDecoration( borderRadius: BorderRadius.circular(ScreenUtil.setBorderRadius(10)), color: Colors.grey[200], ), child: Text( 'Hello, World!', style: TextStyle(fontSize: ScreenUtil.setSp(16)), ), ), ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值