本文示例开发环境: Mac 11.2.3 / Android Studio 4.1.2
教程相关:
官网: https://flutter.dev/
Flutter中文网: https://flutterchina.club/
GitHub: https://github.com/flutter/flutter
bilibili视频教程: https://www.bilibili.com/video/BV1S4411E7LY?p=25&t=5
在 macOS 上安装和配置 Flutter 开发环境
https://flutterchina.club/get-started/install/
第一个App
code
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
编译为本机
Flutter应用程序是用单个代码库 (Dart) 开发的,然后框架将代码库编译为本地iOS或Android。在目标平台中,仅代码的UI组件会转换为等效的本机UI组件。
我们在上面例子中demo1的根目录下, 会找到对应的Android和iOS工程:
使用 Android Studio 或者 Xcode 能分别运行工程并生成对应的 Android/iOS App.