ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

flutter-开发总结

2019-04-11 08:49:51  阅读:398  来源: 互联网

标签:总结 color Text Colors 开发 child new flutter ###


  1 ### 上拉加载下拉刷新
  2 ```
  3 import 'dart:async';
  4 import 'package:flutter_easyrefresh/easy_refresh.dart';
  5 import 'package:flutter_easyrefresh/bezier_hour_glass_header.dart';
  6 import 'package:flutter_easyrefresh/bezier_bounce_footer.dart';
  7 import 'package:flutter_smart_park/widget/refresh/emptyWidget.dart';
  8 
  9 List list = [];
 10 
 11 GlobalKey<EasyRefreshState> _easyRefreshKey = new GlobalKey<EasyRefreshState>();
 12 GlobalKey<RefreshHeaderState> _headerKey = new GlobalKey<RefreshHeaderState>();
 13 GlobalKey<RefreshFooterState> _footerKey = new GlobalKey<RefreshFooterState>();
 14 
 15 new EasyRefresh(
 16 key: _easyRefreshKey,
 17 autoLoad: true, //自动加载
 18 autoControl: false, //自动控制
 19 firstRefresh: true, //首次加载
 20 firstRefreshWidget: null,
 21 refreshHeader: BezierHourGlassHeader(
 22 key: _headerKey,
 23 color: Theme.of(context).scaffoldBackgroundColor,
 24 ),
 25 refreshFooter: BezierBounceFooter(
 26 key: _footerKey,
 27 color: Theme.of(context).scaffoldBackgroundColor,
 28 ),
 29 child: new text('data'),
 30 emptyWidget: new EmptyWidget(),
 31 onRefresh: () async {
 32 List a = [];
 33 for (var i = 0; i < 10; i++) {
 34 a.add(i);
 35 }
 36 
 37 new Timer(new Duration(seconds: 2), () {
 38 setState(() {
 39 list.clear();
 40 list.addAll(a);
 41 });
 42 _easyRefreshKey.currentState.callRefreshFinish();
 43 });
 44 },
 45 loadMore: () async {
 46 List a = [];
 47 for (var i = 0; i < 10; i++) {
 48 a.add(i);
 49 }
 50 
 51 new Timer(new Duration(seconds: 2), () {
 52 setState(() {
 53 if (list.length < 20) {
 54 list.addAll(a);
 55 print(list.length);
 56 }
 57 });
 58 _easyRefreshKey.currentState.callLoadMoreFinish();
 59 });
 60 },
 61 ),
 62 ```
 63 
 64 ### 底部导航 
 65 ```
 66 bottomNavigationBar: BottomNavigationBar(
 67 items: <BottomNavigationBarItem>[
 68 BottomNavigationBarItem(icon: Icon(Icons.home), title: Text('Home')),
 69 BottomNavigationBarItem(icon: Icon(Icons.business), title: Text('Business')),
 70 BottomNavigationBarItem(icon: Icon(Icons.school), title: Text('School')),
 71 ],
 72 currentIndex: _selectedIndex,
 73 fixedColor: Colors.deepPurple,
 74 onTap: _onItemTapped,
 75 ),
 76 
 77 ```
 78 
 79 ### listView 列表
 80 ```
 81 new ListView.builder(
 82 scrollDirection: Axis.horizontal, //横向
 83 shrinkWrap:true, // 自动高度
 84 physics: NeverScrollableScrollPhysics(), // 禁止滚动
 85 itemCount: 10,
 86 itemBuilder: (BuildContext context, int index) {
 87 
 88 },
 89 ),
 90 ```
 91 
 92 ### text 文本
 93 ```
 94 Text(
 95 "Hello world "*6, //字符串重复六次
 96 textAlign: TextAlign.center, //对齐方式
 97 overflow: TextOverflow.ellipsis, // 文本溢出
 98 maxLines: 3, // 最多显示的行数
 99 textScaleFactor: 1.5,
100 style: TextStyle(
101 color: Colors.blue,
102 fontSize: 18.0,
103 height: 1.2, 
104 fontFamily: "Courier",
105 background: new Paint()..color=Colors.yellow,
106 decoration:TextDecoration.underline,
107 decorationStyle: TextDecorationStyle.dashed
108 ),
109 );
110 
111 // 一行文字多种样式
112 Text.rich(
113 TextSpan(
114 children: [
115 TextSpan(
116 text: "Home: "
117 ),
118 TextSpan(
119 text: "https://flutterchina.club",
120 style: TextStyle(
121 color: Colors.blue
122 ),
123 ),
124 ]
125 ),
126 )
127 ```
128 
129 ### Container容器
130 ```
131 new Container(
132 alignment: Alignment.center, //对齐方式
133 decoration:new BoxDecoration(
134 gradient:const LinearGradient(
135 colors:[Colors.lightBlue,Colors.greenAccent,Colors.purple] //渐变
136 ),
137 border:Border.all(width:2.0,color:Colors.red)
138 ),
139 ),
140 ```
141 
142 ### Image图片
143 BoxFit.fill:全图显示,图片会被拉伸,并充满父容器。
144 BoxFit.contain:全图显示,显示原比例,可能会有空隙。
145 BoxFit.cover:显示可能拉伸,可能裁切,充满(图片要充满整个容器,还不变形)。
146 BoxFit.fitWidth:宽度充满(横向充满),显示可能拉伸,可能裁切。
147 BoxFit.fitHeight :高度充满(竖向充满),显示可能拉伸,可能裁切。
148 BoxFit.scaleDown:效果和contain差不多,但是此属性不允许显示超过源图片大小,可小不可大。
149 
150 ImageRepeat.repeat : 横向和纵向都进行重复,直到铺满整个画布。
151 ImageRepeat.repeatX: 横向重复,纵向不重复。
152 ImageRepeat.repeatY:纵向重复,横向不重复。
153 
154 ```
155 new Image.network(
156 'http://jspang.com/static/myimg/blogtouxiang.jpg',
157 scale:1.0,
158 fit:BoxFit.cover,
159 olorBlendMode: BlendMode.darken, //图片混合模式
160 repeat: ImageRepeat.repeat, //图片重复
161 ),
162 
163 CachedNetworkImage(
164 imageUrl: 'https://pro.modao.cc/uploads4/images/3089/30894737/v2_pnb2pb.png',
165 placeholder: (context, url) => CircularProgressIndicator(),
166 errorWidget: (context, url, error) => Icon(Icons.error),
167 fit: BoxFit.cover,
168 width: ScreenUtil().setWidth(130),
169 height: ScreenUtil().setWidth(90),
170 ),
171 ```
172 
173 ### GridView网格列表组件
174 ```
175 new GridView(
176 gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
177 crossAxisCount: 3,
178 mainAxisSpacing: 2.0,
179 crossAxisSpacing: 2.0,
180 childAspectRatio: 0.7
181 ),
182 children: <Widget>[
183 new Image.network('http://img5.mtime.cn/mt/2018/10/22/104316.77318635_180X260X4.jpg',fit: BoxFit.cover),
184 new Image.network('http://img5.mtime.cn/mt/2018/10/10/112514.30587089_180X260X4.jpg',fit: BoxFit.cover),
185 ],
186 )
187 
188 new GridView.builder(
189 shrinkWrap: true,
190 physics: new NeverScrollableScrollPhysics(),
191 gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
192 crossAxisCount: 4, //一行多少列
193 mainAxisSpacing: ScreenUtil().setWidth(LqruiStyle.borderWidth), //y轴距离
194 crossAxisSpacing:
195 ScreenUtil().setWidth(LqruiStyle.borderWidth), //x轴距离
196 childAspectRatio: 1.4, //框的比列
197 ),
198 itemCount: homeIcon.length,
199 itemBuilder: (BuildContext context, int index) {
200 <!-- 内容 -->
201 },
202 ),
203 ```
204 
205 ### Column垂直布局
206 ```
207 new Column(
208 mainAxisAlignment: MainAxisAlignment.center,
209 crossAxisAlignment: CrossAxisAlignment.start,
210 children: <Widget>[
211 Text('I am JSPang'),
212 Text('my website is jspang.com'),
213 Text('I love coding')
214 ],
215 )
216 ```
217 
218 ### Row横向布局
219 ```
220 new Row(
221 mainAxisAlignment: MainAxisAlignment.center,
222 crossAxisAlignment: CrossAxisAlignment.start,
223 children: <Widget>[
224 Expanded(
225 child: Text('I am JSPang'),
226 )
227 Text('my website is jspang.com'),
228 Text('I love coding')
229 ],
230 )
231 ```
232 
233 ### Stack层叠布局
234 ```
235 var stack = new Stack(
236 alignment: const FractionalOffset(0.5, 0.8),
237 children: <Widget>[
238 new CircleAvatar(
239 backgroundImage: new NetworkImage('http://jspang.com/static//myimg/blogtouxiang.jpg'),
240 radius: 100.0,
241 ),
242 new Container(
243 decoration: new BoxDecoration(
244 color: Colors.lightBlue,
245 ),
246 padding: EdgeInsets.all(5.0),
247 child: new Text('JSPang 技术胖'),
248 ),
249 new Positioned(
250 bottom:10.0,
251 right:10.0,
252 child: new Text('技术胖'),
253 ),
254 ],
255 );
256 ```
257 
258 ### Card卡片组件布局
259 ```
260 new Card(
261 child: Column(
262 children: <Widget>[
263 ListTile(
264 title:new Text('吉林省吉林市昌邑区',style: TextStyle(fontWeight: FontWeight.w500),),
265 subtitle: new Text('技术胖:1513938888'),
266 leading: new Icon(Icons.account_box,color: Colors.lightBlue,),
267 ),
268 new Divider(),
269 ListTile(
270 title:new Text('北京市海淀区中国科技大学',style: TextStyle(fontWeight: FontWeight.w500),),
271 subtitle: new Text('胜宏宇:1513938888'),
272 leading: new Icon(Icons.account_box,color: Colors.lightBlue,),
273 ),
274 new Divider(),
275 ListTile(
276 title:new Text('河南省濮阳市百姓办公楼',style: TextStyle(fontWeight: FontWeight.w500),),
277 subtitle: new Text('JSPang:1513938888'),
278 leading: new Icon(Icons.account_box,color: Colors.lightBlue,),
279 ),
280 new Divider(),
281 ],
282 ),
283 );
284 ```
285 
286 ### 路由
287 ```
288 import 'package:fluro/fluro.dart';
289 import 'package:flutter_smart_park/routes/routes.dart';
290 
291 Routes.router
292 .navigateTo(context, '${Routes.xxx}?id=1',
293 transition: TransitionType.inFromRight)
294 .then((result) {
295 print(result);
296 });
297 
298 Routes.router.navigateTo(
299 context,
300 '${Routes.userSignUp}',
301 transition: TransitionType.inFromRight,
302 );
303 
304 ```
305 
306 ### 文本左右布局
307 ```
308 Widget page(title, text) {
309   return new Container(
310     margin: EdgeInsets.only(top: ScreenUtil().setWidth(10)),
311     child: Row(
312       children: <Widget>[
313         Expanded(
314           child: Text(
315             '$title',
316             style: TextStyle(
317               color: Color(LqruiStyle.colorText2),
318               fontSize: ScreenUtil().setSp(14),
319             ),
320           ),
321         ),
322         Expanded(
323           flex: 0,
324           child: Text(
325             '$text',
326             style: TextStyle(
327               fontSize: ScreenUtil().setSp(14),
328             ),
329           ),
330         ),
331       ],
332     ),
333   );
334 }
335 ```
336 
337 ### 文本默认样式
338 ```
339 new DefaultTextStyle(
340 //1.设置文本默认样式 
341 style: TextStyle(
342 color:Colors.red,
343 fontSize: 20.0,
344 ),
345 textAlign: TextAlign.start,
346 child: Column(
347 crossAxisAlignment: CrossAxisAlignment.start,
348 children: <Widget>[
349 Text("hello world"),
350 Text("I am Jack"),
351 Text("I am Jack",
352 style: TextStyle(
353 inherit: false, //2.不继承默认样式
354 color: Colors.grey
355 ),
356 ),
357 ],
358 ),
359 );
360 ```
361 
362 ### 页面加载事件
363 ```
364 @override
365 void initState() {
366 super.initState();
367 print('111111111111111111111111111');
368 }
369 ```
370 
371 ### 输入法弹起导致容器越界
372 ```
373 1. 包一层SingleChildScrollView
374 2. 设置Scaffold resizeToAvoidBottomPadding: false,
375 ```
376 
377 ### 切换后页面状态的保持
378 ```
379 // 子页面
380 class _HomePageState extends State<HomePage> with AutomaticKeepAliveClientMixin {
381 @override
382 bool get wantKeepAlive =>true;
383 }
384 
385 // 使用 IndexedStack
386 new IndexedStack(
387 index: currentIndex,
388 children: tabBodies
389 )
390 ```
391 
392 ### 点击事件
393 ```
394 new GestureDetector(
395 child: new Text("忘记密码"),
396 onTap: () => {}, //点击
397 onDoubleTap: () => {}, //双击
398 onLongPress: () => {}, //长按
399 ),
400 ```
401 
402 ### 输入框
403 ```
404 Container(
405 child: TextField(
406 keyboardType: TextInputType.emailAddress,
407 decoration: InputDecoration(
408 labelText: "Email",
409 hintText: "电子邮件地址",
410 prefixIcon: Icon(Icons.email),
411 border: InputBorder.none //隐藏下划线
412 )
413 ),
414 decoration: BoxDecoration(
415 // 下滑线浅灰色,宽度1像素
416 border: Border(bottom: BorderSide(color: Colors.grey[200], width: 1.0))
417 ),
418 )
419 
420 Theme(
421 data: Theme.of(context).copyWith(
422 hintColor: Color(LqruiStyle.dividerColor), //定义下划线颜色
423 inputDecorationTheme: InputDecorationTheme(
424 labelStyle:
425 TextStyle(color: Colors.grey), //定义label字体样式
426 hintStyle: TextStyle(
427 color: Colors.grey, fontSize: 14.0) //定义提示文本样式
428 ),
429 ),
430 child: Form(),
431 ),
432 ```
433 
434 ### 圆角容器
435 ```
436 new ClipOval( //默认全圆角
437 child: Container(
438 width: 100,
439 height: 100, 
440 color: Colors.red, 
441 ),
442 ),
443 
444 new ClipRRect( //自定义
445 borderRadius: BorderRadius.circular(50)
446 child: Container(
447 width: 100,
448 height: 100,
449 color: Colors.red, 
450 ),
451 ),
452 ```
453 
454 ### 下拉框
455 ```
456 DropdownButtonHideUnderline( //隐藏下划线盒子
457 child: DropdownButton(
458 items: items, //下拉菜单item点击之后的回调
459 hint: new Text('下拉选择你想要的数据'), //当没有默认值的时候可以设置的提示
460 value: widget.select, //下拉菜单选择完之后显示给用户的值
461 onChanged: widget.onChanged, //下拉菜单item点击之后的回调
462 elevation: 24, //设置阴影的高度
463 // isDense: true,
464 //减少按钮的高度。默认情况下,此按钮的高度与其菜单项的高度相同。如果isDense为true,
465 则按钮的高度减少约一半。 这个当按钮嵌入添加的容器中时,非常有用
466 isExpanded: true,
467 iconSize: ScreenUtil().setSp(30), //设置三角标icon的大小
468 ),
469 );
470 ```
471 
472 ### 单边框
473 ```
474 decoration: BoxDecoration(
475 border: Border(
476 left: BorderSide(
477 width: ScreenUtil().setWidth(5),
478 color: Color(LqruiStyle.primaryColor),
479 style: BorderStyle.solid,
480 ),
481 ),
482 ),
483 ```
484 
485 ### 模拟器分辨率
486 ```
487 414 736 154
488 360 640 320
489 ```
490 
491 ### 状态栏去阴影
492 ```
493 import 'dart:io';
494 import 'package:flutter/services.dart';
495 
496 if (Platform.isAndroid) {
497 SystemUiOverlayStyle systemUiOverlayStyle = SystemUiOverlayStyle(
498 statusBarColor: Colors.red,
499 );
500 SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
501 }
502 ```

 

 

### Text属性详解

https://www.jianshu.com/p/70376c16bdef

标签:总结,color,Text,Colors,开发,child,new,flutter,###
来源: https://www.cnblogs.com/john-hwd/p/10687444.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有