我正在尝试创建一个初始屏幕,并且在加载初始屏幕时,我想从本地存储中加载用户的首选项并获取用户的地理位置。
我该怎么做?下面是我尝试过的示例代码。
下面是我的 main.dart 文件。
<%= form_with scope: :article, url: articles_path, local: true do |form| %>
<% if @article.errors.any? %>
<div id='error_explanation'>
<h2>
<%= pluralize(@article.errors.count, "error") %> prohibited this articles from being saved:
</h2>
<ul>
<% @article.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<%= form.label :title %><br>
<%= form.text_field :title %>
</p>
<p>
<%= form.label :text %><br>
<%= form.text_area :text %>
</p>
<p>
<%= form.submit %>
<% end %>
<%= link_to 'Back', articles_path %>
下面是我的 global.dart 文件。
import 'package:flutter/material.dart';
import 'package:my_products/page/home.dart';
import "./component/choice.dart";
import "global.dart";
void main(){
runApp(
new MaterialApp(
home: SplashScreen(),
debugShowCheckedModeBanner: false,
debugShowMaterialGrid: false,
)
);
}
class SplashScreen extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return SplashScreenState();
}
}
class SplashScreenState extends State<SplashScreen> with SingleTickerProviderStateMixin{
bool loaded = false;
AnimationController _animationController;
Animation _animation;
getUserLangPref(){
String language;
// print(identical(Global(), Global()));
// print(Global().location);
// Global().prefStorage.ready.then((_){
language = Global().language;
loaded = (language != null) ? true : false;
Future.delayed(const Duration(milliseconds: 2000), ()=> afterPrefLoaded());
// });
}
@override
void initState() {
super.initState();
_animationController = new AnimationController(
vsync: this, duration: Duration(milliseconds: 800));
_animation = Tween(begin: 0.0, end: 1.0).animate(CurvedAnimation(
parent: _animationController, curve: Curves.easeInCirc));
_animationController.forward();
Future.wait([Global().ready]);
print("======");
print(Global().location);
print("======");
Global().ready.then((_) => getUserLangPref());
Global().ready.then((_) => print(Global().location));
print("======");
}
afterPrefLoaded() async {
if (loaded) {
Navigator.of(context).pushReplacement(
MaterialPageRoute(builder: (context) => MyHome()));
} else {
Navigator.of(context).pushReplacement(
MaterialPageRoute(builder: (context) => LanguageChoice()));
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
body: FadeTransition(
opacity: _animation,
child: Center(
child:
SizedBox(height: 250.0, child: Image.asset('images/my.jpg')))));
}
}