flutter應(yīng)用的發(fā)布合規(guī)化處理

安卓商店在應(yīng)用上架時(shí)會(huì)要求用戶清楚并授權(quán)之后才可收集用戶和設(shè)備信息,如mac地址 imei等

1、安卓開發(fā)者會(huì)在application中進(jìn)行此操作,flutter應(yīng)用可在main文件中添加授權(quán)對(duì)話框,

flutter的MyApp()可等同看作是應(yīng)用的application入口。

在MyApp()中添加對(duì)話框,用戶點(diǎn)擊同意即進(jìn)行部分三方插件的初始化,如極光、融云等。

可在MyApp()的build的body中添加判斷,初次安裝先處理授權(quán)框,同意后再正常初始化內(nèi)容。

如果你的合規(guī)化對(duì)話框中沒有用戶協(xié)議和隱私政策,可不必將MyApp拆分為MyAppPrepare的子項(xiàng)。

代碼如下:


void main() async{

? await setupServiceLocator();//后臺(tái)音頻播放初始化

? runApp(MultiProvider(

? ? providers: [

? ? ? ChangeNotifierProvider(create: (_) => UserProvider()),

? ? ],

? ? child: MyAppPrepare(),//解決MaterialApp中的跳轉(zhuǎn)路由的context問題

? ));

}

///解決MaterialApp中的跳轉(zhuǎn)路由的context問題

class MyAppPrepare extends StatelessWidget {

? @override

? Widget build(BuildContext context) {

? ? return MaterialApp(

? ? ? debugShowCheckedModeBanner: false,

? ? ? builder: (context, widget) {

? ? ? ? return MediaQuery(

? ? ? ? ? ///設(shè)置文字大小不隨系統(tǒng)設(shè)置改變

? ? ? ? ? data: MediaQuery.of(context).copyWith(textScaleFactor: 1.0),

? ? ? ? ? child: FlutterEasyLoading(child: widget),

? ? ? ? );

? ? ? },

? ? ? home: MyApp(),

? ? ? theme: ThemeData(primarySwatch: ColorTool.white),

? ? ? localizationsDelegates: [? ?//國際化

? ? ? ? RefreshLocalizations.delegate,

? ? ? ? GlobalMaterialLocalizations.delegate,

? ? ? ? GlobalWidgetsLocalizations.delegate,

? ? ? ? CupertinoLocalizationsDelegate(),

? ? ? ],

? ? ? supportedLocales: [Locale('zh', 'CN')],

? ? );

? }

}

class MyApp extends StatefulWidget {

? @override

? State<StatefulWidget> createState() {

? ? return _MyAppState();

? }

? static BuildContext getContext() {

? ? return _MyAppState.getContext();

? }

}

class _MyAppState extends State<MyApp> with WidgetsBindingObserver{

? RouteManager _routeDelegate = RouteManager();

static BuildContext appContext;

? static BuildContext getContext() {

? ? return appContext;

? }

? @override

? void initState() {

? ? super.initState();

? ? //添加觀察者,檢測(cè)頁面生命周期

? ? WidgetsBinding.instance.addObserver(this);

? ? EventBusUtils _event = EventBusUtils.instance;

? ? if (CacheManager.shared.prefs != null && CacheManager.shared.getString("isShowAgreement") != null) {

? ? ? _prepareInitIM(); //初始化融云IM

? ? }

? }

? @override

? void dispose() {

? ? super.dispose();

? ? //頁面銷毀時(shí),移出頁面生命周期監(jiān)聽者

? ? WidgetsBinding.instance?.removeObserver(this);

? }

? @override

? Widget build(BuildContext context) {

? ? appContext = context;

? ? return FutureBuilder<CacheManager>(

? ? ? future: CacheManager.preInit(context),//進(jìn)行數(shù)據(jù)初始化,極光初始化,如SharedPreferences

? ? ? builder: (BuildContext context, AsyncSnapshot<CacheManager> snapshot) {

? ? ? ? var _widget = (snapshot.connectionState == ConnectionState.done &&

? ? ? ? ? ? ? ? snapshot.data != null)

? ? ? ? ? ? ? Router(routerDelegate: _routeDelegate)

? ? ? ? ? ? : snapshot.connectionState != ConnectionState.done

? ? ? ? ? ? ? Scaffold(

? ? ? ? ? ? ? ? ? ? body: Center(

? ? ? ? ? ? ? ? ? ? ? child: CircularProgressIndicator(color: ColorTool.red),

? ? ? ? ? ? ? ? ? ? ),

? ? ? ? ? ? ? ? ? )

? ? ? ? ? ? ? ? :_showAgreementWindow(appContext);? //顯示用戶授權(quán)框,應(yīng)用合規(guī)化

? ? ? ? return _widget;

? ? ? },

? ? );

? }


_showAgreementWindow()方法未貼出,可自行編寫一個(gè)你需要的樣式。

為什么將MyApp()分為兩個(gè)是因?yàn)槟愕暮弦?guī)化對(duì)話框中應(yīng)該存在用戶協(xié)議和隱私政策,這樣做是為了將?MaterialApp 內(nèi)容使用 StatelessWeight 包裹一層,解決Navigator operation requested with a context that does not include a Navigator.問題。即:使用路由(Navigator),根控件不能直接是 MaterialApp.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容