Flutter開發之Widget學習

来源:https://www.cnblogs.com/jyd0124/archive/2020/02/09/widget.html
-Advertisement-
Play Games

一、Text 組件 屬性 textAlign: TextAlign.left, 文本對齊方式 maxLines: 1, 顯示最大行 overflow: TextOverflow.clip, 文本溢出的處理方式 clip:直接切斷溢出的文字。 ellipsis:在後邊顯示省略號(...) 常用 fad ...


一、Text 組件

屬性

 textAlign: TextAlign.left,                           -----文本對齊方式  maxLines: 1,                                            -----顯示最大行  overflow: TextOverflow.clip,                 -----文本溢出的處理方式
  • clip:直接切斷溢出的文字。
  • ellipsis:在後邊顯示省略號(...)  常用
  • fade: 漸變消失效果

style文字的樣式

 body: new Center(
          child: new Text('非淡泊無以明志,非寧靜無以致遠。(諸葛亮)',
              textAlign: TextAlign.left,
              maxLines: 1,
              overflow: TextOverflow.ellipsis,
              style: TextStyle(
                fontSize: 20,
                color: Color.fromARGB(255, 0, 0, 255),
                decoration: TextDecoration.underline,
                decorationStyle: TextDecorationStyle.solid,
                fontStyle: FontStyle.italic,
              )),
        ),

二、Container組件

 Alignment屬性,Container內child的對齊方式,也就是容器子內容的對齊方式,並不是容器本身的對齊方式。 padding         內邊距 margin           外邊距 decoration     裝飾器 使用:
body: new Center(
          child: new Container(
            child: new Text(
              '非淡泊無以明志,非寧靜無以致遠。(諸葛亮)',
              style: TextStyle(fontSize: 30.0),
            ),
            alignment: Alignment.topLeft,
            width: 500.0,
            height: 200.0,
            //color: Colors.lightBlue,
            //padding: const EdgeInsets.all(10),    //內邊距
            padding: const EdgeInsets.fromLTRB(10.0, 50.0, 0, 0),
            margin: const EdgeInsets.all(20.0),
            decoration: new BoxDecoration(
              gradient: const LinearGradient(
                  colors: [Colors.lightBlue, Colors.green, Colors.purple]
                  ),
                  border: Border.all(width: 5.0,color:Colors.red
                  ),
            ),
          ),
        ),

三、Image組件

加入圖片的方式:

  1. Image.asset                  項目資源圖片
  2. Image.file (絕對路徑)    系統資源圖片
  3. Image.network(url)   網路資源圖片

fit屬性

  • BoxFit.fill                             
  • BoxFit.contain                     
  • BoxFit.cover                        

repeat屬性

  • ImageRepeat.repeat      橫向和縱向都重覆,鋪滿整個容器
  • ImageRepeat.repeatX    橫向重覆
  • ImageRepeat.repeatY    縱向重覆
body: new Center(
            child: new Container(
          child: new Image.network(
            'https://profile.csdnimg.cn/0/5/2/1_jyd0124',
            fit: BoxFit.cover,
            //color: Colors.lightBlue,
            //colorBlendMode: BlendMode.darken, //圖片混合模式(colorBlendMode)和color屬性配合使用
          ),
          width: 300.0,
          height: 200.0,
          color: Colors.lightGreen,
        )
    ),

四、ListView組件

列表使用

body: new ListView(
          children: <Widget>[
            /*new Image.network(
                'https://cdn2.jianshu.io/assets/web/banner-s-club-aa8bdf19f8cf729a759da42e4a96f366.png'),
            new Image.network(
                'https://cdn2.jianshu.io/assets/web/banner-s-7-1a0222c91694a1f38e610be4bf9669be.png'),
             */ //圖片列表使用
            new ListTile(
              leading: new Icon(
                Icons.perm_camera_mic,
              ),
              title: new Text('perm_camera_mic'),
            ),
            new ListTile(
              leading: new Icon(
                Icons.perm_phone_msg,
              ),
              title: new Text('perm_phone_msg'),
            ),
          ],
        ),

橫向列表:ListView組件裡加一個scrollDirection屬性

body: new Center(
            child: new Container(
                height: 200.0,
                child: new ListView(
                  scrollDirection: Axis.horizontal, //Axis.vertical:縱向列表
                  children: <Widget>[
                    new Container(
                      width: 230.0,
                      color: Colors.lightBlue,
                    ),
                    new Container(
                      width: 230.0,
                      color: Colors.lightGreen,
                    ),
                  ],
                ))),

Dart語言List的聲明方式:

  • var myList = List(): 非固定長度的聲明。
  • var myList = List(2): 固定長度的聲明。
  • var myList= List<String>():固定類型的聲明方式。
  • var myList = [1,2,3]: 對List直接賦值
import 'package:flutter/material.dart';

void main() =>
    runApp(MyApp(items: List<String>.generate(1000, (i) => 'item $i')));

class MyApp extends StatelessWidget {
  final List<String> items;
  MyApp({Key key, @required this.items}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'ListView Dome',
      home: new Scaffold(
        appBar: new AppBar(title: new Text('ListView Widget')),
        body: new ListView.builder(
            itemCount: items.length,
            itemBuilder: (context, index) {
              return new ListTile(
                title: new Text('${items[index]}'),
              );
            }),
      ),
    );
  }
}

五、GridView組件

  常用屬性:

  • crossAxisSpacing:網格間的空當。
  • crossAxisCount:一行放置的網格數量
body: GridView.count(
            padding: EdgeInsets.all(20.0),
            crossAxisSpacing: 10.0,
            crossAxisCount: 3,
            children: <Widget>[
              const Text('I am j.y.d'),
              const Text('I love flutter'),
              const Text('jyd0124.com'),
              const Text('2020/02/06'),
              const Text('Come on,China!'),
              const Text('Come on,Wuhan!'),
            ],
          ),

官方已經不鼓勵使用這種方法,另一種寫法為

body: GridView(
           gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
             crossAxisCount: 3,
             mainAxisSpacing: 2.0,
             crossAxisSpacing: 2.0,
             childAspectRatio: 0.75,
             ),
            children: <Widget>[
              new Image.network('http://img5.mtime.cn/mg/2019/10/02/105324.67493314_170X256X4.jpg',fit:BoxFit.cover),
              new Image.network('http://img5.mtime.cn/mg/2019/09/26/092514.83698073_170X256X4.jpg',fit:BoxFit.cover),
              new Image.network('http://img5.mtime.cn/mg/2019/11/07/111316.10093613_170X256X4.jpg',fit:BoxFit.cover),
              new Image.network('http://img5.mtime.cn/mg/2019/12/13/094432.64997517_170X256X4.jpg',fit:BoxFit.cover),
              new Image.network('http://img31.mtime.cn/mt/2014/02/22/230757.74994253_220X124X4.jpg',fit:BoxFit.cover),
              new Image.network('http://img5.mtime.cn/mg/2019/07/10/164947.40820910_170X256X4.jpg',fit:BoxFit.cover),
            ],
        ),
  • childAspectRatio:寬高比
  • mainAxisSpacing:橫向網格空檔 
  • crossAxisSpacing: 向縱向網格空擋

至此,使用組件的學習就到這兒了,下篇我們將學習佈局的相關知識!


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 微信公眾號: "Dotnet9" ,網站: "Dotnet9" ,問題或建議: "請網站留言" , 如果對您有所幫助: "歡迎贊賞" 。 簡化MVVM屬性設置和修改 .NET CORE(C ) WPF開發 閱讀導航 1. 常用類屬性設置、獲取方式 2. 二次封裝 INotifyPropertyCha ...
  • 前段時間看.net core 更更更新了,大家反應都挺好,想有機會也學習一下,正好這兩天要寫一個簡單的服務在centos上面跑,於是決定放棄使用java,直接.net core走起來,事情進行的非常順利. 但是我們的vps都是合作伙伴提供,有時候是centos7,有時候是centos6,雖然cent ...
  • 這個是C#5中的新特性,當遇到await時,會從線程池中取出一個線程非同步執行await等待的操作,然後方法立即返回。等非同步操作結束後回到await所在的地方接著往後執行。await需要等待async Task ...
  • using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System. ...
  • 首先 經過我測試,php74模塊沒有支持apache的.所以升級到php74 之後,php無法使用. 最基本的函數phpinfo 調用不出來,沒有相關的模塊. 安裝mariadb 10.4 之後發現,mariadb 這個版本好像不支持密碼(反正我是沒搞定,一天) .改配置什麼的我都試了,仍然免密登陸 ...
  • 前言 我不是名校畢業,更沒有大廠的背景,我只是一個畢業不到 2 年的普普通通的程式員,在摸爬滾打的工作這段時間里,深知了有一個「完整的知識體系」是非常重要的。當事人非常後悔沒有在大學期間知道這個道理…… 眾多大廠招人的需求也是非常註重此方面,畢竟我們不能單單隻是一個只會寫代碼的程式員,更應該成為一個 ...
  • 為了在伺服器上跑爬蟲,以及學SegNet,研究了一圈看來linux是必學品了。在自己電腦上安裝了一個 1。官網下載iso,一個linux dvd是穩定版,選之,另一個stream版是更新更快的測試版,裡面軟體更新。 https://www.centos.org/download/ 2。刻u盤,用Wi ...
  • 問題說明(環境:windows7,MySql8.0) 今天安裝好MySql後啟動MySql服務 啟動服務都失敗的就不要往下看了,自行百度解決。 打開客戶端秒退,但在cmd中是可以使用資料庫的。 正常來說只要能用就好了,但客戶端方便,就不想敲那一行代碼。就在網上找,百度,google,各種方法都試了。 ...
一周排行
    -Advertisement-
    Play Games
  • Dapr Outbox 是1.12中的功能。 本文只介紹Dapr Outbox 執行流程,Dapr Outbox基本用法請閱讀官方文檔 。本文中appID=order-processor,topic=orders 本文前提知識:熟悉Dapr狀態管理、Dapr發佈訂閱和Outbox 模式。 Outbo ...
  • 引言 在前幾章我們深度講解了單元測試和集成測試的基礎知識,這一章我們來講解一下代碼覆蓋率,代碼覆蓋率是單元測試運行的度量值,覆蓋率通常以百分比表示,用於衡量代碼被測試覆蓋的程度,幫助開發人員評估測試用例的質量和代碼的健壯性。常見的覆蓋率包括語句覆蓋率(Line Coverage)、分支覆蓋率(Bra ...
  • 前言 本文介紹瞭如何使用S7.NET庫實現對西門子PLC DB塊數據的讀寫,記錄了使用電腦模擬,模擬PLC,自至完成測試的詳細流程,並重點介紹了在這個過程中的易錯點,供參考。 用到的軟體: 1.Windows環境下鏈路層網路訪問的行業標準工具(WinPcap_4_1_3.exe)下載鏈接:http ...
  • 從依賴倒置原則(Dependency Inversion Principle, DIP)到控制反轉(Inversion of Control, IoC)再到依賴註入(Dependency Injection, DI)的演進過程,我們可以理解為一種逐步抽象和解耦的設計思想。這種思想在C#等面向對象的編 ...
  • 關於Python中的私有屬性和私有方法 Python對於類的成員沒有嚴格的訪問控制限制,這與其他面相對對象語言有區別。關於私有屬性和私有方法,有如下要點: 1、通常我們約定,兩個下劃線開頭的屬性是私有的(private)。其他為公共的(public); 2、類內部可以訪問私有屬性(方法); 3、類外 ...
  • C++ 訪問說明符 訪問說明符是 C++ 中控制類成員(屬性和方法)可訪問性的關鍵字。它們用於封裝類數據並保護其免受意外修改或濫用。 三種訪問說明符: public:允許從類外部的任何地方訪問成員。 private:僅允許在類內部訪問成員。 protected:允許在類內部及其派生類中訪問成員。 示 ...
  • 寫這個隨筆說一下C++的static_cast和dynamic_cast用在子類與父類的指針轉換時的一些事宜。首先,【static_cast,dynamic_cast】【父類指針,子類指針】,兩兩一組,共有4種組合:用 static_cast 父類轉子類、用 static_cast 子類轉父類、使用 ...
  • /******************************************************************************************************** * * * 設計雙向鏈表的介面 * * * * Copyright (c) 2023-2 ...
  • 相信接觸過spring做開發的小伙伴們一定使用過@ComponentScan註解 @ComponentScan("com.wangm.lifecycle") public class AppConfig { } @ComponentScan指定basePackage,將包下的類按照一定規則註冊成Be ...
  • 操作系統 :CentOS 7.6_x64 opensips版本: 2.4.9 python版本:2.7.5 python作為腳本語言,使用起來很方便,查了下opensips的文檔,支持使用python腳本寫邏輯代碼。今天整理下CentOS7環境下opensips2.4.9的python模塊筆記及使用 ...