使用 Container 、 ClipRRect 、 CircleAvatar 、 Card 和 PhysicalModel 實(shí)現(xiàn)具有視覺(jué)吸引力的圖像效果。
在 Flutter 應(yīng)用 UI 設(shè)計(jì)中,圓形圖像是常見(jiàn)的視覺(jué)元素。本博客探討了使用不同技術(shù)實(shí)現(xiàn)圓形圖像效果的各種方法。無(wú)論是使用網(wǎng)絡(luò)圖像、本地文件還是資源,這些方法都可以靈活地創(chuàng)建圓形和圓角形狀。
我們來(lái)探討一下Flutter中實(shí)現(xiàn)圓形圖像的方法:
圖片
您可以使用 Container 小部件在 Flutter 應(yīng)用中創(chuàng)建圓形圖像。這是一個(gè)簡(jiǎn)單的方法:
Container( width: 120, height: 120, clipBehavior: Clip.antiAlias, decoration: const BoxDecoration( shape: BoxShape.circle, ), child: Image.network( 'https://picsum.photos/seed/picsum/200/300', fit: BoxFit.cover, ),),
通過(guò)將 BoxDecoration 與 BoxShape.circle 一起使用,您可以定義容器的圓形形狀。但是,為了確保圖像完全適合圓圈內(nèi),我們添加 clipBehavior: Clip.antiAlias 。這會(huì)剪輯圖像以匹配圓形形狀,從而創(chuàng)建無(wú)縫的圓形圖像效果。
ClipRRect( borderRadius: BorderRadius.circular(120), child: Image.network( 'https://picsum.photos/seed/picsum/200/300', fit: BoxFit.cover, width: 120, height: 120, ),),
使用 ClipRRect 定義圓角矩形,并通過(guò)將 borderRadius 設(shè)置為 120 等值,確保角完全圓滑。由于指定的尺寸(寬度和高度),子圖像適合此圓角矩形。fit: BoxFit.cover 屬性確保圖像覆蓋整個(gè)區(qū)域,同時(shí)保持其縱橫比。這會(huì)產(chǎn)生平滑的圓形圖像效果。
CircleAvatar( radius: 60, backgroundImage: NetworkImage( 'https://picsum.photos/seed/904/600', ),),
使用 CircleAvatar 小部件,您所需要做的就是設(shè)置 radius 屬性來(lái)確定圓形頭像的大小。此外,您可以使用 backgroundImage 屬性指定圖像的 URL,小部件將自動(dòng)創(chuàng)建圓形圖像效果。
Card( shape: const CircleBorder(), clipBehavior: Clip.antiAlias, elevation: 5, child: Image.network( 'https://picsum.photos/seed/904/600', width: 120, height: 120, fit: BoxFit.cover, ),)
通過(guò)將 shape 設(shè)置為 CircleBorder() ,您可以為 Card 定義圓形形狀。clipBehavior: Clip.antiAlias 屬性確保子內(nèi)容被剪裁以匹配圓形形狀。這會(huì)產(chǎn)生干凈的圓形圖像效果。除此之外,您還可以指定卡片的不同屬性,例如標(biāo)高、陰影顏色等。
PhysicalModel( color: Colors.transparent, clipBehavior: Clip.antiAlias, elevation: 5.0, shape: BoxShape.circle, child: Image.network( 'https://picsum.photos/seed/904/600', width: 120, height: 120, fit: BoxFit.cover, ))
通過(guò)調(diào)整 elevation,可以增加圖像的深度。shape: BoxShape.circle 確保圓形外觀,使用 color: Colors.transparent ,小部件保持半透明。通過(guò)設(shè)置 borderRadius: BorderRadius.circular(10) ,您可以確定角的曲率。clipBehavior: Clip.antiAlias 確保子內(nèi)容與圓角對(duì)齊。
圖片
原文:https://medium.com/@dudhatkirtan/different-ways-to-creating-rounded-corner-image-and-circular-image-in-flutter-498e0a45b502
本文鏈接:http://www.www897cc.com/showinfo-26-58905-0.html在 Flutter 中創(chuàng)建圓角圖像和圓形圖像有多少種方法?
聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。郵件:2376512515@qq.com