Advertisement
Thiagoccaufes

admob

Jan 27th, 2021
8
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.80 KB | None | 0 0
  1. import 'dart:convert';
  2.  
  3. import 'package:buscador_gifs/ui/GifPage.dart';
  4. import 'package:flutter/cupertino.dart';
  5. import 'package:flutter/material.dart';
  6. import 'package:http/http.dart' as http;
  7. import 'package:share/share.dart';
  8. import 'package:transparent_image/transparent_image.dart';
  9. class HomePage extends StatefulWidget {
  10. @override
  11. _HomePageState createState() => _HomePageState();
  12. }
  13.  
  14. class _HomePageState extends State<HomePage> {
  15. String _search;
  16. int _offset=0;
  17. int getCount(List data){
  18. if(_search==null){
  19. return data.length;
  20. }else{
  21. return data.length+1;
  22. }
  23. }
  24. Future<Map> getGifs() async{
  25. http.Response response;
  26. if(_search==null)
  27. response = await http.get("https://api.giphy.com/v1/gifs/trending?api_key=YkQKvSJPeMj1YgxyGcvz7pLpAYpz6Y37&limit=20&rating=g");
  28. else
  29. response = await http.get("https://api.giphy.com/v1/gifs/search?api_key=YkQKvSJPeMj1YgxyGcvz7pLpAYpz6Y37&q=$_search&limit=19&offset=$_offset&rating=g&lang=en");
  30. return json.decode(response.body);
  31. }
  32. @override
  33. Widget build(BuildContext context) {
  34. return Scaffold(
  35. appBar: AppBar(
  36. backgroundColor: Colors.black,
  37.  
  38. title: Image.network("https://developers.giphy.com/static/img/dev-logo-lg.7404c00322a8.gif"),
  39. centerTitle: true,
  40. ),
  41. backgroundColor: Colors.black,
  42. body:Column(
  43. children: <Widget>[
  44. Padding(padding: EdgeInsets.all(10.0),
  45. child: TextField(
  46. decoration: InputDecoration(
  47. labelText: "Pesquise Aqui",
  48. labelStyle: TextStyle(color: Colors.white),
  49. border: OutlineInputBorder()
  50. ),
  51. style: TextStyle(color: Colors.white, fontSize: 18.0),
  52. textAlign: TextAlign.center,
  53. onSubmitted: (text){
  54. setState(() {
  55. _search=text;
  56. _offset = 0;
  57. });
  58. }
  59. ),
  60. ),
  61. Expanded(
  62. child: FutureBuilder(
  63. future: getGifs(),
  64. builder: (context, snapshot){
  65. switch(snapshot.connectionState){
  66. case ConnectionState.waiting:
  67. case ConnectionState.none:
  68. return Container(
  69. width: 200.0,
  70. height: 200.0,
  71. alignment: Alignment.center,
  72. child: CircularProgressIndicator(
  73. valueColor: AlwaysStoppedAnimation<Color>(Colors.white) ,
  74. strokeWidth: 5.0,
  75. ),
  76. );
  77. default:
  78. if(snapshot.hasError) return Container();
  79. else return _createGiffTable(context, snapshot);
  80. return Container();
  81. }
  82. }
  83. )
  84. )
  85.  
  86. ],
  87.  
  88. ),
  89. );
  90. }
  91. Widget _createGiffTable(BuildContext context, AsyncSnapshot snapshot){
  92. return GridView.builder(
  93. padding: EdgeInsets.all(10.0),
  94. gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
  95. crossAxisCount: 2,
  96. crossAxisSpacing: 10.0,
  97. mainAxisSpacing: 10.0,
  98. ),
  99. itemCount: getCount(snapshot.data["data"]),
  100. itemBuilder: (context, index){
  101. if(_search==null || index < snapshot.data["data"].length)
  102. return GestureDetector(
  103. child: FadeInImage.memoryNetwork(
  104. placeholder: kTransparentImage,
  105. image: snapshot.data["data"][index]["images"]["fixed_height"]["url"],
  106. height: 300.0,
  107. fit: BoxFit.cover,
  108. ),
  109.  
  110. onTap: (){
  111. Navigator.push(context,
  112. MaterialPageRoute(
  113. builder: (context)=>GifPage(snapshot.data["data"][index]))
  114. );
  115.  
  116. },
  117. onLongPress:(){
  118. Share.share(snapshot.data["data"][index]["images"]["fixed_height"]["url"]);
  119. },
  120. );
  121. else
  122. return Container(
  123. child: GestureDetector(
  124. child: Column(
  125. children: [
  126. Icon(Icons.add, color:Colors.white, size:70.0),
  127. Text("Carregar mais",
  128. style: TextStyle(color: Colors.white, fontSize: 22.0) )
  129. ],
  130. ),
  131. onTap: (){
  132. setState(() {
  133. _offset+=19;
  134. });
  135.  
  136. },
  137. )
  138. );
  139. });
  140. }
  141. @override
  142. void initState(){
  143. super.initState();
  144. getGifs().then(
  145. (map){
  146. print(map);
  147. }
  148. );
  149. }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement