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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM