跳至主要內容

flutter

shiori大约 3 分钟教程分享笔记

Google公司推出的dark语言编写的多平台应用

什么是flutter

Google公司推出的dark语言编写的多平台应用

Currently the most beautiful apps

Free and Open Source
官网open in new window | 中文站open in new window | Flutter中文网open in new window

起步

已拥有Android Studio, IntelliJ, VS Code等IDE可直接添加plugins。

在AS中安装

“file”-->“settings”-->“plugins”,搜索“flutter”即可,如图: flutter1open in new windowflutter2open in new windowflutter3open in new windowflutter4open in new windowflutter5open in new window

下载Flutter SDK

https://flutter.cn/docs/get-started/install/windows#get-the-flutter-sdk
(Windows) 解压文件,将flutter整个目录放到你想放在你想放置 Flutter SDK 的路径中
例如E:\Program Files\flutter\flutter_windows_2.10.4-stable
flutter6open in new window

或者你可以直接git clone Flutter仓库直接下载发行版

$>git clone https://github.com/flutter/flutter.git -b stable

配置环境变量

开始菜单搜索功能,搜索env,选择编辑环境变量 flutter7open in new window 系统变量中找到“path”,编辑,加入 flutter\bin 目录的完整路径
flutter8open in new windowflutter9open in new window 配置好环境变量后,就可以通过命令提示符访问了

$>where flutter dart
flutter10open in new window
flutter10

更多关于Flutter的安装配置请访问官方网站

flutter-installopen in new window


好了,前面的弄好了之后,开始正式使用Flutter吧!

使用

New Flutter Project

创建flutter项目
flutter11open in new window 选中Flutter,在Flutter SDK path中添加刚刚下载Flutter SDK放置的目录,记住一定是\flutter根目录 flutter12open in new windowflutter13open in new window 新建项目会导入依赖,耐心等待 flutter14open in new window

Run Flutter App

直接使用创建flutter的模板run! flutter15open in new window 运行Flutter,报错了,不过不要着急,遇到问题不要慌,上网一搜总能找到答案 flutter16open in new window 报错的问题是:

Finished with error: Failed to bind web development server:
SocketException: Failed to create server socket (OS Error: Failed to start accept), address = localhost, port = 51321

应该是需要配置“Additional run args”参数 flutter17open in new windowflutter18open in new window

Additional run args:--web-port=8080 --web-hostname=127.0.0.1

再次运行,成功!
成功了open in new window web端还是响应式布局,满足任何设备浏览器访问 响应1open in new window响应2open in new window

热部署(hot reload)

lib/main.dart中修改字符串 hotreload1open in new windowhotreload2open in new window 非常快的热部署到应用上! 查看1open in new window

编写第一个Flutter应用

根据官网open in new window的教程编写一个英语单词滚动视图APP 将下面这段代码覆盖到lib/main.dart中,

// Copyright 2018 The Flutter team. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Welcome to Flutter',
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Welcome to Flutter'),
        ),
        body: const Center(
          child: Text('Hello World'),
        ),
      ),
    );
  }
}

创建Material Design风格的应用。在 pubspec.yaml 文件的 flutter 部分选择加入uses-material-design: true会是一个明智之举,通过这个可以让您使用更多 Material 的特性,比如其预定义好的图标集。
flutter19open in new window

TO Be Continue...