Flutter入门学习(二)第一个Flutter应用

2023-11-02

Flutter 开发环境搭建好之后,创建第一个Flutter应用

使用VSCode来创建第一个Flutter应用

打开 VSCode 后,Cmd + Shift + p, 选择 Flutter: New Project 即可创建。

如下图:
请添加图片描述
如果右下角报找不到 Flutter SDK 的话需要配置一下 Flutter 的路径

如下图:

请添加图片描述
在配置环境的时候我们已经下载了 Flutter SDK,选择 Locate SDK,然后选择下载好的 Flutter 路径就好了。

创建好的项目如下:

请添加图片描述
运行的话使用 VSCode 的 运行就可以了,如下图:
请添加图片描述
Flutter入门学习,第一个Flutter应用就运行起来了,点击加号,会走VSCode里的方法,可以打断点查看。

代码分析

import 'package:flutter/material.dart';

import并不陌生,一般就是在导库或者文件之类的

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

应用程序的 main 函数,mian 函数里面有一个方法,runApp()

函数 const MyApp() 作为 runApp 的函数参数,调用runApp,继续会执行到 MyApp() 这个函数。

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

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        // This is the theme of your application.
        //
        // Try running your application with "flutter run". You'll see the
        // application has a blue toolbar. Then, without quitting the app, try
        // changing the primarySwatch below to Colors.green and then invoke
        // "hot reload" (press "r" in the console where you ran "flutter run",
        // or simply save your changes to "hot reload" in a Flutter IDE).
        // Notice that the counter didn't reset back to zero; the application
        // is not restarted.
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyApp 我们可以看到,MyApp 是定义的一个类。原来const MyApp()是一个初始化函数,返回一个 const 类型的对象 app,然后 main 函数将这个app对象给 run 起来了,就有了我们的应用程序,通俗理解就是这样的。

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

虽然不懂dart语言,但是从写法上我们可以猜,这是一个初始化函数

@override
Widget build(BuildContext context) 

从这个override我们可以看到,这个函数应该是系统函数,复写了而已。

  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        // This is the theme of your application.
        //
        // Try running your application with "flutter run". You'll see the
        // application has a blue toolbar. Then, without quitting the app, try
        // changing the primarySwatch below to Colors.green and then invoke
        // "hot reload" (press "r" in the console where you ran "flutter run",
        // or simply save your changes to "hot reload" in a Flutter IDE).
        // Notice that the counter didn't reset back to zero; the application
        // is not restarted.
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }

这个函数直接返回了一个 MaterialApp 对象,这个对象的一些属性,例如 title,theme,home都在下面进行了一些设置。这种函数式编程,代码看起来还是挺舒适的。

主要的来看下home这里

home: const MyHomePage(title: 'Flutter Demo Home Page')

MyHomePage() 这又是一个初始化函数,MyHomePage是一个类

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);

  // This widget is the home page of your application. It is stateful, meaning
  // that it has a State object (defined below) that contains fields that affect
  // how it looks.

  // This class is the configuration for the state. It holds the values (in this
  // case the title) provided by the parent (in this case the App widget) and
  // used by the build method of the State. Fields in a Widget subclass are
  // always marked "final".

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

主要看这里

  @override
  State<MyHomePage> createState() => _MyHomePageState();

createState() 是一个函数,_MyHomePageState() 也是一个函数, _MyHomePageState() 这个函数直接作为 createState() 的函数返回值。

调用 createState() 实际上就执行到了 _MyHomePageState()

最终应用程序页面的构建落脚到了这里

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      // This call to setState tells the Flutter framework that something has
      // changed in this State, which causes it to rerun the build method below
      // so that the display can reflect the updated values. If we changed
      // _counter without calling setState(), then the build method would not be
      // called again, and so nothing would appear to happen.
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    // This method is rerun every time setState is called, for instance as done
    // by the _incrementCounter method above.
    //
    // The Flutter framework has been optimized to make rerunning build methods
    // fast, so that you can just rebuild anything that needs updating rather
    // than having to individually change instances of widgets.
    return Scaffold(
      appBar: AppBar(
        // Here we take the value from the MyHomePage object that was created by
        // the App.build method, and use it to set our appbar title.
        title: Text(widget.title),
      ),
      body: Center(
        // Center is a layout widget. It takes a single child and positions it
        // in the middle of the parent.
        child: Column(
          // Column is also a layout widget. It takes a list of children and
          // arranges them vertically. By default, it sizes itself to fit its
          // children horizontally, and tries to be as tall as its parent.
          //
          // Invoke "debug painting" (press "p" in the console, choose the
          // "Toggle Debug Paint" action from the Flutter Inspector in Android
          // Studio, or the "Toggle Debug Paint" command in Visual Studio Code)
          // to see the wireframe for each widget.
          //
          // Column has various properties to control how it sizes itself and
          // how it positions its children. Here we use mainAxisAlignment to
          // center the children vertically; the main axis here is the vertical
          // axis because Columns are vertical (the cross axis would be
          // horizontal).
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headline4,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}

里面定义了一个全局变量 _counter

定义了一个函数 _incrementCounter()

复写了 Widget build(BuildContext context) 方法,这个方法中返回了一个Scaffold 对象

这个 Scaffold 包含 appBarbody

还在 build 这个函数里添加了一个按钮 floatingActionButton

floatingActionButton: FloatingActionButton(
    onPressed: _incrementCounter,
    tooltip: 'Increment',
    child: const Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.

按钮点击的方法函数即 _incrementCounter()

整体上代码是从上依次往下进行执行,每一处都不是多余的,而且很合理

mian 函数里运行了一个app对象,app里有一个 home 对象,即app 的主页,homePage中初始化了主页的模块,里面是一个Scaffold的对象,这个对象有appBar,有body,导航栏有内容,有一个按钮,可以点击。

总之应用程序的main函数运行了一个app,app中构建了一个主页,主页上添加了一些内容。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Flutter入门学习(二)第一个Flutter应用 的相关文章

随机推荐

  • 区块链二级知识考试

    区块链基础知识二级考试 考试时间30分钟 总分100分 请认真作答 出题人及监考老师 高志豪 请转载者注明 谢谢支持 一 单选题 每题5分 共30分 1 中本聪是哪里人 A 中国人 B 美国人 C 日本人 D 不确定 2 下面哪种共识机制效
  • WPF DataGrid 导出Excel

    region Excel导出 private void btnExportExcel Click object sender RoutedEventArgs e Export this dgvList XX信息查询列表 public voi
  • STM32 F1,F4,CAN多字节发送和接收

    一 简介 CAN的基础知识在这里不做过多介绍 其他网站上讲解的很基础 因为CAN一次性只能接收1字节8位 所以在这里只介绍怎样让CAN能像串口那样一次性接收非常多的位 亲测有效 具体先看效果图 在这里我的实现是通过两块STM32板子 可以是
  • 【mac】mac鼠标指针跟随很慢的问题

    使用时感觉鼠标指针跟随太慢 在系统偏好设置里面将鼠标跟随速度调到最大 还是感觉很慢 后来在网上找到了一个通过命令行改全局配置的方式调快跟随速度 具体方法如下 可以先查看一下当前值 打开终端 输入命令 lcc localhost defaul
  • html的实体字符,h5展示特殊符号<>

    前言 在 HTML 中 某些字符是预留的 不能使用小于号 lt 和大于号 gt 这是因为浏览器会误认为它们是标签 比如 这样是不行的 p lt p 比如用实体字符 p lt p HTML 中有用的字符实体 注释 实体名称对大小写敏感 显示结
  • 单链表的创建、单链表的删除、单链表的插入(数据结构)

    1 创建一个超级简单的单链表 include
  • 用HttpClient抓取人人网高校数据库(省,高校,院系三级级联)--更新1

    更新备注 将src文件改成了一个完整的项目 解压后可以直接导入到Eclipse中去 省去大家配置 项目乱码请改项目属性为GBK 另外 如果你要登陆人人网 的话 需要申请一个人人网账号 这里提供公用的 lei d0809 gmail com
  • matlab画三维、二维动态曲线

    matlab画三维 二维动态曲线 画三维曲线动图 xlabel X m ylabel Y m zlabel Z m grid on for i 1 length x 1 axis 0 05 2 5 0 05 5 0 1 0 1 line x
  • Matlab—频谱分析作图

    clf fs 50 采样频率 每秒钟采样多少个点 N 60 采样点数量 T N fs 采样时间 n 0 N 1 t n fs 时间序列 f n fs N 频率序列 y1 10 sin 2 pi 15 t y2 10 sin 2 pi 20
  • 硬件第二节 MOS管电路工作原理及详解

    文章目录 一 MOS管画法辨认 1 1 辨认MOS管 二 MOS管使用 2 1 作为开关管 2 1 1 导通条件 2 1 2实例 三 如何选择MOS管 3 1 MOS管需要注意的几个参数 3 1 1 选择PMOS还是NMOS 3 1 2 电
  • Proxmox VE(PVE) 进行网卡直通

    文章目录 我的设备 介绍 添加CPU支持 开启iommu 查询网卡信息 Intel CPU AMD CPU 新增所需模块 添加PCI设备 命令模式添加 web页面模式添加 验证IOMMU有效 IOMMU中断重映射 查看中断重映射 启用中断重
  • 用函数输出星星

    2013 11 10 11 54 0人阅读 评论 0 收藏 编辑 删除 01 02 程序的版权和版本声明部分 03 Copyright c 2013 烟台大学计算机学院 04 All rights reserved 05 文件名称 test
  • 静态测试和动态测试

    静态测试 不运行被测试的软件系统 而是采用其他手段和技术对被测试软件进行检测的一种测试技术 代码走读 文档评审 程序分析等 静态测试常用技术 静态分析技术 1 定义 一种不通过执行程序而分析程序执行的技术 2 功能 检查软件的表示和描述是否
  • flask获取post参数_Flask教程2:模板

    什么是模板 模板负责定义页面的显示样式 与应用的逻辑相互独立 在Flask中 模板放在templates文件夹 是单独的html文件 编写一个模板 在app文件夹内创建templates文件夹 并新建index html文件 用来显示用户的
  • sqlilabs第26a

    sqlilabs第26a 一 手注 有错误希望师傅们指出 一 手注 直接看源码 无回显 我使用boolean盲注 过滤了and 空格 注释 空格可以通过 或者 0a绕过 and可以用 或者双写绕过 但这道题 不行 注释使用 1 1闭合 判断
  • Apache Druid远程代码执行漏洞复现(CVE-2021-25646)

    Apache Druid远程代码执行漏洞复现 CVE 2021 25646 漏洞描述 Apache Druid包括执行用户提供的JavaScript的功能嵌入在各种类型请求中的代码 此功能在用于高信任度环境中 默认已被禁用 但是 在Drui
  • 39天前端入门教程,免费领!!还送原创书籍+限量鼠标垫

    39天前端入门教程课程内容 福利 课程包含完整视频 笔记 源码 开发工具 39天前端入门教程 免费领 还送原创书籍 限量鼠标垫 关注 黑马程序员视频库 回复518 即可免费领取哦
  • mysql对姓名、手机号、身份证号做脱敏处理

    SELECT phone手机号脱敏处理 IF phone CONCAT LEFT phone 3 RIGHT phone 4 AS dephone cardno身份证号脱敏处理 IF cardno CONCAT LEFT cardno 3
  • 小甲鱼python视频xxoo爬虫代码改进--煎蛋网

    2020 7 31 今天学习得是关于小甲鱼得python课程 根据这个课程也确确实实得学到了不少东西 所以希望大家也可以一起去学习 下面是我在小甲鱼上课改造之后得代码 这个课程是在b站上看的 号码是 av27789609 这个是第五十节左右
  • Flutter入门学习(二)第一个Flutter应用

    Flutter 开发环境搭建好之后 创建第一个Flutter应用 使用VSCode来创建第一个Flutter应用 打开 VSCode 后 Cmd Shift p 选择 Flutter New Project 即可创建 如下图 如果右下角报找