嗨,我想让我的用户在他们第一次登录后保持登录状态,直到他们自己注销为止。
这是您第一次打开应用程序时的屏幕。
import requests
from bs4 import BeautifulSoup
page = requests.get("https://rl.insider.gg/en/xbox")
soup = BeautifulSoup(page.content, 'html.parser')
trendingitems = soup.find(id="trendingItems")
for link in trendingitems.find_all('a'):
linkitems = (link.get('href'))
print(linkitems)
这是您使用该帐户登录时的屏幕登录页面。
Future<void> main() async {
runApp(new MaterialApp(
debugShowCheckedModeBanner: false,
home: new MyApp()));
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => new _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return new SplashScreen(
seconds: 5,
navigateAfterSeconds: new AfterSplash(),
imageBackground: AssetImage('assets/picture/splash.png'),
loaderColor: Colors.red,
loadingText: Text('Ver 2.0'),
);
}
}
class AfterSplash extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Sizer(
builder: (context, orientation, screenType) {
return Scaffold(
body: FloatingNavBar(
color: Colors.red[400],
items: [
FloatingNavBarItem(
iconData: Icons.home,
title: 'Home',
page: HomeScreen(),
),
FloatingNavBarItem(
iconData: Icons.lock_clock,
title: 'Riwayat',
page: Bill(),
),
FloatingNavBarItem(
iconData: Icons.local_library,
title: 'Info',
page: Info(),
)
],
selectedIconColor: Colors.white,
hapticFeedback: true,
horizontalPadding: 60,
),
);
},
);
}
}
这是您输入用户名和密码的屏幕
class SPAL extends StatefulWidget {
@override
_SPALState createState() => new _SPALState();
}
class _SPALState extends State<SPAL> {
@override
Widget build(BuildContext context) {
return new SplashScreen(
seconds: 5,
navigateAfterSeconds: new AfterSplash(),
imageBackground: AssetImage('assets/picture/splash.png'),
loaderColor: Colors.red,
loadingText: Text('Ver 2.0'),
);
}
}
class AfterSplash extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Sizer(
builder: (context, orientation, screenType) {
return Scaffold(
body: FloatingNavBar(
color: Colors.red[400],
items: [
FloatingNavBarItem(
iconData: Icons.home,
title: 'Home',
page: HSAL(),
),
FloatingNavBarItem(
iconData: Icons.lock_clock,
title: 'Riwayat',
page: Bill(),
),
FloatingNavBarItem(
iconData: Icons.local_library,
title: 'Info',
page: Info(),
)
],
selectedIconColor: Colors.white,
hapticFeedback: true,
horizontalPadding: 60,
),
);
},
);
}
}
TextEditingController emailController = new TextEditingController();
TextEditingController pwdController = new TextEditingController();
class LoginScreen extends StatefulWidget {
@override
_LoginScreenState createState() => _LoginScreenState();
}
class _LoginScreenState extends State<LoginScreen> {
final GlobalKey<FormState> _emailing = GlobalKey();
final GlobalKey<FormState> _passwd = GlobalKey();
String password = '';
bool isPasswordVisible = false;
bool _isLoading = false;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.red[400],
centerTitle: true,
title: Text('Login'),
),
body: SingleChildScrollView(
child: Container(
margin: EdgeInsets.symmetric(vertical: 2.h, horizontal: 2.h),
child: Column(
children: [
Text(
'Masuk',
style: TextStyle(fontSize: 15.sp),
),
Padding(
padding: EdgeInsets.symmetric(vertical: 1.h),
child: Form(
key: _emailing,
child: TextFormField(
controller: emailController,
decoration: InputDecoration(
hintText: 'name@gmail.com/08XX-XXXX-XXXX',
labelText: 'Email/Nomor Hp',
// icon: Icon(Icons.mail),
suffixIcon: emailController.text.isEmpty
? Container(width: 0)
: IconButton(
icon: Icon(Icons.close),
onPressed: () => emailController.clear(),
),
border: OutlineInputBorder(),
focusColor: Colors.red),
keyboardType: TextInputType.emailAddress,
textInputAction: TextInputAction.done,
// autofocus: true,
validator: (value) => value == null || value.isEmpty
? "Masukan Email atau Nomor Hp Anda"
: null),
),
),
Padding(
padding: EdgeInsets.symmetric(vertical: 1.h),
child: Form(
key: _passwd,
child: TextFormField(
controller: pwdController,
onChanged: (value) =>
setState(() => this.password = value),
decoration: InputDecoration(
hintText: 'Password',
labelText: 'Password',
suffixIcon: IconButton(
icon: isPasswordVisible
? Icon(Icons.visibility_off)
: Icon(Icons.visibility),
onPressed: () => setState(
() => isPasswordVisible = !isPasswordVisible),
),
border: OutlineInputBorder()),
obscureText: isPasswordVisible,
validator: (value) => value == null || value.isEmpty
? "Masukan Password Anda"
: null),
),
),
FlatButton(
child: Text(
'Lupa Password',
style: TextStyle(color: Colors.grey),
),
onPressed: () {
Navigator.push(
context, MaterialPageRoute(builder: (_) => LupaPass()));
}),
ButtonWidget(
text: 'Submit',
onClicked: () async {
if (_emailing.currentState.validate() &&
_passwd.currentState.validate()) {
LG();
Navigator.pushAndRemoveUntil(
context,
MaterialPageRoute(builder: (_) => SPAL()),
(Route<dynamic> route) => false,
);
}
},
),
],
),
),
),
);
}
}
TextEditingController emailController = new TextEditingController();
TextEditingController pwdController = new TextEditingController();
class HomeScreen extends StatefulWidget {
@override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: SingleChildScrollView(
child: Container(
child: Column(children: <Widget>[
Stack(children: <Widget>[
Container(
child: Center(
child: Stack(
children: [
SizedBox(
height: 30.0.h,
width: 100.0.w,
child: DecoratedBox(
decoration: BoxDecoration(
color: Colors.red[400],
borderRadius: BorderRadius.only(
bottomLeft: Radius.circular(20),
bottomRight: Radius.circular(20),
),
),
child: Opacity(
opacity: 0.2,
child: ClipRRect(
child: Image.asset(
'assets/picture/indonesia.png',
fit: BoxFit.cover,
),
),
),
),
)
],
),
),
),
Padding(
padding: EdgeInsets.symmetric(vertical: 22.5.h),
child: Center(
child: SizedBox(
height: 11.0.h,
width: 95.0.w,
child: DecoratedBox(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(15),
boxShadow: [
BoxShadow(
color: Colors.black.withOpacity(0.3),
spreadRadius: 3,
blurRadius: 7,
offset: Offset(0, 0), // changes position of shadow
),
],
),
),
),
),
),
Padding(
padding: EdgeInsets.symmetric(vertical: 4.0.h),
child: Container(
child: Image.asset(
'assets/Logo/iu.png',
height: 15.h,
width: 29.w,
),
),
),
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
Padding(
padding:
EdgeInsets.symmetric(vertical: 6.0.h, horizontal: 2.5.h),
child: SizedBox(
width: 60,
height: 60,
child: DecoratedBox(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(30)),
child: Center(
child: IconButton(
icon: Icon(Icons.person),
iconSize: 31,
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (_) => LoginScreen()));
}),
),
),
),
)
],
),
Container(
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Padding(
padding: EdgeInsets.symmetric(
vertical: 17.5.h, horizontal: 3.0.h),
child: Row(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Text('Indonesia ',
style: GoogleFonts.inter(
fontWeight: FontWeight.bold,
fontSize: 20.sp,
color: Colors.white,
shadows: [
Shadow(
color: Colors.black,
offset: Offset(-1.0, -1.0),
),
Shadow(
color: Colors.black,
offset: Offset(1.0, -1.0),
),
Shadow(
color: Colors.black,
offset: Offset(1.0, 1.0),
),
Shadow(
color: Colors.black,
offset: Offset(-1.0, 1.0),
),
])),
Text(
'Bisa',
style: GoogleFonts.inter(
fontWeight: FontWeight.bold,
fontSize: 20.sp,
color: Colors.white,
shadows: [
Shadow(
color: Colors.black,
offset: Offset(-1.0, -1.0),
),
Shadow(
color: Colors.black,
offset: Offset(1.0, -1.0),
),
Shadow(
color: Colors.black,
offset: Offset(1.0, 1.0),
),
Shadow(
color: Colors.black,
offset: Offset(-1.0, 1.0),
),
]),
)
],
)),
],
),
),
// Menu(),
MLabel(),
ICarousel(),
Container(
child: Padding(
padding: EdgeInsets.only(top: 56.0.h, left: 2.0.h, right: 2.0.h),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Sorotan',
style: TextStyle(color: Colors.red, fontSize: 20),
),
Text('Berita dan artikel yang sedang menarik baru - baru ini')
],
),
)),
GridHome()
])
])),
));
}
}
答案 0 :(得分:2)
使用 shared_preferences 是一个选项,步骤如下:
这里有一些资源可以帮助shared_preferences
:
更新:
我们需要两种方法,您可以在新文件中定义它们并调用它 localService.dart
例如:
//this is for saving a variable that means has logged in succefully
Future<void> save_loggedIn() async {
final prefs = await SharedPreferences.getInstance();
prefs.setString("loggedIn", "true");
}
//this is for checking the saved variable to check if the user is already logged in or not
Future<String> read_loggedIn() async {
final prefs = await SharedPreferences.getInstance();
var value = prefs.getString("loggedIn");
return value;
}
现在在主屏幕重定向基于检查 shared_prefrences 中的登录变量:
Future<void> main() async {
runApp(new MaterialApp(
debugShowCheckedModeBanner: false,
home: (read_loggedIn() == "true") ? new SPAL (): new MyApp()));
}
现在在您的日志记录屏幕中添加此行以将变量保存在 shared_prefrences
中:
ButtonWidget(
text: 'Submit',
onClicked: () async {
if (_emailing.currentState.validate() &&
_passwd.currentState.validate()) {
// store the variable to remember that this user is logged in
save_loggedIn();
LG();
Navigator.pushAndRemoveUntil(
context,
MaterialPageRoute(builder: (_) => SPAL()),
(Route<dynamic> route) => false,
);
}
},
),
第二次更新:
我忘了告诉你应该在 pubspec.yaml 文件中添加 shared_prefrences
包:
版本:1.0.0+1
environment:
sdk: ">=2.7.0 <3.0.0"
dependencies:
flutter:
sdk: flutter
shared_preferences:
答案 1 :(得分:0)
那么如何做,只需在登录按钮上添加此代码:
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setString('email', emailController.text);
所以它看起来像这样:
ButtonWidget(
text: 'Submit',
onClicked: () async {
if (_emailing.currentState.validate() &&
_passwd.currentState.validate()) {
LG();
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setString('email', emailController.text);
Navigator.pushAndRemoveUntil(
context,
MaterialPageRoute(builder: (_) => SPAL()),
(Route<dynamic> route) => false,
);
}
},
),
这个在 main.dart 上
WidgetsFlutterBinding.ensureInitialized();
SharedPreferences prefs = await SharedPreferences.getInstance();
var email = prefs.getString('email');
它看起来像这样:
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
SharedPreferences prefs = await SharedPreferences.getInstance();
var email = prefs.getString('email');
runApp(new MaterialApp(
debugShowCheckedModeBanner: false,
home: email == null ? new MyApp (): new SPAL()));
}