安卓商店在应用上架时会要求用户清楚并授权之后才可收集用户和设备信息,如mac地址 imei等
1、安卓开发者会在application中进行此操作,flutter应用可在main文件中添加授权对话框,
flutter的MyApp()可等同看作是应用的application入口。
在MyApp()中添加对话框,用户点击同意即进行部分三方插件的初始化,如极光、融云等。
可在MyApp()的build的body中添加判断,初次安装先处理授权框,同意后再正常初始化内容。
如果你的合规化对话框中没有用户协议和隐私政策,可不必将MyApp拆分为MyAppPrepare的子项。
代码如下:
void main() async{
await setupServiceLocator();//后台音频播放初始化
runApp(MultiProvider(
providers: [
ChangeNotifierProvider(create: (_) => UserProvider()),
],
child: MyAppPrepare(),//解决MaterialApp中的跳转路由的context问题
));
}
///解决MaterialApp中的跳转路由的context问题
class MyAppPrepare extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
builder: (context, widget) {
return MediaQuery(
///设置文字大小不随系统设置改变
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();
//添加观察者,检测页面生命周期
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();
//页面销毁时,移出页面生命周期监听者
WidgetsBinding.instance?.removeObserver(this);
}
@override
Widget build(BuildContext context) {
appContext = context;
return FutureBuilder<CacheManager>(
future: CacheManager.preInit(context),//进行数据初始化,极光初始化,如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); //显示用户授权框,应用合规化
return _widget;
},
);
}
_showAgreementWindow()方法未贴出,可自行编写一个你需要的样式。
为什么将MyApp()分为两个是因为你的合规化对话框中应该存在用户协议和隐私政策,这样做是为了将 MaterialApp 内容使用 StatelessWeight 包裹一层,解决Navigator operation requested with a context that does not include a Navigator.问题。即:使用路由(Navigator),根控件不能直接是 MaterialApp.