1.Flutter(å)ä¹Flutterçå¸å±Widget
2.深入探索 Flutter 加载优化: cached_network_image 源码解析
3.flutter热更新安卓端详细教程-附源码-超简单
Flutter(å)ä¹Flutterçå¸å±Widget
ä¸.ååå¸å±ç»ä»¶
ååå¸å±ç»ä»¶çå«ä¹æ¯å ¶åªæä¸ä¸ªåç»ä»¶ï¼å¯ä»¥éè¿è®¾ç½®ä¸äºå±æ§è®¾ç½®è¯¥åç»ä»¶æå¨çä½ç½®ä¿¡æ¯çã
æ¯è¾å¸¸ç¨çååå¸å±ç»ä»¶æï¼AlignãCenterãPaddingãContainerã
1.1.Alignç»ä»¶1.1.1.Alignä»ç»çå°Alignè¿ä¸ªè¯ï¼æ们就ç¥éå®ææ们ç对é½æ¹å¼æå ³ã
å¨å ¶ä»ç«¯çå¼åä¸ï¼iOSãAndroidãå端ï¼Aligné常åªæ¯ä¸ä¸ªå±æ§èå·²ï¼ä½æ¯Flutterä¸Alignä¹æ¯ä¸ä¸ªç»ä»¶ã
æ们å¯ä»¥éè¿æºç æ¥çä¸ä¸Alignæåªäºå±æ§ï¼
constAlign({ Keykey,码教this.alignment:Alignment.center,//对é½æ¹å¼ï¼é»è®¤å± ä¸å¯¹é½this.widthFactor,//宽度å åï¼ä¸è®¾ç½®çæ åµï¼ä¼å°½å¯è½å¤§this.heightFactor,//é«åº¦å åï¼ä¸è®¾ç½®çæ åµï¼ä¼å°½å¯è½å¤§Widgetchild//è¦å¸å±çåWidget})è¿éæ们ç¹å«è§£éä¸ä¸widthFactoråheightFactorä½ç¨ï¼
å 为åç»ä»¶å¨ç¶ç»ä»¶ä¸ç对é½æ¹å¼å¿ é¡»æä¸ä¸ªåæï¼å°±æ¯ç¶ç»ä»¶å¾ç¥éèªå·±çèå´ï¼å®½åº¦åé«åº¦ï¼ï¼
å¦æwidthFactoråheightFactorä¸è®¾ç½®ï¼é£ä¹é»è®¤Alignä¼å°½å¯è½ç大ï¼å°½å¯è½å æ®èªå·±æå¨çç¶ç»ä»¶ï¼ï¼
æ们ä¹å¯ä»¥å¯¹ä»ä»¬è¿è¡è®¾ç½®ï¼æ¯å¦widthFactor设置为3ï¼é£ä¹ç¸å¯¹äºAlignç宽度æ¯åç»ä»¶è·¨åº¦ç3åï¼
1.1.2.Alignæ¼ç»æ们ç®åæ¼ç»ä¸ä¸Align:
classMyHomeBodyextendsStatelessWidget{ @overrideWidgetbuild(BuildContextcontext){ returnAlign(child:Icon(Icons.pets,size:,color:Colors.red),alignment:Alignment.bottomRight,widthFactor:3,heightFactor:3,);}}1.2.Centerç»ä»¶1.2.1.Centerä»ç»Centerç»ä»¶æ们å¨åé¢å·²ç»ç¨è¿å¾å¤æ¬¡äºã
äºå®ä¸Centerç»ä»¶ç»§æ¿èªAlignï¼åªæ¯å°alignment设置为Alignment.centerã
æºç åæï¼
classCenterextendsAlign{ constCenter({ Keykey,doublewidthFactor,doubleheightFactor,Widgetchild}):super(key:key,widthFactor:widthFactor,heightFactor:heightFactor,child:child);}1.2.2.Centeræ¼ç»æ们å°ä¸é¢ç代ç Alignæ¢æCenter
classMyHomeBodyextendsStatelessWidget{ @overrideWidgetbuild(BuildContextcontext){ returnCenter(child:Icon(Icons.pets,size:,color:Colors.red),widthFactor:3,heightFactor:3,);}}1.3.Paddingç»ä»¶1.3.1.Paddingä»ç»Paddingç»ä»¶å¨å ¶ä»ç«¯ä¹æ¯ä¸ä¸ªå±æ§èå·²ï¼ä½æ¯å¨Flutterä¸æ¯ä¸ä¸ªWidgetï¼ä½æ¯Flutterä¸æ²¡æMarginè¿æ ·ä¸ä¸ªWidgetï¼è¿æ¯å 为å¤è¾¹è·ä¹å¯ä»¥éè¿Paddingæ¥å®æã
Paddingé常ç¨äºè®¾ç½®åWidgetå°ç¶Widgetçè¾¹è·ï¼ä½ å¯ä»¥ç§°ä¹ä¸ºæ¯ç¶ç»ä»¶çå è¾¹è·æåWidgetçå¤è¾¹è·ï¼ã
æºç åæï¼
constPadding({ Keykey,@requiredthis.padding,//EdgeInsetsGeometryç±»åï¼æ½è±¡ç±»ï¼ï¼ä½¿ç¨EdgeInsetsWidgetchild,})1.3.2.Paddingæ¼ç»ä»£ç æ¼ç»ï¼
classMyHomeBodyextendsStatelessWidget{ @overrideWidgetbuild(BuildContextcontext){ returnPadding(padding:EdgeInsets.all(),child:Text("è«å¬ç©¿ææå¶å£°ï¼ä½å¦¨åå¸ä¸å¾è¡ã竹æèéè½»è马ï¼è°æï¼ä¸èçé¨ä»»å¹³çã",style:TextStyle(color:Colors.redAccent,fontSize:),),);}}1.4.Containerç»ä»¶Containerç»ä»¶ç±»ä¼¼äºå ¶ä»Androidä¸çViewï¼iOSä¸çUIViewã
å¦æä½ éè¦ä¸ä¸ªè§å¾ï¼æä¸ä¸ªèæ¯é¢è²ãå¾åãæåºå®ç尺寸ãéè¦ä¸ä¸ªè¾¹æ¡ãåè§çææï¼é£ä¹å°±å¯ä»¥ä½¿ç¨Containerç»ä»¶ã
.1.Containerä»ç»Containerå¨å¼åä¸è¢«ä½¿ç¨çé¢çæ¯é常é«çï¼ç¹å«æ¯æ们ç»å¸¸ä¼å°å ¶ä½ä¸ºå®¹å¨ç»ä»¶ã
ä¸é¢æ们æ¥çä¸ä¸Containeræåªäºå±æ§ï¼
Container({ this.alignment,this.padding,//容å¨å è¡¥ç½ï¼å±äºdecorationçè£ é¥°èå´Colorcolor,//èæ¯è²Decorationdecoration,//èæ¯è£ 饰DecorationforegroundDecoration,//åæ¯è£ 饰doublewidth,//容å¨ç宽度doubleheight,//容å¨çé«åº¦BoxConstraintsconstraints,//容å¨å¤§å°çéå¶æ¡ä»¶this.margin,//容å¨å¤è¡¥ç½ï¼ä¸å±äºdecorationçè£ é¥°èå´this.transform,//åæ¢this.child,})大å¤æ°å±æ§å¨ä»ç»å ¶å®å®¹å¨æ¶é½å·²ç»ä»ç»è¿äºï¼ä¸åèµè¿°ï¼ä½æ两ç¹éè¦è¯´æï¼
容å¨ç大å°å¯ä»¥éè¿widthãheightå±æ§æ¥æå®ï¼ä¹å¯ä»¥éè¿constraintsæ¥æå®ï¼å¦æåæ¶åå¨æ¶ï¼widthãheightä¼å ãå®é ä¸Containerå é¨ä¼æ ¹æ®widthãheightæ¥çæä¸ä¸ªconstraintsï¼
colorådecorationæ¯äºæ¥çï¼å®é ä¸ï¼å½æå®coloræ¶ï¼Containerå ä¼èªå¨å建ä¸ä¸ªdecorationï¼
decorationå±æ§ç¨åæ们详ç»å¦ä¹ ï¼
1.4.2.Containeræ¼ç»ç®åè¿è¡ä¸ä¸ªæ¼ç¤ºï¼
classMyHomeBodyextendsStatelessWidget{ @overrideWidgetbuild(BuildContextcontext){ returnCenter(child:Container(color:Color.fromRGBO(3,3,,.5),width:,height:,child:Icon(Icons.pets,size:,color:Colors.white),),);}}1.4.3.BoxDecorationContaineræä¸ä¸ªé常éè¦çå±æ§decorationï¼
ä»å¯¹åºçç±»åæ¯Decorationç±»åï¼ä½æ¯å®æ¯ä¸ä¸ªæ½è±¡ç±»ã
å¨å¼åä¸ï¼æ们ç»å¸¸ä½¿ç¨å®çå®ç°ç±»BoxDecorationæ¥è¿è¡å®ä¾åã
BoxDecoration常è§å±æ§ï¼
constBoxDecoration({ this.color,//é¢è²ï¼ä¼åContainerä¸çcolorå±æ§å²çªthis.image,//èæ¯å¾çthis.border,//è¾¹æ¡ï¼å¯¹åºç±»åæ¯Borderç±»åï¼éé¢æ¯ä¸ä¸ªè¾¹æ¡ä½¿ç¨BorderSidethis.borderRadius,//åè§ææthis.boxShadow,//é´å½±ææthis.gradient,//æ¸åææthis.backgroundBlendMode,//èæ¯æ··åthis.shape=BoxShape.rectangle,//å½¢å})é¨åæææ¼ç¤ºï¼
classMyHomeBodyextendsStatelessWidget{ @overrideWidgetbuild(BuildContextcontext){ returnCenter(child:Container(//color:Color.fromRGBO(3,3,,.5),width:,height:,child:Icon(Icons.pets,size:,color:Colors.white),decoration:BoxDecoration(color:Colors.amber,//èæ¯é¢è²border:Border.all(color:Colors.redAccent,width:3,style:BorderStyle.solid),//è¿éä¹å¯ä»¥ä½¿ç¨Border.allç»ä¸è®¾ç½®//top:BorderSide(//color:Colors.redAccent,//width:3,//style:BorderStyle.solid//),borderRadius:BorderRadius.circular(),//è¿éä¹å¯ä»¥ä½¿ç¨.onlyåå«è®¾ç½®boxShadow:[BoxShadow(offset:Offset(5,5),color:Colors.purple,blurRadius:5)],//shape:BoxShape.circle,//ä¼åborderRadiuså²çªgradient:LinearGradient(colors:[Colors.green,Colors.red])),),);}}1.4.4.å®ç°åè§å¾åä¸ä¸ä¸ªç« èæ们æå°å¯ä»¥éè¿Container+BoxDecorationæ¥å®ç°åè§å¾åã
å®ç°ä»£ç å¦ä¸ï¼
classMyHomeBodyextendsStatelessWidget{ @overrideWidgetbuild(BuildContextcontext){ returnAlign(child:Icon(Icons.pets,size:,color:Colors.red),alignment:Alignment.bottomRight,widthFactor:3,heightFactor:3,);}}0äº.å¤åå¸å±ç»ä»¶å¨å¼åä¸ï¼æ们ç»å¸¸éè¦å°å¤ä¸ªWidgetæ¾å¨ä¸èµ·è¿è¡å¸å±ï¼æ¯å¦æ°´å¹³æ¹åãåç´æ¹åæåï¼çè³ææ¶åéè¦ä»ä»¬è¿è¡å±å ï¼æ¯å¦å¾çä¸é¢æ¾ä¸æ®µæåçï¼
è¿ä¸ªæ¶åæ们éè¦ä½¿ç¨å¤åå¸å±ç»ä»¶ï¼Multi-childlayoutwidgetsï¼ã
æ¯è¾å¸¸ç¨çå¤åå¸å±ç»ä»¶æ¯RowãColumnãStackï¼æ们æ¥å¦ä¹ ä¸ä¸ä»ä»¬ç使ç¨ã
2.1.Flexç»ä»¶äºå®ä¸ï¼æ们å³å°å¦ä¹ çRowç»ä»¶åColumnç»ä»¶é½ç»§æ¿èªFlexç»ä»¶ã
Flexç»ä»¶åRowãColumnå±æ§ä¸»è¦çåºå«å°±æ¯å¤ä¸ä¸ªdirectionã
å½directionçå¼ä¸ºAxis.horizontalçæ¶åï¼åæ¯Rowã
å½directionçå¼ä¸ºAxis.verticalçæ¶åï¼åæ¯Columnã
å¨å¦ä¹ RowåColumnä¹åï¼æ们å å¦ä¹ 主轴å交åè½´çæ¦å¿µã
å 为Rowæ¯ä¸è¡æå¸ï¼Columnæ¯ä¸åæå¸ï¼é£ä¹å®ä»¬é½åå¨ä¸¤ä¸ªæ¹åï¼å¹¶ä¸ä¸¤ä¸ªWidgetæåçæ¹ååºè¯¥æ¯å¯¹ç«çã
å®ä»¬ä¹ä¸é½æ主轴ï¼MainAxisï¼å交åè½´ï¼CrossAxisï¼çæ¦å¿µï¼
对äºRowæ¥è¯´ï¼ä¸»è½´ï¼MainAxisï¼å交åè½´ï¼CrossAxisï¼åå«æ¯ä¸å¾
对äºColumnæ¥è¯´ï¼ä¸»è½´ï¼MainAxisï¼å交åè½´ï¼CrossAxisï¼åå«æ¯ä¸å¾
2.1.Rowç»ä»¶2.1.1.Rowä»ç»Rowç»ä»¶ç¨äºå°ææçåWidgetææä¸è¡ï¼å®é ä¸è¿ç§å¸å±åºè¯¥æ¯åé´äºWebçFlexå¸å±ã
å¦æçæFlexå¸å±ï¼ä¼åç°é常ç®åã
ä»æºç ä¸æ¥çRowçå±æ§ï¼
classMyHomeBodyextendsStatelessWidget{ @overrideWidgetbuild(BuildContextcontext){ returnAlign(child:Icon(Icons.pets,size:,color:Colors.red),alignment:Alignment.bottomRight,widthFactor:3,heightFactor:3,);}}1mainAxisSizeï¼
表示Rowå¨ä¸»è½´(æ°´å¹³)æ¹åå ç¨ç空é´ï¼é»è®¤æ¯MainAxisSize.maxï¼è¡¨ç¤ºå°½å¯è½å¤çå ç¨æ°´å¹³æ¹åç空é´ï¼æ¤æ¶æ 论åwidgetså®é å ç¨å¤å°æ°´å¹³ç©ºé´ï¼Rowç宽度å§ç»çäºæ°´å¹³æ¹åçæ大宽度
èMainAxisSize.min表示尽å¯è½å°çå ç¨æ°´å¹³ç©ºé´ï¼å½åwidgets没æå 满水平å©ä½ç©ºé´ï¼åRowçå®é 宽度çäºææåwidgetså ç¨çç水平空é´ï¼
mainAxisAlignmentï¼è¡¨ç¤ºåWidgetså¨Rowæå ç¨ç水平空é´å 对é½æ¹å¼
å¦æmainAxisSizeå¼ä¸ºMainAxisSize.minï¼åæ¤å±æ§æ æä¹ï¼å 为åwidgetsç宽度çäºRowç宽度
åªæå½mainAxisSizeçå¼ä¸ºMainAxisSize.maxæ¶ï¼æ¤å±æ§æææä¹
MainAxisAlignment.start表示沿textDirectionçåå§æ¹å对é½ï¼
å¦textDirectionåå¼ä¸ºTextDirection.ltræ¶ï¼åMainAxisAlignment.start表示左对é½ï¼textDirectionåå¼ä¸ºTextDirection.rtlæ¶è¡¨ç¤ºä»å³å¯¹é½ã
èMainAxisAlignment.endåMainAxisAlignment.startæ£å¥½ç¸åï¼
MainAxisAlignment.centerè¡¨ç¤ºå± ä¸å¯¹é½ã
crossAxisAlignmentï¼è¡¨ç¤ºåWidgetså¨çºµè½´æ¹åç对é½æ¹å¼
Rowçé«åº¦çäºåWidgetsä¸æé«çåå ç´ é«åº¦
å®çåå¼åMainAxisAlignmentä¸æ ·(å å«startãendãcenterä¸ä¸ªå¼)
ä¸åçæ¯crossAxisAlignmentçåèç³»æ¯verticalDirectionï¼å³verticalDirectionå¼ä¸ºVerticalDirection.downæ¶crossAxisAlignment.startæ顶é¨å¯¹é½ï¼verticalDirectionå¼ä¸ºVerticalDirection.upæ¶ï¼crossAxisAlignment.startæåºé¨å¯¹é½ï¼ècrossAxisAlignment.endåcrossAxisAlignment.startæ£å¥½ç¸åï¼
2.1.2.Rowæ¼ç»æ们æ¥å¯¹é¨åå±æ§è¿è¡ç®åç代ç æ¼ç»ï¼å ¶ä»ä¸äºå±æ§å¤§å®¶èªå·±å¦ä¹ ä¸ä¸
classMyHomeBodyextendsStatelessWidget{ @overrideWidgetbuild(BuildContextcontext){ returnAlign(child:Icon(Icons.pets,size:,color:Colors.red),alignment:Alignment.bottomRight,widthFactor:3,heightFactor:3,);}}.1.3.mainAxisSizeé»è®¤æ åµä¸ï¼Rowä¼å°½å¯è½å æ®å¤ç宽度ï¼è®©åWidgetå¨å ¶ä¸è¿è¡æå¸ï¼è¿æ¯å 为mainAxisSizeå±æ§é»è®¤å¼æ¯MainAxisSize.maxã
æ们æ¥çä¸ä¸ï¼å¦æè¿ä¸ªå¼è¢«ä¿®æ¹ä¸ºMainAxisSize.maxä¼ä»ä¹ååï¼
2.1.4.TextBaselineå ³äºTextBaselineçåå¼è§£æ
2.1.5.Expandedå¦ææ们å¸æ红è²åé»è²çContainerWidgetä¸è¦è®¾ç½®åºå®ç宽度ï¼èæ¯å æ®å©ä½çé¨åï¼è¿ä¸ªæ¶ååºè¯¥å¦ä½å¤çå¢ï¼
è¿ä¸ªæ¶åæ们å¯ä»¥ä½¿ç¨Expandedæ¥å 裹ContainerWidgetï¼å¹¶ä¸å°å®ç宽度ä¸è®¾ç½®å¼ï¼
flexå±æ§ï¼å¼¹æ§ç³»æ°ï¼Rowä¼æ ¹æ®ä¸¤ä¸ªExpandedçå¼¹æ§ç³»æ°æ¥å³å®å®ä»¬å æ®å©ä¸ç©ºé´çæ¯ä¾
classMyHomeBodyextendsStatelessWidget{ @overrideWidgetbuild(BuildContextcontext){ returnAlign(child:Icon(Icons.pets,size:,color:Colors.red),alignment:Alignment.bottomRight,widthFactor:3,heightFactor:3,);}}.2.Columnç»ä»¶Columnç»ä»¶ç¨äºå°ææçåWidgetææä¸åï¼å¦ä¼äºåé¢çRowåï¼Columnåªæ¯årowçæ¹åä¸åèå·²ã
2.2.1.Columnä»ç»æ们ç´æ¥çå®çæºç ï¼æ们åç°åRowå±æ§æ¯ä¸è´çï¼ä¸å解é
classMyHomeBodyextendsStatelessWidget{ @overrideWidgetbuild(BuildContextcontext){ returnAlign(child:Icon(Icons.pets,size:,color:Colors.red),alignment:Alignment.bottomRight,widthFactor:3,heightFactor:3,);}}.2.2.Columnæ¼ç»æ们ç´æ¥å°Rowç代ç ä¸Rowæ¹ä¸ºColumnï¼æ¥ç代ç è¿è¡ææ
classMyHomeBodyextendsStatelessWidget{ @overrideWidgetbuild(BuildContextcontext){ returnAlign(child:Icon(Icons.pets,size:,color:Colors.red),alignment:Alignment.bottomRight,widthFactor:3,heightFactor:3,);}}.3.Stackç»ä»¶å¨å¼åä¸ï¼æ们å¤ä¸ªç»ä»¶å¾æå¯è½éè¦éå æ¾ç¤ºï¼æ¯å¦å¨ä¸å¼ å¾çä¸æ¾ç¤ºæåæè ä¸ä¸ªæé®çã
å¨Androidä¸å¯ä»¥ä½¿ç¨Frameæ¥å®ç°ï¼å¨Web端å¯ä»¥ä½¿ç¨ç»å¯¹å®ä½ï¼å¨Flutterä¸æ们éè¦ä½¿ç¨å±å å¸å±Stackã
2.3.1.Stackä»ç»æ们è¿æ¯éè¿æºç æ¥çä¸ä¸Stackæåªäºå±æ§ï¼
classMyHomeBodyextendsStatelessWidget{ @overrideWidgetbuild(BuildContextcontext){ returnAlign(child:Icon(Icons.pets,size:,color:Colors.red),alignment:Alignment.bottomRight,widthFactor:3,heightFactor:3,);}}6åæ°j解æï¼
alignmentï¼æ¤åæ°å³å®å¦ä½å»å¯¹é½æ²¡æå®ä½ï¼æ²¡æ使ç¨Positionedï¼æé¨åå®ä½çåwidgetãæè°é¨åå®ä½ï¼å¨è¿éç¹æ没æå¨æä¸ä¸ªè½´ä¸å®ä½ï¼leftãright为横轴ï¼topãbottom为纵轴ï¼åªè¦å å«æ个轴ä¸çä¸ä¸ªå®ä½å±æ§å°±ç®å¨è¯¥è½´ä¸æå®ä½ã
textDirectionï¼åRowãWrapçtextDirectionåè½ä¸æ ·ï¼é½ç¨äºå³å®alignment对é½çåèç³»å³ï¼textDirectionçå¼ä¸ºTextDirection.ltrï¼åalignmentçstart代表左ï¼end代表å³ï¼textDirectionçå¼ä¸ºTextDirection.rtlï¼åalignmentçstart代表å³ï¼end代表左ã
fitï¼æ¤åæ°ç¨äºå³å®æ²¡æå®ä½çåwidgetå¦ä½å»éåºStackç大å°ãStackFit.loose表示使ç¨åwidgetç大å°ï¼StackFit.expand表示æ©ä¼¸å°Stackç大å°ã
overflowï¼æ¤å±æ§å³å®å¦ä½æ¾ç¤ºè¶ åºStackæ¾ç¤ºç©ºé´çåwidgetï¼å¼ä¸ºOverflow.clipæ¶ï¼è¶ åºé¨åä¼è¢«åªè£ï¼éèï¼ï¼å¼ä¸ºOverflow.visibleæ¶åä¸ä¼ã
2.3.2.Stackæ¼ç»Stackä¼ç»å¸¸åPositionedä¸èµ·æ¥ä½¿ç¨ï¼Positionedå¯ä»¥å³å®ç»ä»¶å¨Stackä¸çä½ç½®ï¼ç¨äºå®ç°ç±»ä¼¼äºWebä¸çç»å¯¹å®ä½ææã
ä¸ä¸ªç®åçæ¼ç»ï¼
注æï¼Positionedç»ä»¶åªè½å¨Stackä¸ä½¿ç¨ã
classMyHomeBodyextendsStatelessWidget{ @overrideWidgetbuild(BuildContextcontext){ returnAlign(child:Icon(Icons.pets,size:,color:Colors.red),alignment:Alignment.bottomRight,widthFactor:3,heightFactor:3,);}}7\
åæï¼/post/深入探索 Flutter 加载优化: cached_network_image 源码解析
深入探索 Flutter 加载优化: cached_network_image 源码解析
在Flutter项目开发中,高效地加载和缓存是码教优化用户体验的关键。我曾面临性能问题,码教如商品列表滑动卡顿、码教cms福利源码内存占用高,码教选择了cached_network_image插件解决加载难题。码教321资源站源码
CachedNetworkImage是码教Image的升级版,提供网络的码教缓存能力。其build方法内部通过OctoImage加载,码教最终调用ImageHandler,码教形成调用链。码教
加载流程涉及注册流监听、码教获取和缓存数据。码教足疗仪平台源码Image中resolveImage方法创建ImageStreamListener对象,码教加载完成时,码教_handleImageFrame回调并获取ImageInfo,影视解说文案网站源码触发Image重建。
CachedNetworkImage使用自定义ImageProvider-CachedNetworkImageProvider,覆盖resolve方法,ipa软件网站源码返回ImageStream。在_loadAsync中,首先查找缓存,若未命中,发起网络请求下载数据,利用/Android-Alvin/Android-LearningNotes。资源持续更新中...