Advertisement
rifki_cs29

restaurant_page

Jun 23rd, 2021
1,176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 2.09 KB | None | 0 0
  1. part of 'pages.dart';
  2.  
  3. class RestaurantPage extends StatelessWidget {
  4.   static const routeName = '/restaurant_page';
  5.  
  6.   Widget _buildListRestaurant(BuildContext context) {
  7.     Future<String> _loadAsset(BuildContext context) async {
  8.       return await DefaultAssetBundle.of(context)
  9.           .loadString('assets/local_restaurant.json');
  10.     }
  11.  
  12.     Future<List<RestaurantItem>> _loadRestaurants(BuildContext context) async {
  13.       String jsonString = await _loadAsset(context);
  14.       return parseJson(jsonString).restaurants;
  15.     }
  16.  
  17.     return FutureBuilder(
  18.       future: _loadRestaurants(context),
  19.       builder: (context, snapshot) {
  20.         final List<RestaurantItem> restaurants =
  21.             snapshot.data as List<RestaurantItem>;
  22.         if (restaurants.isEmpty || (restaurants.length == 0)) {
  23.           return _buildNullRestaurant(context);
  24.         } else {
  25.           return _buildListRecomended(context, restaurants);
  26.         }
  27.       },
  28.     );
  29.   }
  30.  
  31.   @override
  32.   Widget build(BuildContext context) {
  33.     return CustomScaffold(body: _buildListRestaurant(context));
  34.   }
  35.  
  36.   Widget _buildListRecomended(
  37.       BuildContext context, List<RestaurantItem> restaurants) {
  38.     return Padding(
  39.       padding: EdgeInsets.only(top: 85, bottom: 12),
  40.       child: ListView.builder(
  41.         itemBuilder: (context, index) {
  42.           final recomendedRestaurant = restaurants[index];
  43.           return Padding(
  44.             padding: EdgeInsets.only(left: 16, right: 16),
  45.             child: GestureDetector(
  46.                 onTap: () {
  47.                   Navigator.pushNamed(context, DetailRestaurant.routeName,
  48.                       arguments: recomendedRestaurant);
  49.                 },
  50.                 child: RestaurantListItem(restaurant: recomendedRestaurant)),
  51.           );
  52.         },
  53.         itemCount: restaurants.length,
  54.       ),
  55.     );
  56.   }
  57.  
  58.   Widget _buildNullRestaurant(BuildContext context) {
  59.     return Stack(
  60.       children: [
  61.         Center(
  62.             child: Lottie.asset('assets/nodata.json',
  63.                 width: 250, height: 250, fit: BoxFit.fill))
  64.       ],
  65.     );
  66.   }
  67. }
  68.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement