Skip to content

Instantly share code, notes, and snippets.

@dnys1
Created October 24, 2020 16:57
Show Gist options
  • Save dnys1/00c50e79db2b8298c5d45f007fef5624 to your computer and use it in GitHub Desktop.
Save dnys1/00c50e79db2b8298c5d45f007fef5624 to your computer and use it in GitHub Desktop.
πŸš€ Flutter Challenges: Problem 1
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