【发布时间】:2020-06-03 13:11:21
【问题描述】:
我正在构建一个用于学习的天气应用程序,并且在应用程序开始时我想获取用户位置。闪屏是无状态的小部件,不做任何事情。当位置可用时,我想推送到另一个屏幕。
我可以做到,但这是正确的做法吗(请查看下面的代码)? 让我知道任何其他解决方案。
在从 build 方法返回小部件之前,我调用了 getLocation 方法。这是理想的做法吗?因为这个屏幕状态不会更新。
class LaunchScreen extends StatelessWidget {
static const String id = 'launch_screen';
void getLocation(BuildContext context) async {
LocationData location = await LocationService().getCurrentLocation();
if (location != null) {
// Go to home
print(location.latitude);
print(location.longitude);
} else {
// Error fetching location
}
}
@override
Widget build(BuildContext context) {
getLocation(context);
return Scaffold(
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Expanded(
child: Container(),
),
Center(
child: Image(
image: AssetImage('images/launch_screen/app_logo.png'),
),
),
SizedBox(
height: 8,
),
Center(
child: Shimmer.fromColors(
baseColor: Colors.white.withOpacity(0.6),
highlightColor: Colors.white,
child: Text(
'Forecasts',
style: GoogleFonts.montserratAlternates(
textStyle:
TextStyle(fontSize: 60, fontWeight: FontWeight.w300),
color: Colors.white.withOpacity(0.6)),
),
),
),
Expanded(
child: Container(),
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'Made with ❤️ ',
style: TextStyle(
fontSize: 12,
fontWeight: FontWeight.w300,
color: Colors.white.withOpacity(0.5)),
),
Text(
'Parth Adroja',
style: TextStyle(
fontSize: 12,
fontWeight: FontWeight.w500,
color: Colors.white.withOpacity(0.5)),
),
],
),
SizedBox(
height: 20,
)
],
),
);
}
}
【问题讨论】: