為您解碼網(wǎng)站建設(shè)的點點滴滴
發(fā)表日期:2018-10 文章編輯:小燈 瀏覽次數(shù):1652
有狀態(tài)widget:StatefulWidget和無狀態(tài)widget:StatelessWidget 前者不需要實現(xiàn)Widget build(BuildContext context)。
具體的選擇取決于widget是否需要管理一些狀態(tài)
在Dart語言中使用下劃線前綴標(biāo)識符,會強制其變成私有的。
Icons.favorite Icons類里面有很多默認(rèn)圖標(biāo)
isOdd 是否奇數(shù) 2.isOdd -> false 1.isOdd -> true
pushSaved “”開頭的自動轉(zhuǎn)成私有(方法和變量)
導(dǎo)航欄添加按鈕和事件
@overrideWidget build(BuildContext context) {return new Scaffold(appBar: new AppBar(title: new Text('Startup Name Generator'),actions: <Widget>[// AppBar 添加一個按鈕 樣式為list 事件是_pushSavednew IconButton(icon: new Icon(Icons.list), onPressed: _pushSaved)],),body: _buildSuggestions(),);}// tooltip 長時間按下的提示文字IconButton(icon: new Icon(Icons.search), tooltip: 'Search', onPressed: null)
Navigator.of(context).push(new MaterialPageRoute(builder: (context) {},),);
// 多行void main() {runApp(new Center(child: new Text('Hello, world!',textDirection: TextDirection.ltr,),))}// 一行void main() => runApp(new MyApp());
// Material 是UI呈現(xiàn)的“一張紙”
請確保在pubspec.yaml文件中,將flutter的值設(shè)置為:uses-material-design: true。這允許我們可以使用一組預(yù)定義Material icons。
Row(橫向排列)和Column(縱向排列)
child: new Row(children: <Widget>[new ...,new ...,new ...,],)
child: new Column(children: <Widget>[new ...,new ...,new ...,],),
cached_network_image 圖片占位和淡入淡出
push
Navigator.push(context,new MaterialPageRoute(builder: (context) => new 新界面),);// 如果需要傳值:新界面({Key key, @required this.接收字段的名字}) : super(key: key);popNavigator.pop(context);
dio網(wǎng)絡(luò)請求https://github.com/flutterchina/dio
import 'dart:convert'; // package將響應(yīng)內(nèi)容轉(zhuǎn)化為一個json Map
// 使用fromJson工廠函數(shù),將json Map 轉(zhuǎn)化為一個Post對象
new Post.fromJson(json);
future參數(shù)是一個異步的網(wǎng)絡(luò)請求
import 'dart:io'; // 添加請求的headers
// 長連接
import 'package:web_socket_channel/io.dart';import 'package:multi_server_socket/multi_server_socket.dart';
Future<Post> fetchPost() async {final response = await http.get('[http://jsonplaceholder.typicode.com/posts/1](http://jsonplaceholder.typicode.com/posts/1)');final responseJson = json.decode(response.body);return new Post.fromJson(responseJson);}// 請求添加headers/*Future<Post> fetchPost() async {final response = await http.get('[https://jsonplaceholder.typicode.com/posts/1](https://jsonplaceholder.typicode.com/posts/1)',headers: {HttpHeaders.AUTHORIZATION: "Basic your_api_token_here"},);final json = jsonDecode(response.body);return new Post.fromJson(json);}*/new FutureBuilder<Post>(future: fetchPost(),builder: (context, snapshot) {return new CircularProgressIndicator();})
// 連接長連接IOWebSocketChannel.connect('[ws://echo](ws://echo/).[websocket.org](http://websocket.org/)’)// 接收消息new StreamBuilder(stream: widget.channel.stream,builder: (context, snapshot) {return new Padding(child: new Text(snapshot.hasData ? '${snapshot.data}' : ''),padding: const EdgeInsets.symmetric(vertical: 20.0));})// 發(fā)送消息widget.channel.sink.add(_textController.text);// 關(guān)閉長連接widget.channel.sink.close();
https://flutterchina.club/assets-and-images/
Container添加 padding, margins, borders, background color, 或?qū)⑵渌b飾添加到widget.GridView將 widgets 排列為可滾動的網(wǎng)格.ListView將widget排列為可滾動列表Stack將widget重疊在另一個widget之上.Material Components:Card將相關(guān)內(nèi)容放到帶圓角和投影的盒子中。ListTile將最多3行文字,以及可選的行前和和行尾的圖標(biāo)排成一行
-asset 路徑是與pubspec.yaml平級的文件路徑flutter:# Include the Material Design fonts.uses-material-design: truefonts:- family: Rock Saltfonts:# [https://fonts.google.com/specimen/Rock+Salt](https://fonts.google.com/specimen/Rock+Salt)- asset: fonts/Arial-Unicode.ttf- family: VT323fonts:# [https://fonts.google.com/specimen/VT323](https://fonts.google.com/specimen/VT323)- asset: fonts/Arial-Unicode.ttf- family: Ewertfonts:# [https://fonts.google.com/specimen/Ewert](https://fonts.google.com/specimen/Ewert)- asset: fonts/Ewert-Regular.ttf
new Row(mainAxisAlignment: MainAxisAlignment.end, children: <Widget>[new FlatButton(onPressed: () {}, child: Icon(Icons.close))],);
new Divider(color: Colors.lightBlue,)
new Image.asset(“圖片路徑", width: 20.0, height: 20.0,)
001、new Padding(padding: new EdgeInsets.fromLTRB(48.0, 20.0, 48.0, 20.0),child: new Row(children: <Widget>[new Expanded(child:new RaisedButton(onPressed: (){},//設(shè)置控件的高度child: new Padding(padding: new EdgeInsets.fromLTRB(0.0, 10.0, 0.0, 10.0),child: new Text("登錄",style: TextStyle(color: Colors.white)),),color: Colors.brown,),),],),),002、new Container(width: MediaQuery.of(context).size.width - 48 * 2 ,padding: new EdgeInsets.only(top: 40.0),child: new RaisedButton(onPressed: (){},//設(shè)置控件的高度child: new Padding(padding: new EdgeInsets.fromLTRB(0.0, 10.0, 0.0, 10.0),child: new Text("登錄",style: TextStyle(color: Colors.white)),),color: Colors.brown,),),003、Widget _bigButton(String text, double lSpace, double rSpace) {return new Container(width: MediaQuery.of(context).size.width - lSpace - rSpace,height: 48.0,margin: new EdgeInsets.only(left: lSpace, right: rSpace),color: Colors.white54,padding: new EdgeInsets.only(top: 0.0),child: new RaisedButton(onPressed: (){print(text);},child: new Padding(padding: new EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 0.0),child: new Text(text,style: TextStyle(color: Colors.white)),),color: Colors.brown,),);}
MediaQuery.of(context).size.width
MediaQuery.of(context).devicePixelRatio
MediaQuery.of(context).padding.top
擔(dān)心鍵盤擋住控件,可以使用 SingleChildScrollView,將SingleChildScrollView當(dāng)做容器。
一個超級簡單界面
import 'package:flutter/material.dart';class RegisterPage extends StatelessWidget {@overrideWidget build(BuildContext context) {return new Scaffold(backgroundColor: Colors.black,body: new RegisterWidget(),);}}class RegisterWidget extends StatefulWidget {RegisterWidgetState createState() => RegisterWidgetState();}class RegisterWidgetState extends State<RegisterWidget> {@overrideWidget build(BuildContext context) {return new Text("RegisterPage", style: TextStyle(color: Colors.white),);}}
· InkWell // 純文字按鈕· OutLineButton // 邊框按鈕· IconButton// icon按鈕·
TextFieldinputFormatters: <TextInputFormatter> [WhitelistingTextInputFormatter.digitsOnly,],
以上已經(jīng)添加
驗證碼按鈕
new Positioned(child: new Container(width: 80.0,height: 27.0,alignment: Alignment.center,decoration: new BoxDecoration(border: new Border.all(color: Colors.white,width: 1.0,),borderRadius: new BorderRadius.circular(4.0),),child: InkWell(child: _mText(_verifyStr, 12.0),onTap: () {},),)),
@overridevoid dispose() {super.dispose();_cancelTimer();}_startTimer() {if (_verifyStr == '重新發(fā)送' || _verifyStr == '獲取驗證碼') {_seconds = 5;_timer = new Timer.periodic(new Duration(seconds: 1), (timer) {if (_seconds == 0) {_cancelTimer();return;}_seconds--;_verifyStr = '$_seconds(s)';setState(() {});if (_seconds == 0) {_verifyStr = '重新發(fā)送';}});}}_cancelTimer() {_timer?.cancel();}
Widget _protocolWidget() {return new Container(child: new Row(children: <Widget>[new GestureDetector(onTap: () {print("選擇");},child: Icon(Icons.add_alert, color: Colors.white),),new Text.rich(new TextSpan(text: '我已閱讀并同意',style: new TextStyle(fontSize: 12.0,color: Colors.grey[500],fontWeight: FontWeight.w400,),children: [new TextSpan(recognizer: new TapGestureRecognizer()..onTap = () {print("《燎原用戶服務(wù)協(xié)議》");},text: "《燎原用戶服務(wù)協(xié)議》",style: new TextStyle(fontSize: 14.0,color: Color(0XFFB57A36),fontWeight: FontWeight.w400,),)])),],));}
new Card(elevation: 4.0,shape: new RoundedRectangleBorder(borderRadius: BorderRadius.only(topLeft: Radius.circular(16.0),topRight: Radius.circular(16.0),bottomLeft: Radius.circular(12.0),bottomRight: Radius.circular(2.0),)),child: new IconButton(icon: Icon(Icons.add), onPressed: () {}),)
import 'package:flutter/material.dart';// with AutomaticKeepAliveClientMixinclass YYTabbarWidget extends StatefulWidget {List<Widget> tabItems = [];Widget title;List<Widget> tabViews = [];PageController pageController;final ValueChanged<int> onPageChanged;final Widget drawer;YYTabbarWidget({Key key,this.drawer,this.tabItems,this.title,this.tabViews,this.pageController,this.onPageChanged,}) : super(key: key);_YYTabbarWidgetState createState() => _YYTabbarWidgetState(drawer, title, tabItems, tabViews, pageController, onPageChanged);}class _YYTabbarWidgetState extends State<YYTabbarWidget> with SingleTickerProviderStateMixin {final Widget _title;final List<Widget> _tabViews;final List<Widget> _tabItems;final ValueChanged<int> _onPageChanged;final Widget _drawer;_YYTabbarWidgetState(this._drawer,this._title,this._tabItems,this._tabViews,this._pageController,this._onPageChanged,) : super();TabController _tabController;PageController _pageController;@overridevoid initState() {super.initState();_tabController = new TabController(length: _tabItems.length, vsync: this);}@overridevoid dispose() {_tabController.dispose();super.dispose();}_renderTab() {print(_tabItems);List<Widget> list = new List();for (int i = 0; i < _tabItems.length; i++) {list.add(new FlatButton(onPressed: () {print(i);_pageController.jumpTo(MediaQuery.of(context).size.width * i);}, child: _tabItems[I],));}return list;}@overrideWidget build(BuildContext context) {return new Scaffold(drawer: _drawer,appBar: new AppBar(title: _title,),body: new PageView(controller: _pageController,children: _tabViews,onPageChanged: (index) {_tabController.animateTo(index);_onPageChanged?.call(index);},),bottomNavigationBar: new Material(color: Colors.white,child: new TabBar(indicatorPadding: new EdgeInsets.only(top: 0.0),controller: _tabController,tabs: _renderTab(),indicatorColor: Colors.red,),),);}}
physics: new AlwaysScrollableScrollPhysics(), // 讓ListView一直可以滾動
解決:AutomaticKeepAliveClientMixinclass HomePageState extends State<HomePage> with AutomaticKeepAliveClientMixin {@overridebool get wantKeepAlive => true;}
///不帶參數(shù)的路由表跳轉(zhuǎn)Navigator.pushNamed(context,routeName);///跳轉(zhuǎn)新頁面并且替換,比如登錄頁跳轉(zhuǎn)主頁Navigator.pushReplacementNamed(context,routeName);///跳轉(zhuǎn)到新的路由,并且關(guān)閉給定路由的之前的所有頁面Navigator.pushNamedAndRemoveUntil(context,'/calendar',ModalRoute.withName('/'));///帶參數(shù)的路由跳轉(zhuǎn),并且監(jiān)聽返回Navigator.push(context,newMaterialPageRoute(builder:(context)=>newNotifyPage())).then((res){///獲取返回處理});
cupertino_icons: ^0.1.2#iconflutter_spinkit: "^2.1.0"# load more loadingimport 'package:flutter_spinkit/flutter_spinkit.dart';dio: x.x.x#無網(wǎng)絡(luò)請求import 'package:dio/dio.dart';
_dioRequest() async {Dio dio = new Dio();Response response;try {String url;var params; // 請求參數(shù)Options options; // 配置:超時,請求頭,請求類型等response = await dio.request(url, data: params, options: options);} on DioError catch(e) {// 請求出錯時,返回一個DioError對象}}
1、在根目錄運行2、一次性創(chuàng)建.g.dart文件 使用build 此時目錄內(nèi)不能有.g.dart文件3、watch是監(jiān)聽 有model類的文件創(chuàng)建 自動創(chuàng)建.g.dart文件flutter packages pub run build_runner buildflutter packages pub run build_runner watch
日期:2018-10 瀏覽次數(shù):7357
日期:2018-12 瀏覽次數(shù):4425
日期:2018-07 瀏覽次數(shù):4960
日期:2018-12 瀏覽次數(shù):4260
日期:2018-09 瀏覽次數(shù):5597
日期:2018-12 瀏覽次數(shù):10014
日期:2018-11 瀏覽次數(shù):4899
日期:2018-07 瀏覽次數(shù):4666
日期:2018-05 瀏覽次數(shù):4952
日期:2018-12 瀏覽次數(shù):4403
日期:2018-10 瀏覽次數(shù):5226
日期:2018-12 瀏覽次數(shù):6300
日期:2018-11 瀏覽次數(shù):4555
日期:2018-08 瀏覽次數(shù):4681
日期:2018-11 瀏覽次數(shù):12747
日期:2018-09 瀏覽次數(shù):5667
日期:2018-12 瀏覽次數(shù):4934
日期:2018-10 瀏覽次數(shù):4267
日期:2018-11 瀏覽次數(shù):4616
日期:2018-12 瀏覽次數(shù):6151
日期:2018-06 瀏覽次數(shù):4096
日期:2018-08 瀏覽次數(shù):5541
日期:2018-10 瀏覽次數(shù):4538
日期:2018-12 瀏覽次數(shù):4624
日期:2018-07 瀏覽次數(shù):4451
日期:2018-12 瀏覽次數(shù):4601
日期:2018-06 瀏覽次數(shù):4483
日期:2018-11 瀏覽次數(shù):4459
日期:2018-12 瀏覽次數(shù):4340
日期:2018-12 瀏覽次數(shù):5361
Copyright ? 2013-2018 Tadeng NetWork Technology Co., LTD. All Rights Reserved.