habitat.dart 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. import 'package:flutter/material.dart';
  2. import 'package:google_fonts/google_fonts.dart';
  3. import 'package:quiosco_soler_2/catalogo/models/submodelos.dart';
  4. import 'package:quiosco_soler_2/catalogo/pages/detalles_producto.dart';
  5. import '../providers/submodelos_provider.dart';
  6. class Habitat extends StatefulWidget {
  7. final String modelo;
  8. final List<String> modelos;
  9. const Habitat({Key? key, required this.modelo, required this.modelos}) : super(key: key);
  10. @override
  11. State<Habitat> createState() => _HabitatState();
  12. }
  13. class _HabitatState extends State<Habitat> {
  14. final _submodelosProvider = SubmodelosProvider();
  15. bool _isLoading = true;
  16. bool _searching = false;
  17. late Submodelos _submodelos;
  18. late List<String> _modelos;
  19. late String _modelo;
  20. @override
  21. void initState() {
  22. super.initState();
  23. init();
  24. }
  25. void init() async{
  26. await fetchSubmodelos(widget.modelo);
  27. _modelos = widget.modelos;
  28. _modelo = widget.modelo;
  29. final index = _modelos.indexOf(_modelo);
  30. _modelos.removeAt(index);
  31. _modelos.insert(0, widget.modelo);
  32. setState(() => _isLoading = false);
  33. }
  34. Future<void> fetchSubmodelos(modelo) async{
  35. final submodelos = await _submodelosProvider.fetchSubmodelos('Hábitat', modelo);
  36. _submodelos = submodelos;
  37. setState(() => _searching = false);
  38. }
  39. @override
  40. Widget build(BuildContext context) {
  41. final _screenSize = MediaQuery.of(context).size;
  42. return Scaffold(
  43. appBar: AppBar(),
  44. body: SafeArea(
  45. child: _isLoading ? const Center(
  46. child: CircularProgressIndicator(),
  47. ) : Column(
  48. children: [
  49. Container(
  50. width: _screenSize.width,
  51. alignment: Alignment.center,
  52. child: Stack(
  53. children: [
  54. Image.asset(
  55. 'assets/habitat_header.jpg',
  56. width: _screenSize.width,
  57. fit: BoxFit.cover,
  58. ),
  59. Positioned(
  60. bottom: 46.0,
  61. left: 28.0,
  62. child: Text(
  63. 'Producto',
  64. style: GoogleFonts.roboto(
  65. fontSize: 12.0,
  66. color: Colors.white,
  67. fontWeight: FontWeight.w300,
  68. shadows: [
  69. const Shadow(
  70. offset: Offset(1.0, 1.0),
  71. blurRadius: 3.0,
  72. color: Colors.black87,
  73. ),
  74. ],
  75. ),
  76. ),
  77. ),
  78. Positioned(
  79. bottom: 16.0,
  80. left: 28.0,
  81. child: Text(
  82. 'Hábitat',
  83. style: GoogleFonts.roboto(
  84. fontSize: 22.0,
  85. color: Colors.white,
  86. fontWeight: FontWeight.w300,
  87. shadows: [
  88. const Shadow(
  89. offset: Offset(1.0, 1.0),
  90. blurRadius: 3.0,
  91. color: Colors.black87,
  92. ),
  93. ],
  94. ),
  95. ),
  96. )
  97. ],
  98. ),
  99. ),
  100. Container(
  101. margin: const EdgeInsets.all(8.0),
  102. width: _screenSize.width,
  103. height: 48.0,
  104. child: ListView.separated(
  105. scrollDirection: Axis.horizontal,
  106. itemCount: _modelos.length,
  107. itemBuilder: (context, index) => TextButton(
  108. style: TextButton.styleFrom(
  109. primary: _modelos[index] == _modelo ? const Color(0xFFB40404) : Colors.transparent,
  110. shape: RoundedRectangleBorder(
  111. borderRadius: BorderRadius.circular(28.0),
  112. side: BorderSide(
  113. color: _modelos[index] == _modelo ? const Color(0xFFB40404) : Colors.black26,
  114. width: 2.0,
  115. ),
  116. )
  117. ),
  118. child: Text(
  119. _modelos[index],
  120. style: GoogleFonts.roboto(
  121. color: _modelo == _modelos[index] ? const Color(0xFFB40404) : Colors.black,
  122. ),
  123. ),
  124. onPressed: () async{
  125. setState(() {
  126. _modelo = _modelos[index];
  127. _searching = true;
  128. });
  129. await fetchSubmodelos(_modelo);
  130. },
  131. ),
  132. separatorBuilder: (context, index) => const SizedBox(width: 8.0),
  133. ),
  134. ),
  135. Expanded(
  136. child: _searching ? const Center(
  137. child: CircularProgressIndicator(),
  138. ) : GridView.count(
  139. crossAxisCount: 2,
  140. shrinkWrap: true,
  141. children: List.generate(_submodelos.result!.length, (index) => TextButton(
  142. child: Column(
  143. crossAxisAlignment: CrossAxisAlignment.stretch,
  144. children: [
  145. Expanded(
  146. child: FadeInImage(
  147. placeholder: const AssetImage('assets/progressbar.gif'),
  148. image: NetworkImage('http://www.solerpalau.mx/ALP/${_submodelos.result![index].id}.jpg'),
  149. fit: BoxFit.fitHeight,
  150. ),
  151. ),
  152. Container(
  153. padding: const EdgeInsets.all(4.0),
  154. child: Text(
  155. _submodelos.result![index].submodelo!,
  156. textAlign: TextAlign.center,
  157. style: GoogleFonts.roboto(
  158. fontWeight: FontWeight.bold,
  159. color: const Color.fromRGBO(102, 102, 102, 1),
  160. fontSize: 16.0,
  161. ),
  162. ),
  163. )
  164. ],
  165. ),
  166. onPressed: () => Navigator.push(context, MaterialPageRoute(
  167. builder: (context) => DetallesProducto(
  168. linea: 'Hábitat',
  169. modelo: _modelo,
  170. submodelo: _submodelos.result![index].submodelo!,
  171. ),
  172. )),
  173. style: TextButton.styleFrom(
  174. primary: Colors.black12,
  175. padding: EdgeInsets.zero,
  176. tapTargetSize: MaterialTapTargetSize.shrinkWrap,
  177. minimumSize: const Size(0.0, 0.0),
  178. ),
  179. )),
  180. ),
  181. )
  182. ],
  183. ),
  184. ),
  185. );
  186. }
  187. }