Created
October 24, 2020 00:46
-
-
Save dnys1/49cf9f5fff3db93e1cc131252eb2e658 to your computer and use it in GitHub Desktop.
π Flutter Challenges: Problem 1
This file contains hidden or 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 UserService { | |
static const _dummyUsers = ['John', 'Mike', 'Jill', 'Steve']; | |
Future<List<String>> getUsers() async { | |
await Future<void>.delayed(const Duration(seconds: 5)); | |
return _dummyUsers; | |
} | |
} | |
class MyApp extends StatelessWidget { | |
static final userService = UserService(); | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'π Flutter Challenges', | |
debugShowCheckedModeBanner: false, | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: HomePage(), | |
routes: { | |
'/second': (_) => SecondPage(userService), | |
}, | |
); | |
} | |
} | |
class HomePage extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text('π Flutter Challenges'), | |
), | |
body: Center( | |
child: RaisedButton( | |
child: Text('Go to Users'), | |
onPressed: () { | |
Navigator.of(context).pushNamed('/second'); | |
}, | |
), | |
), | |
); | |
} | |
} | |
class SecondPage extends StatefulWidget { | |
final UserService userService; | |
SecondPage(this.userService); | |
@override | |
State<SecondPage> createState() => _SecondPageState(); | |
} | |
class _SecondPageState extends State<SecondPage> { | |
List<String> _users; | |
@override | |
void initState() { | |
super.initState(); | |
widget.userService.getUsers().then((users) { | |
setState(() => _users = users); | |
}); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text('π Flutter Challenges'), | |
), | |
body: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
if (_users == null) ...const [ | |
Text('Loading users...'), | |
SizedBox(height: 20), | |
CircularProgressIndicator(), | |
] else | |
..._users.map((user) => Text(user)), | |
const SizedBox(height: 20), | |
RaisedButton( | |
child: const Text('Back'), | |
onPressed: () { | |
Navigator.of(context).pop(); | |
}, | |
), | |
], | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment