Flutter學習筆記(41)--自定義Dialog實現版本更新彈窗

来源:https://www.cnblogs.com/upwgh/archive/2020/07/23/13367589.html
-Advertisement-
Play Games

功能點: 1.更新彈窗UI 2.強更與非強更且別控制 3.屏蔽物理返回鍵(因為強更的時候點擊返回鍵,彈窗會消失) 4.點擊彈窗外透明區域時,彈窗不消失 ...


如需轉載,請註明出處:Flutter學習筆記(41)--自定義Dialog實現版本更新彈窗

功能點:

1.更新彈窗UI

2.強更與非強更且別控制

3.屏蔽物理返回鍵(因為強更的時候點擊返回鍵,彈窗會消失)

4.點擊彈窗外透明區域時,彈窗不消失

先看下效果圖:

 

Dialog實現代碼:

import 'package:flutter/material.dart';
import 'package:xiaopijiang/utils/assets_util.dart';
import 'package:xiaopijiang/utils/toast_util.dart';

///created by WGH
///on 2020/7/23
///description:版本更新提示彈窗
class UpdateDialog extends Dialog {
  final String upDateContent;
  final bool isForce;

  UpdateDialog({this.upDateContent, this.isForce});

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Container(
            width: 319,
            height: 370,
            child: Stack(
              children: <Widget>[
                Image.asset(
                  AssetsUtil.getImagePath(
                      imageName: 'bg_update', suffix: 'png'),
                  fit: BoxFit.cover,
                ),
                Container(
                  width: double.infinity,
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: <Widget>[
                      Container(
                        margin: EdgeInsets.only(top: 110),
                        child: Text('發現新版本',
                            style: TextStyle(
                                fontSize: 20,
                                color: Colors.white,
                                decoration: TextDecoration.none)),
                      ),
                      Text(upDateContent,
                          style: TextStyle(
                              fontSize: 16,
                              color: Colors.black54,
                              decoration: TextDecoration.none)),
                      Container(
                        width: 250,
                        height: 42,
                        margin: EdgeInsets.only(bottom: 15),
                        child: RaisedButton(
                            color: Colors.red,
                            shape: StadiumBorder(),
                            child: Text(
                              '立即更新',
                              style:
                                  TextStyle(fontSize: 20, color: Colors.white),
                            ),
                            onPressed: () {
                              ToastUtil.showTips('下載apk');
                            }),
                      )
                    ],
                  ),
                ),
              ],
            ),
          ),
          GestureDetector(
            onTap: () {
              Navigator.pop(context);
            },
            child: Offstage(
              offstage: isForce,
              child: Container(
                  margin: EdgeInsets.only(top: 30),
                  child: Image.asset(
                    AssetsUtil.getImagePath(
                        imageName: 'ic_update_close', suffix: 'png'),
                    width: 35,
                    height: 35,
                  )),
            ),
          )
        ],
      ),
    );
  }

  static showUpdateDialog(
      BuildContext context, String mUpdateContent, bool mIsForce) {
    return showDialog(
        barrierDismissible: false,
        context: context,
        builder: (BuildContext context) {
          return WillPopScope(
              child: UpdateDialog(
                  upDateContent: mUpdateContent, isForce: mIsForce),onWillPop: _onWillPop);
        });
  }

  static Future<bool> _onWillPop() async{
    return false;
  }
}

調用Dialog:

  _checkUpdate() async{
    int serviceVersionCode = 101;
    PackageInfo packageInfo = await PackageInfo.fromPlatform();
    //獲取當前的版本號
    int currentVersionCode = int.parse(packageInfo.version.replaceAll('.', ''));
    //如果獲取伺服器的版本號比當前應用程式的版本號還高,那麼提示升級
    if (serviceVersionCode > currentVersionCode) {
      if(Platform.isAndroid){
        //Android平臺在應用內進行更新
        //彈出"版本更新"的對話框
        UpdateDialog.showUpdateDialog(context, '1.修複已知bug\n2.優化用戶體驗', false);
      }else if(Platform.isIOS){
        //iOS平臺跳轉道AppStore進行更新
      }
    }
  }

重點說明:

屏蔽物理返回鍵(因為強更的時候點擊返回鍵,彈窗會消失)

 

barrierDismissible: false,

 

4.點擊彈窗外透明區域時,彈窗不消失

 

          return WillPopScope(
              child: UpdateDialog(
                  upDateContent: mUpdateContent, isForce: mIsForce),
              onWillPop: _onWillPop);

  static Future<bool> _onWillPop() async {
    return false;
  }

 


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

-Advertisement-
Play Games
更多相關文章
  • 在Zabbix Server伺服器上安裝oracle-instantclient11.2後,結果使用sqlplus命令時遇到“sqlplus: error while loading shared libraries: libnsl.so.1: cannot open shared object f... ...
  • 本文更新於2020-05-03,使用MySQL 5.7,操作系統為Deepin 15.4。 許可權 許可權存取需要用到mysql庫中user、db、host、tables_priv、columns_prvi這幾個許可權表。列分為4個部分:用戶列、許可權列、安全列、資源控制列。許可權列又分為普通許可權和管理許可權。 ...
  • 開始之前明確一下死鎖和鎖等待這兩個事件的異同相同的之處:兩者都是當前事物在試圖請求被其他事物已經占用的鎖,從而造成當前事物無法執行的現象不同的之處:死鎖是相關session雙方或者多方中必然要犧牲(回滾)至少一個事務,否則雙方(或者多方)都無法執行;鎖等待則不然,對於暫時無法申請到的鎖,嘗試持續地“ ...
  • MHA(Master HA)是一款開源的 MySQL 的高可用程式,它為 MySQL 主從複製架構提供了 automating master failover 功能。MHA 在監控到 master 節點故障時,會提升其中擁有最新數據的 slave 節點成為新的master 節點,在此期間,MHA 會 ...
  • 原文地址:https://www.citusdata.com/blog/2019/02/08/the-most-useful-postgres-extension-pg-stat-statements/Postgresql的Extensions能夠延伸,更改和推進Postgres的行為。怎麼樣?通過 ...
  • 一、隱士intent,緊接著上一次複習 得到第二個活動返回的數據之後,我們需要重寫onActivityResult()來獲取數據 protected void onActivityResult(int requestCode,int resultCode,Intent data){ switch(r ...
  • 請輸入有效的11位數字手機號碼,或者包含區號的11位或12位數字座機號碼 出現這個問題按照提示上的來看應該是手機號碼輸入的不對 但是輸入的手機號碼的確是11位 這個手機號是開發者賬號綁定的手機號 也就是這個開發者賬號的蘋果ID綁定的電子郵箱和綁定的手機號碼 解決方案就是在手機上登錄上開發者的那個賬號 ...
  • 首先刪除 SceneDelegate.h 和 SceneDelegate.m ,info.plist中的 Application Scene Manifest 和 AppDelegate.m 中的 #pragma mark - UISceneSession lifecycle - (UISceneC ...
一周排行
    -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模塊筆記及使用 ...