Advertisement
Guest User

Untitled

a guest
Feb 28th, 2020
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 3.09 KB | None | 0 0
  1. class HomePage extends StatelessWidget {
  2.   @override
  3.   Widget build(BuildContext context) {
  4.     return Scaffold(
  5.       appBar: AppBar(
  6.         title: Text('Users'),
  7.       ),
  8.       body: BlocBuilder<UserBloc, UserState>(
  9.         builder: (context, state) => state.when(
  10.           loading: _onLoading,
  11.           loaded: (users) => _onLoaded(users, context),
  12.           failed: (message) => _onFailed(message, context),
  13.         ),
  14.       ),
  15.     );
  16.   }
  17.  
  18.   Widget _onLoading() => const Center(
  19.         child: CircularProgressIndicator(),
  20.       );
  21.  
  22.   Widget _onLoaded(List<User> users, BuildContext context) => RefreshIndicator(
  23.         onRefresh: () async => context.bloc<UserBloc>().add(Refresh()),
  24.         child: ListView.separated(
  25.           separatorBuilder: (context, _) => Divider(height: 0),
  26.           itemCount: users.length,
  27.           itemBuilder: (context, index) => _ListItem(users[index]),
  28.         ),
  29.       );
  30.  
  31.   Widget _onFailed(String message, BuildContext context) => Center(
  32.         child: Column(
  33.           mainAxisAlignment: MainAxisAlignment.center,
  34.           children: [
  35.             Text(
  36.               message,
  37.               style: TextStyle(fontSize: 20.0),
  38.             ),
  39.             SizedBox(height: 10),
  40.             RaisedButton.icon(
  41.               color: Colors.deepPurple,
  42.               textColor: Colors.white,
  43.               onPressed: () async => context.bloc<UserBloc>().add(Refresh()),
  44.               icon: Icon(Icons.refresh),
  45.               label: Text('TRY AGAIN'),
  46.             ),
  47.           ],
  48.         ),
  49.       );
  50. }
  51.  
  52. class _ListItem extends StatelessWidget {
  53.   final User user;
  54.  
  55.   const _ListItem(this.user);
  56.  
  57.   @override
  58.   Widget build(BuildContext context) {
  59.     return ExpansionTile(
  60.       key: ValueKey(user.id),
  61.       leading: CircleAvatar(
  62.         child: Text(
  63.           _nameToInitials(user.name),
  64.         ),
  65.       ),
  66.       title: Text(user.name),
  67.       subtitle: Text(user.company.name),
  68.       children: [
  69.         _textRow('Street', user.address.street),
  70.         _textRow('Suite', user.address.suite),
  71.         _textRow('City', user.address.city),
  72.         _textRow('Zip Code', user.address.zipCode),
  73.         _textRow(
  74.             'Coordinates', '${user.address.geo.lat}, ${user.address.geo.lng}'),
  75.       ],
  76.     );
  77.   }
  78.  
  79.   Widget _textRow(String key, String value) => Padding(
  80.         padding: const EdgeInsets.only(left: 20.0, bottom: 8.0),
  81.         child: Align(
  82.           alignment: Alignment.centerLeft,
  83.           child: Text.rich(
  84.             TextSpan(
  85.               text: '$key: ',
  86.               style: TextStyle(color: Colors.deepPurple),
  87.               children: [
  88.                 TextSpan(
  89.                   text: value,
  90.                   style: TextStyle(
  91.                     fontWeight: FontWeight.bold,
  92.                     color: Colors.grey[800],
  93.                   ),
  94.                 ),
  95.               ],
  96.             ),
  97.           ),
  98.         ),
  99.       );
  100.  
  101.   String _nameToInitials(String name) {
  102.     final spiltName = name.split(' ');
  103.     return '${spiltName.first.substring(0, 1)}${spiltName.last.substring(0, 1)}';
  104.   }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement