Flutter事例源代碼分析

import 'package:flutter/material.dart';//引用庫

void main() => runApp(new MyApp());//應(yīng)用入口,表示啟動應(yīng)用

//應(yīng)用結(jié)構(gòu)如下
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Flutter Demo',//應(yīng)用名稱
      theme: new ThemeData(
        primarySwatch: Colors.blue,//應(yīng)用主題
      ),
      home: new MyHomePage(title: 'Flutter Demo Home Page'),//首頁路由,即主界面。其中home也是一個weight
    );
  }
}

//首頁代碼
class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;

  @override
  _MyHomePageState createState() => new _MyHomePageState();//產(chǎn)生對應(yīng)的狀態(tài)類
}

//狀態(tài)類如下
class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;//狀態(tài),相當于變量聲明

//設(shè)置的函數(shù)
  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

//構(gòu)建UI界面代碼如下
 @override
  Widget build(BuildContext context) {
    return new Scaffold(//其中Scaffold是Material庫提供的一個weight,包含了許多常用的控件。
      appBar: new AppBar(
        title: new Text(widget.title),
      ),
      body: new Center(
        child: new Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            new Text(
              'You have pushed the button this many times:',
            ),
            new Text(
              '$_counter',
              style: Theme.of(context).textTheme.display1,
            ),
          ],
        ),
      ),
      floatingActionButton: new FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: new Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容