Flutter入門到實戰之第一個Flutter例子
阿新 • • 發佈:2019-01-04
接著上一篇Flutter入門配置環境成功後,進行了第一個例子的實戰,步驟如下:
1.開啟Android Studio,選擇File——New——New Flutter Project,建立Flutter專案
2. 選擇Flutter Application——Next,點選Next之後填寫建立專案的相關資訊
3.依次填寫專案名、Flutter sdk path(Flutter sdk路徑)、Project location(專案存放位置工作空間)、Description(專案簡單描述)
4.輸入包名後,點選Finish,進行專案構建階段 ,此時需等待一段時間.
5.專案建立完成之後的目錄如下:
6.基本設定:在main.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 輔導', theme: ThemeData( primarySwatch: Colors.blue, ), home: MyHomePage(title: 'flutter例項!'),//設定標題文字 ); } } 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( '你好,flutter',//設定內容 ), Text( '$_counter', style: Theme.of(context).textTheme.display1, ), ], ), ), floatingActionButton: FloatingActionButton( onPressed: _incrementCounter, tooltip: 'Increment', child: Icon(Icons.add),//按鈕 ), // This trailing comma makes auto-formatting nicer for build methods. ); } }
7.執行出來介面截圖如下:
這樣一個簡單的入門小例子就完成了,後面會慢慢加上其他功能和效果,新手上路,如有問題,敬請諒解!