Last active
February 25, 2024 21:23
-
-
Save edwvilla/f8d18f65d885d0f5ce68f2351ee1b2ad 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 'dart:developer'; | |
import 'package:flutter/material.dart'; | |
class CounterNotifier with ChangeNotifier { | |
int _count = 0; | |
int get value => _count; | |
void increment() { | |
_count++; | |
notifyListeners(); | |
} | |
} | |
class CounterInheritedNotifier extends InheritedNotifier<CounterNotifier> { | |
const CounterInheritedNotifier({ | |
super.key, | |
required Widget child, | |
required CounterNotifier notifier, | |
}) : super(child: child, notifier: notifier); | |
static CounterInheritedNotifier? of(BuildContext context) { | |
return context | |
.dependOnInheritedWidgetOfExactType<CounterInheritedNotifier>(); | |
} | |
} |
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 'dart:developer'; | |
import 'package:appcurso/models/product.dart'; | |
import 'package:appcurso/modules/home/controller/home_controller.dart'; | |
import 'package:appcurso/modules/home/widgets/product_card_widget.dart'; | |
import 'package:appcurso/services/inherited/inherited_api.dart'; | |
import 'package:flutter/material.dart'; | |
import 'package:get/get.dart'; | |
class HomeRoute extends StatelessWidget { | |
const HomeRoute({super.key}); | |
@override | |
Widget build(BuildContext context) { | |
final HomeController controller = Get.put(HomeController()); | |
return Scaffold( | |
floatingActionButton: const CounterButton(), | |
body: SafeArea( | |
child: FutureBuilder( | |
future: controller.getProducts(), | |
builder: (context, snapshot) { | |
if (snapshot.connectionState == ConnectionState.waiting) { | |
return const Center( | |
child: CircularProgressIndicator(), | |
); | |
} | |
if (snapshot.hasError || | |
!snapshot.hasData || | |
snapshot.data == null) { | |
return const Center( | |
child: Text("No hay datos"), | |
); | |
} | |
final List<Product> productList = snapshot.data!; | |
return Padding( | |
padding: const EdgeInsets.symmetric( | |
horizontal: 20, | |
), | |
child: GridView.builder( | |
itemCount: productList.length, | |
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount( | |
crossAxisCount: 2, | |
crossAxisSpacing: 20, | |
mainAxisSpacing: 10, | |
childAspectRatio: 3 / 4, // height / width | |
), | |
itemBuilder: (ctx, index) => ProductCard( | |
product: productList[index], | |
), | |
), | |
); | |
}), | |
), | |
); | |
} | |
} | |
class CounterButton extends StatelessWidget { | |
const CounterButton({ | |
super.key, | |
}); | |
@override | |
Widget build(BuildContext context) { | |
final counter = CounterInheritedNotifier.of(context)!.notifier!; | |
return FloatingActionButton( | |
onPressed: () { | |
counter.increment(); | |
log(counter.value.toString()); | |
}, | |
child: Text("${counter.value}"), | |
); | |
} | |
} |
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 'dart:developer'; | |
import 'package:appcurso/models/product.dart'; | |
import 'package:appcurso/modules/login/controller/login_controller.dart'; | |
import 'package:appcurso/services/api_service.dart'; | |
import 'package:flutter/material.dart'; | |
import 'package:get/get.dart'; | |
class InheritedApi extends InheritedWidget { | |
InheritedApi({ | |
Key? key, | |
required Widget child, | |
}) : super(key: key, child: child); | |
@override | |
bool updateShouldNotify(covariant InheritedWidget oldWidget) => true; | |
static InheritedApi of(BuildContext context) { | |
return context.dependOnInheritedWidgetOfExactType<InheritedApi>()!; | |
} | |
final apiService = ApiService(); | |
Future<List<Product>?> getProducts() async { | |
final token = Get.find<LoginController>().userCredential?.jwt; | |
try { | |
if (token == null) { | |
throw Exception("El usuario no está autenticado"); | |
} | |
return await apiService.getProducts(token: token); | |
} catch (e) { | |
log(e.toString()); | |
return null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment