flutter下hello world

import 'package:flutter/material.dart';
void main() => runApp(MyApp());

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

Dart是面向对象的语言,即使是函数也是对象,并且属于Function类型的对象。这意味着函数可以分配给变量或作为参数传递给其他函数。当然你也可以像JavaScript一样,调用一个函数。

比如,

void main() =>runApp(MyApp());

就是一个函数。因为这个函数体里只有一行代码,所以可以直接使用=>来省略{},只有函数体里只有一行时,才可以使用,否则请使用大括号。

(ps:学习Dart语法时你要记住一条,Dart里一切皆对象,包括数字和函数)

StatefulWidget和StatelessWidget

  • StatefulWidget : 具有可变状态的窗口部件,也就是你在使用应用的时候就可以随时变化,比如我们常见的进度条,随着进度不断变化。
  • StatelessWidget:不可变状态窗口部件,也就是你在使用时不可以改变,比如固定的文字(写上后就在那里了,死也不会变了)。

VSCode中如何热加载

用VSCode编写Flutter不好的一点就是要手动加载更新应用,个人感觉这至少会降低10%的工作效率。

当我们运行flutter run以后,会有一段红色文字的提示,说明了我们可以作的事情。

To hot reload changes while running, press "r". To hot restart (a
nd rebuild state), press "R".
An Observatory debugger and profiler on Android SDK built for x86 is
available at: http://127.0.0.1:64590/
You can dump the widget hierarchy of the app (debugDumpApp) by pressing "w".
To dump the rendering tree of the app (debugDumpRenderTree), press "t".
For layers (debugDumpLayerTree), use "L"; for accessibility (debugDumpSemantics), use "S" (for traversal order) or "U" (for inverse hit test order).
To toggle the widget inspector (WidgetsApp.showWidgetInspectorOverride), press "i".
To toggle the display of construction lines (debugPaintSizeEnabled),
press "p".
To simulate different operating systems, (defaultTargetPlatform), press "o".
To display the performance overlay (WidgetsApp.showPerformanceOverlay), press "P".
To save a screenshot to flutter.png, press "s".
To repeat this help message, press "h". To detach, press "d"; to quit, press "q".

  

我们来看几个重点的:

  • r 键:点击后热加载,也就算是重新加载吧。
  • p 键:显示网格,这个可以很好的掌握布局情况,工作中很有用。
  • o 键:切换android和ios的预览模式。
  • q 键:退出调试预览模式。

如果你觉的这太麻烦了,可以开启Debug模式,这时就可以实现真正的热加载了(我们保存,效果立即就会改变),但有时报错也挺烦人的。

posted @ 2020-09-11 18:14  江离白芷  阅读(162)  评论(0)    收藏  举报