為您解碼網(wǎng)站建設(shè)的點(diǎn)點(diǎn)滴滴
發(fā)表日期:2018-09 文章編輯:小燈 瀏覽次數(shù):2040
歡迎關(guān)注我的公眾號(hào)
首先推薦兩篇文章,寫的非常的詳細(xì),從中可以了解到原理,寫的非常的詳細(xì)!
Demo 的下載地址(正式包,使用Flutter命令打的正式包)
App詳情
HomePage
;里面嵌套了四個(gè)頁面,使用的是TabBar
和 TabBarView
的組合,比如安卓中的Fragment
Viewpager
flutter:sdk: flutter# The following adds the Cupertino Icons font to your application.# Use with the CupertinoIcons class for iOS style icons.cupertino_icons: ^0.1.0fluttertoast: ^2.0.7 #"Packages get" 要去主動(dòng)的 get 一次依賴 dio: ^v1.0.3# 添加網(wǎng)絡(luò)依賴
* 首頁的關(guān)鍵代碼 ```//為給定的[子]控件創(chuàng)建默認(rèn)選項(xiàng)卡控制器。 return new DefaultTabController( length: 5, child: new Scaffold( appBar: new AppBar( backgroundColor: Colors.black45,// title: titleWidget(), title: new Text("首頁",style: new TextStyle(color: Colors.white,fontSize: 22.00),), actions: <Widget>[ new IconButton( icon: new Icon(Icons.add_a_photo), onPressed: () { Navigator .of(context) .push(new MaterialPageRoute(builder: (context) { return new OtherPage(); })); }) ], bottom: new TabBar( isScrollable: true, labelStyle: new TextStyle(fontSize: 22.00,color: Colors.red), indicatorPadding:EdgeInsets.zero, labelColor: Colors.white, indicatorWeight:4.0, unselectedLabelColor: Colors.blueAccent, tabs: [ new Tab( text: "豆瓣電影", ), new Tab( text: "控件擺放", ), new Tab( text: "列表展示", ), new Tab( text: "其他控件展示", ), ]), ), body: new TabBarView(children: [new TabOne(), new TabTwo(),new TabThree(),new TabFroth()]), )); ```
TextField
相當(dāng)于安卓中的Edittext
,只不過獲取值的時(shí)候有些變化 new Expanded(child: new TextField(//不要主動(dòng)彈起來autofocus: false,controller: _textController,decoration: new InputDecoration.collapsed(hintText: "請(qǐng)輸入要查找的詞",hintStyle: new TextStyle(color: Colors.red)),),
Navigator.of(context).pop();
Toast
,這里我是使用了三方的依賴!底層原理是使用了反射,具體實(shí)現(xiàn)的方法,有興趣的同學(xué)可以看看String res = await _channel.invokeMethod('showToast', params);
Fluttertoast.showToast( msg: "輸入為空,請(qǐng)重新輸入", timeInSecForIos: 1, bgcolor: "#e74c3c", textcolor: '#ffffff');
import 'dart:convert'; import 'dart:io'; import 'package:flutter/material.dart'; import 'package:flutter_app/bean/DataBean.dart'; import 'package:fluttertoast/fluttertoast.dart';class SimilarWordsPage extends StatefulWidget { @override State<StatefulWidget> createState() { return new SimilarWordsPageState(); } }class SimilarWordsPageState extends State<SimilarWordsPage> { List<DataBean> datas = []; static int i=0; final TextEditingController _textController = new TextEditingController();@override Widget build(BuildContext context) { return new Scaffold( appBar: findAppBar(), backgroundColor: Colors.black12, body: findBody(), ); }findBody() { return new Container( child: new Scaffold( body: new ListView.builder( itemCount: datas.length, itemBuilder: (BuildContext context, int position) { i=position; return getRow(position); }, ), )); }Widget findAppBar() { return new AppBar( title: new Container( child: new Row( children: <Widget>[ new Container( child: new FlatButton.icon( onPressed: () { // 本來就在棧頂,退出會(huì)有顯示的問題 Navigator.of(context).pop(); }, icon: new Icon(Icons.close, color: Colors.white30), label: new Text(""), ), width: 60.0, ), new Expanded( child: new TextField( //不要主動(dòng)彈起來 autofocus: false, controller: _textController, decoration: new InputDecoration.collapsed( hintText: "請(qǐng)輸入要查找的詞", hintStyle: new TextStyle(color: Colors.red)), ), ), //點(diǎn)擊事件的第一種實(shí)現(xiàn)的方式我覺得不太好 //new GestureDetector(child: new Icon(Icons.find_in_page),onTap: (){print("dd");}) // 這種點(diǎn)擊時(shí)間有點(diǎn)效果 new IconButton( icon: new Icon(Icons.find_in_page), onPressed: () { print(_textController.text); if (_textController.text.isEmpty) { Fluttertoast.showToast( msg: "輸入為空,請(qǐng)重新輸入", timeInSecForIos: 1, bgcolor: "#e74c3c", textcolor: '#ffffff'); } else { FocusNode focusNode = new FocusNode(); FocusScope.of(context).requestFocus(new FocusNode()); Fluttertoast.showToast( msg: "查找值為:" + _textController.text, timeInSecForIos: 1, bgcolor: "#e74c3c", textcolor: '#ffffff'); getApiData(_textController.text); focusNode.unfocus(); } }) ], ), decoration: new BoxDecoration( borderRadius: const BorderRadius.all(const Radius.circular(4.0)), color: Colors.white10), )); }Widget getRow(int i) { return new Padding( padding: new EdgeInsets.all(10.0), // child: new Text("Row ${datas[i].key}",style: new TextStyle(color: Colors.orange,fontSize: 18.00),) //Column 相當(dāng)于 相對(duì)布局Row 線性布局 child: new Column( children: <Widget>[ new Padding( padding: new EdgeInsets.fromLTRB(0.0, 5.0, 0.0, 5.0), child: new Row( children: <Widget>[ new Expanded( child: new OutlineButton( borderSide:new BorderSide(color: Theme.of(context).primaryColor), child: new Text('條目 = '+i.toString(),style: new TextStyle(color: Theme.of(context).primaryColor),), onPressed: (){}, ) ), ], ), ),new Container( child: new Text( "聯(lián)想到的詞:" + datas[i].key, style: new TextStyle(color: Colors.purple, fontSize: 12.00), ), padding: new EdgeInsets.all(10.0), ), new Container( child: new Text("聯(lián)想到詞的翻譯信息:" + datas[i].message, style: new TextStyle(color: Colors.cyan, fontSize: 15.00)), padding: new EdgeInsets.all(10.0), ) ], ), ); }@override void initState() { super.initState(); // 網(wǎng)絡(luò)請(qǐng)求 //http://dict-mobile.iciba.com/interface/index.php?c=word&m=getsuggest&nums=10&client=6&is_need_mean=1&word=sm //我的 Api的地址 getApiData("sm"); }// 網(wǎng)絡(luò)請(qǐng)求 void getApiData(String tag) async { // 注意導(dǎo)入的包的地方是import 'dart:io'; var httpClient = new HttpClient(); var url = "http://dict-mobile.iciba.com/interface/index.php?c=word&m=getsuggest&nums=20&client=6&is_need_mean=1&word=" + tag; var request = await httpClient.getUrl(Uri.parse(url)); var response = await request.close(); if (response.statusCode == HttpStatus.OK) { var jsonData = await response.transform(utf8.decoder).join(); setState(() { datas = DataBean.decodeData(jsonData); }); for (int i = 0; i < datas.length; i++) { print(datas[i].key); print(datas[i].message); } } } }
widget
相當(dāng)于View
,Widget
的實(shí)例僅僅存在每一幀之間,并且每一幀之間 Flutter
都會(huì)主動(dòng)的創(chuàng)建一顆Widget
樹用于下一幀的渲染。Android
中 View
是可變的,在 Flutter
中的 Widget
是不可變的。這種特性使得 Flutter
中的 Widget
變得十分輕量級(jí)Widget
會(huì)變化,那么它就是有狀態(tài)的。但是如果一個(gè)子Widget
是有狀態(tài)的,但是其父Widget
是不可變的話父Widget
也可以是 StatelessWidget
。TatelessWidget
和StatefulWidget
的核心內(nèi)容是一致的,它們都會(huì)在每一幀中被重構(gòu),不同之處在于StatefulWidget
有一個(gè) State
對(duì)象,它可以為 StatefulWidget
在不同幀之間存儲(chǔ)數(shù)據(jù)。Flutter
中UI
的布局是通過在dart
文件中構(gòu)建 Widget
樹來實(shí)現(xiàn)的。Android
中,使用 LinearLayout
使你的部件垂直或水平放置。在 Flutter
中,你可以使用Row
或者 Co??lumn
來實(shí)現(xiàn)相同的效果。Flutter
中,最簡單的方法是使用ListView
。在Flutter
中,ListView
既是ScrollView
又是Android
中的ListView
。Column
,Row
和Stack
等 Widget
的組合來實(shí)現(xiàn) RelativeLayout
的效果Flutter
中,添加觸摸監(jiān)聽器有兩種方法Widget
支持事件檢測,則可以將一個(gè)函數(shù)傳遞給它并進(jìn)行處理。例如,RaisedButton
有一個(gè)onPressed
參數(shù)Widget
不支持事件檢測,則可以將該 Widget
包裝到 GestureDetector
中,并將函數(shù)傳遞給onTap
參數(shù)。GestureDetector
我們可以監(jiān)聽廣泛的手勢Material
風(fēng)格的組件的話,可以把頂級(jí)部件 MaterialApp
作為應(yīng)用程序的入口。MaterialApp
作為一個(gè)比較方便的部件,包裝了許多實(shí)現(xiàn)了 Material
風(fēng)格所需要的部件(如 Scaffold
)。MaterialApp
是在 WidgetsApp
的基礎(chǔ)上進(jìn)行實(shí)現(xiàn)的Flutter
不會(huì)自動(dòng)導(dǎo)入包Column
相當(dāng)于 相對(duì)布局Row
線性布局HttpClient
導(dǎo)入的包是io
里面的DEBUG
包要不正式包大很多 Built build\app\outputs\apk\debug\app-debug.apk (31.9MB).
而正式包才8.4M
.Bug
的時(shí)候太痛苦了,Flutter
使用 ide
,太痛苦了MaterialApp
帶有 Debug
的標(biāo)記