【发布时间】:2021-02-15 04:50:21
【问题描述】:
import 'dart:convert';
import 'package:flutter/widgets.dart';
import 'package:flutter_app/home_page.dart';
import 'package:flutter_app/login_page.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:http/http.dart' as http;
import 'package:shared_preferences/shared_preferences.dart';
import 'package:geolocator/geolocator.dart';
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
String name;
@override
void initState() {
super.initState();
}
Future <void>loadPref()async{
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
return Future.delayed(Duration(seconds: 1),(){
return name=sharedPreferences.getString("useFullName");
});
}
logout()async{
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
sharedPreferences.clear();
sharedPreferences.commit();
Navigator.of(context).pushAndRemoveUntil(MaterialPageRoute(builder: (BuildContext context) => LoginPage()), (Route<dynamic> route) => false);
}
var locationMessege= "";
void getCurrentLocation()async{
var position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
var lastPosition=await Geolocator.getLastKnownPosition();
print(lastPosition);
setState(() {
locationMessege="$position.latitude,$position.longitude";
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor:Colors.orangeAccent,
title: FutureBuilder(
future: loadPref(),
builder: (context, snapshot) {
if(name==null){
return Text("Loading");
}else{
return Text("$name");
}
},
),
actions: <Widget>[
FlatButton(
onPressed: () {
logout();
},
child: Text("Log Out", style: TextStyle(color: Colors.white)),
),
],
),
body: Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage("assets/bg1.png"), fit: BoxFit.cover)),
child: Center(
child: Container(
padding: EdgeInsets.all(30),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Column(
children:<Widget> [
Container(
width: 130.0,
height: 130.0,
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage("assets/solalogo2.png"), fit: BoxFit.cover))
),
Text("GPS Location",style: TextStyle(
color: Colors.black,
fontSize: 30.0,
fontWeight: FontWeight.bold)),
Icon(Icons.location_on,
size: 46,
color: Colors.yellow,),
Text("Position:$locationMessege",style:TextStyle(
color: Colors.black,
fontSize: 20.0,
fontWeight: FontWeight.bold)),
FlatButton(onPressed:(){
getCurrentLocation();
},
color: Colors.orange,
child: Text("Get New Location",
style: TextStyle(
color: Colors.black,
),))
],
),
],
),
),
),
),
);
}
}
成功登录后,它被定向到这个主页。我想在appbar 标题上加载用户名,但它总是触发加载语句并且在我关闭应用程序并再次加载之前不加载名称。当您重新打开应用程序时,如果用户之前登录,您将被定向到主页。我是 Flutter 的新手,这是我使用它的第二周,任何有用的提示和建议将不胜感激。
【问题讨论】:
-
欢迎,用户登录后是否可以将用户名带到主屏幕?
-
它是否符合您的要求。?那就更好了。
标签: flutter user-interface flutter-futurebuilder