網站首頁 編程語言 正文
正文
flutter組件的實現參考了react的設計理念,界面上所有的內容都是由組件構成,同時也有狀態組件和無狀態組件之分,這里簡單介紹最基本的組件。
在組件代碼的書寫方式上,web端開發的樣式主要有由css進行控制,而客戶端開發根據使用的技術棧不同,寫法也稍微有些不同:ReactNative的寫法和web比較類似,但是ReactNative是使用StyleSheet.create()
方法創建樣式對象,以內聯的方式進行書寫。
import React from 'react'; import { StyleSheet, Text, View } from 'react-native'; const LotsOfStyles = () => { return ( <View style={styles.container}> <Text style={styles.red}>just red</Text> <Text style={styles.bigBlue}>just bigBlue</Text> <Text style={[styles.bigBlue, styles.red]}>bigBlue, then red</Text> <Text style={[styles.red, styles.bigBlue]}>red, then bigBlue</Text> </View> ); }; const styles = StyleSheet.create({ container: { marginTop: 50, }, bigBlue: { color: 'blue', fontWeight: 'bold', fontSize: 30, }, red: { color: 'red', }, }); export default LotsOfStyles;
而flutter則將組件封裝成一個個的對象,樣式及事件以屬性的方式在實例化時進行賦值。
Text( 'Hello, $_name! How are you?', textAlign: TextAlign.center, overflow: TextOverflow.ellipsis, style: const TextStyle(fontWeight: FontWeight.bold), )
Text組件
用我們的小拇指頭就可以想到,Text組件主要是用來展示一個文本字符串。這字符串根據布局容器的約束空間有可能占展示一行文本,也有可能展示多行文本。
Text
組件的構造器有一個可選的style
屬性,如果我們省略掉這個屬性,那么文本就會使用默認的樣式。
如果我們指定了我們定制的style
樣式,這個樣式的類對象是TextStyle
。我們定制的style
樣式會被merge
到最近的默認樣式DefaultTextStyle上去。
默認樣式類DefaultTextStyle
有這么幾個屬性:
maxLine: 最大行數,這個屬性是可選的。
overflow: 文本超出后的樣式。 overflow 的可選值有這么幾個:clip(剪切)、fade(隱藏)、ellipsis(省略)、visible(直接展示)。如果我們點開文檔看一下,會發現它其實是個枚舉類型Enum
。
const TextOverflow = { clip, fade, ellipsis, visible }
Text組件構造器上的主要屬性
- style: 文本樣式。
- textAlign: 文本對齊方式。
- textDirection: 文本方向。
- textHeightBehavior: 定義如何展示style中的height
- selectionColor: 文本選中時的顏色。
- overflow: 文本超出后的樣式。
- maxLine: 最大行數,這個屬性是可選的。
再用小拇指想一想,對齊方式和文本方向不用說也是個枚舉類型。而style則是一個TextStyle
的類型,TextStyle
可以定義字體的:
- 粗細
fontWeight
const Text( 'No, we need bold strokes. ', style: TextStyle(fontWeight: FontWeight.bold), )
- 斜體
FontStyle.italic
const Text( "Welcome to the present", style: TextStyle(fontStyle: FontStyle.italic), )
- 透明度和顏色
TextSpan( text: "You don't have the votes.\n", style: TextStyle( color: Colors.black.withOpacity(0.6)), ),
- 字體大小
Text( "These are wise words, ", style:DefaultTextStyle.of(context).style.apply(fontSizeFactor: 2.0), )
- 行高
const Text( 'Ladies and gentlemen, ', style: TextStyle(height: 5, fontSize: 10), )
需要注意的是:行高會按照 fontSize * n
的比例進行擴展。
然后我們還可以定義字體的下劃線、描邊、填充顏色、甚至漸變色。
- 下劃線
RichText( text: const TextSpan( text: "Don't tax the South ", children: <TextSpan>[ TextSpan( text: 'cuz', style: TextStyle( color: Colors.black, decoration: TextDecoration.underline, decorationColor: Colors.red, decorationStyle:TextDecorationStyle.wavy, ), ), TextSpan( text: ' we got it made in the shade', ), ], ), )
- 描邊和填充顏色
Stack( children: <Widget>[ Text( 'Greetings, planet!', style: TextStyle( fontSize: 40, foreground: Paint() ..style = PaintingStyle.stroke ..strokeWidth = 6 ..color = Colors.blue[700]!, ), ), Text( 'Greetings, planet!', style: TextStyle( fontSize: 40, color: Colors.grey[300], ), ), ], )
- 顏色漸變
Text( 'Greetings, planet!', style: TextStyle( fontSize: 40, foreground: Paint() ..shader = ui.Gradient.linear( const Offset(0, 20), const Offset(150, 20), <Color>[ Colors.red, Colors.yellow, ], ) ), )
整體上想要掌握Text組件的屬性,需要仔細思考一下它大概需要哪些樣式:選用哪種字體,設置什么顏色,需要多少行高,選用哪種對齊方式,是否需要描邊和漸變,是否需要一種裝飾樣式(下劃線,刪除線)就可以掌握了。
而想要文本有可交互的效果,則需要用GestureDetector
這個組件將它包裹起來,在GestureDetector
組件上觸發ontap
s事件。
掌握了這些內容,就算是掌握了flutter的text組件。
原文鏈接:https://juejin.cn/post/7171850429838884894
相關推薦
- 2022-09-30 Python使用draw類繪制圖形示例講解_python
- 2022-04-05 react props數據更改傳到方法里進行渲染html,當父組件改變props改變傳入函數參數沒有
- 2023-07-15 oracle 序列/自增ID
- 2022-10-24 Python?NumPy教程之數據類型對象詳解_python
- 2022-06-10 Linux環境下部署Consul集群_Linux
- 2022-09-16 Go1.16新特性embed打包靜態資源文件實現_Golang
- 2022-10-24 React中的生命周期和子組件_React
- 2022-06-17 Go基礎教程系列之WaitGroup用法實例詳解_Golang
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支