flutter 写一个hello
时间: 2024-04-25 10:06:54 浏览: 104
可以使用以下代码在 Flutter 中编写一个简单的 Hello World 应用程序:
```dart
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Hello World',
home: Scaffold(
appBar: AppBar(
title: Text('Hello World'),
),
body: Center(
child: Text('Hello, World!',
style: TextStyle(fontSize: 24.0),
),
),
),
);
}
}
```
这个应用程序会在屏幕中央显示一个文本,上面写着“Hello, World!”。在这个示例中,我们使用了 Flutter 的 Material 组件库来创建应用程序的 UI。我们还定义了一个 MyApp 类,它继承了 StatelessWidget 类,用于创建应用程序的主要 UI。最后,我们通过运行 main() 函数来启动应用程序。
相关问题
flutter 写一个screen_util类,目的是适配屏幕
好的,以下是一个简单的 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)),
),
),
```
flutter 写一个screen_util类,目的是适配屏幕,支持px和rpx
好的,以下是一个支持 px 和 rpx 的 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 widthPx) => widthPx * _screenWidth / 750;
static double setHeight(double heightPx) => heightPx * _screenHeight / 1334;
static double setSp(double fontSizePx) => fontSizePx * _textScaleFactor;
static double setBorderRadius(double radiusPx) => radiusPx * _pixelRatio;
static double setRpx(double rpx) => rpx * _screenWidth / 750;
}
```
使用方法:
在 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 和 ScreenUtil.setRpx 方法来适配宽度、高度、字体大小和 rpx。
```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(32)),
),
),
```
其中,setWidth 和 setHeight 方法用于适配宽度和高度,参数单位为 px;setSp 方法用于适配字体大小,参数单位为 px;setBorderRadius 方法用于适配边框半径,参数单位为 px;setRpx 方法用于适配 rpx,即屏幕宽度的 1/750,参数单位为 rpx。
阅读全文