Created
August 17, 2020 19:21
-
-
Save ilopX/b40ddfbec97355269b915633391ea45e to your computer and use it in GitHub Desktop.
login
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:provider/provider.dart'; | |
void main() { | |
runApp(MultiProvider(providers: [ | |
ChangeNotifierProvider<AuthProvider>( | |
create: (_) => AuthProvider(), | |
), | |
], child: MyApp())); | |
} | |
class MyApp extends StatelessWidget { | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'My Poor App', | |
debugShowCheckedModeBanner: false, | |
theme: ThemeData( | |
primaryColor: Color(0xff29c17e), | |
visualDensity: VisualDensity.adaptivePlatformDensity, | |
), | |
home: LoginScreen(), | |
onGenerateRoute: (settings) { | |
if (settings.name == '/logout') { | |
return MaterialPageRoute(builder: (context) => LoginScreen()); | |
} else if (settings.name == '/home') { | |
return MaterialPageRoute(builder: (context) => HomeScreen()); | |
} else if (settings.name == '/category') { | |
return MaterialPageRoute(builder: (context) => CategoryScreen()); | |
} else { | |
throw UnimplementedError('Url not fount'); | |
} | |
}, | |
); | |
} | |
} | |
class LoginScreen extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
body: Center( | |
child: Consumer<AuthProvider>(builder: (context, auth, child) { | |
if (auth.isBusy) { | |
return CircularProgressIndicator(); | |
} else { | |
return MaterialButton( | |
onPressed: () async { | |
if (await auth.isLogin('[email protected]', 'pass')) { | |
Navigator.pushReplacementNamed(context, '/home'); | |
} else { | |
// print some auth error | |
} | |
}, | |
child: Text('Login'), | |
); | |
} | |
}), | |
), | |
); | |
} | |
} | |
class HomeScreen extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
final auth = Provider.of<AuthProvider>(context, listen: false); | |
return Scaffold( | |
body: Center( | |
child: MaterialButton( | |
elevation: 2, | |
onPressed: () { | |
Navigator.pushNamed(context, '/category'); | |
}, | |
child: Text('Reset')), | |
), | |
); | |
} | |
} | |
class AuthProvider extends ChangeNotifier { | |
bool _isAuthenticated = false; | |
bool get isAuthenticated => _isAuthenticated; | |
bool _isBusy = false; | |
bool get isBusy => _isBusy; | |
Future<bool> isLogin(String email, String password) async { | |
_isBusy = true; | |
notifyListeners(); | |
await Future.delayed(Duration(seconds: 1)); | |
_isAuthenticated = true; | |
notifyListeners(); | |
_isBusy = false; | |
return true; | |
} | |
Future logout() async { | |
_isBusy = true; | |
notifyListeners(); | |
await Future.delayed(Duration(seconds: 1)); | |
_isAuthenticated = false; | |
notifyListeners(); | |
_isBusy = false; | |
} | |
} | |
class CategoryScreen extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
body: Center( | |
child: Consumer<AuthProvider>( | |
builder: (context, auth, child) { | |
if (auth.isBusy) { | |
return CircularProgressIndicator(); | |
} else { | |
return RaisedButton( | |
onPressed: () async { | |
await auth.logout(); | |
Navigator.pushReplacementNamed(context, '/logout'); | |
// print(auth.isAuthenticated); | |
}, | |
child: Text('Category Logout'), | |
); | |
} | |
}, | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment