Flutter入门 (一) 环境搭建 即新建第一个小项目

 

为什么要用Flutter

移动到比较普遍的跨平台方案主要是基于WebView(Condova), 基于JS(RN,Week),Flutter,

一般都拿RN跟Flutter ,RN毕竟时间比较久  三方库什么比较丰富,Flutter的唯一缺点可能就是三方库的没那么丰富

 但常用的库都比较齐全, 最关键的一点就是谷歌亲儿子,也意味的在安卓的上支持会不遗余力。

 https://pub.dartlang.org/flutter/  这里可以查看 Flutter支持的第三方库

环境搭建(Windows平台)

1.下载  从https://flutter.io/docs/get-started/install 选择自己系统的安装包

2.添加环境变量  flutter\bin 添加到环境变量中

3.在控制台输入 flutter docter 

flutter docter 用来检测 当前环境  所有选项都打勾的情况就说明已经配置成功

 

4. 安装AS 配置AS插件

 

然后回到Plugin 里面 勾选 Dart , Android APK Support, Flutter  重启AS

 

新建第一个项目

File  ---- New ---- New Flutter Project ---Flutter Project

原生安卓 有 Activity  Fragment   各种View   Flutter 你可以把所有可视化的都理解为Widget

首先看下默认的工程  切换的Project试图

 

Flutter 的主要配置都在pubspec.yaml

Yam是一种缩进格式的配置方式,要比较注意空格   

切换到安卓试图 看下main.dart的内容

void main() => runApp(MyApp());  主函数 可以理解为安卓的Application, 不过Flutter 所有界面都是widget
class MyApp extends StatelessWidget {
// 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, //类似安卓中ActionBar主题
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}

class MyHomePage extends StatefulWidget {
MyHomePage({Key key, 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
_MyHomePageState createState() => _MyHomePageState();
}

这两个类 不用这么关注  主要逻辑都是在 _MyHomePageState

 

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

  void _incrementCounter() { //dar中的方法命名
    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  //这个可以理解为安卓中的oncreate方法了
  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(  //ActionBar的状态
        // 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( //可以理解为我们的SetContentView内容
        // Center is a layout widget. It takes a single child and positions it
        // in the middle of the parent.
        child: Column(
          // Column is also 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>[
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.display1,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton( //Flutter中的悬浮按钮  事件可以直接绑定
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}

 

 运行下第一个程序  在模拟器上运行需要  选中 Hardware - GLES 2.0

 

posted @ 2018-12-13 19:12  dikeboyR  阅读(3865)  评论(0编辑  收藏  举报