Flutter事例源代码分析

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

void main() => runApp(new MyApp());//应用入口,表示启动应用

//应用结构如下
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Flutter Demo',//应用名称
      theme: new ThemeData(
        primarySwatch: Colors.blue,//应用主题
      ),
      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();//产生对应的状态类
}

//状态类如下
class _MyHomePageState extends State {
  int _counter = 0;//状态,相当于变量声明

//设置的函数
  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

//构建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: [
            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.
    );
  }
}

你可能感兴趣的:(Flutter事例源代码分析)