Last active
January 5, 2021 16:27
-
-
Save dnys1/72637a382c6a4206d6a716f3869b111c to your computer and use it in GitHub Desktop.
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'; | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: ListScreen(), | |
); | |
} | |
} | |
class ListScreen extends StatelessWidget { | |
Future<bool> isLoggedIn() async { | |
// can do all login check logic here safely | |
await Future<void>.delayed(const Duration(seconds: 3)); | |
return false; | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar(), | |
body: Center( | |
child: FutureBuilder( | |
future: isLoggedIn(), | |
builder: (context, snapshot) { | |
if (!snapshot.hasData) { | |
return CircularProgressIndicator(); | |
} | |
final loggedIn = snapshot.data; | |
if (!loggedIn) { | |
// do not push/pop in build method (i.e. while building a frame) | |
// schedule it for the next frame | |
WidgetsBinding.instance.addPostFrameCallback((_) { | |
Navigator.of(context).pushReplacement( | |
MaterialPageRoute( | |
builder: (BuildContext _) => LoginScreen(), | |
), | |
); | |
}); | |
// return an empty screen while we transition | |
return Container(); | |
} | |
return ListItemView(); | |
}, | |
), | |
), | |
); | |
} | |
} | |
class ListItemView extends StatelessWidget { | |
Future<List<String>> _loadItems() async { | |
// could return anything here/ call as many async functions as needed | |
await Future<void>.delayed(const Duration(seconds: 3)); | |
return ['a', 'b', 'c', 'd']; | |
} | |
@override | |
Widget build(BuildContext context) { | |
return FutureBuilder( | |
future: _loadItems(), | |
builder: (context, snapshot) { | |
if (!snapshot.hasData) { | |
return CircularProgressIndicator(); | |
} | |
final items = snapshot.data; | |
return Padding( | |
padding: const EdgeInsets.all(8.0), | |
child: ListView( | |
children: [ | |
for (var item in items) Text(item), | |
], | |
), | |
); | |
}, | |
); | |
} | |
} | |
class LoginScreen extends StatelessWidget { | |
final emailController = TextEditingController(); | |
final passwordController = TextEditingController(); | |
Future<String> login(String email, String password) async { | |
// do login stuff here, then return session id | |
return ''; | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar(), | |
body: Center( | |
child: Padding( | |
padding: const EdgeInsets.all(20), | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
crossAxisAlignment: CrossAxisAlignment.center, | |
children: [ | |
Text('Login'), | |
TextFormField( | |
controller: emailController, | |
), | |
TextFormField( | |
controller: passwordController, | |
), | |
IconButton( | |
onPressed: () async { | |
print(emailController.text); | |
print(passwordController.text); | |
String sessionID = await login( | |
emailController.text, passwordController.text); | |
print("button pressed, sessionID: $sessionID"); | |
Navigator.pushReplacement( | |
context, | |
MaterialPageRoute(builder: (context) => ListScreen()), | |
); | |
}, | |
icon: Icon(Icons.login), | |
), | |
], | |
), | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment