Advertisement
Guest User

Untitled

a guest
Aug 21st, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. Container(
  2. height: 70.0,
  3. child: Row(
  4. children: <Widget>[
  5. Flexible(
  6. flex: 1,
  7. child: Padding(
  8. padding: const EdgeInsets.only(left: 8.0),
  9. child: TextField(
  10. controller: _searchController,
  11. textAlign: TextAlign.start,
  12. keyboardType: TextInputType.text),
  13. textInputAction: TextInputAction.send,),
  14. ),
  15. ),
  16. InkWell(
  17. child: Container(
  18. height: 40.0,
  19. width: 40.0,
  20. child: Icon(Icons.send, size: 25.0, color: Colors.black),
  21. ),
  22. onTap:_search,
  23. ),
  24. ],
  25. ),
  26. ),
  27. Expanded(
  28. child: FutureBuilder(
  29. future: _searchPost(),
  30. builder: (context, snapshot) {
  31. if(snapshot.hasData) {
  32. List<Contents> content = snapshot.data;
  33. ListView.separated(
  34. itemBuilder: (context, index) {
  35. return ListTile(
  36. title: Text(content[index].title),
  37. ),
  38. subtitle: Text(content[index].createdAt),
  39. ),
  40. );
  41. },
  42. itemCount: content.length,
  43. separatorBuilder: (context, index) {
  44. return Divider(height: 1.0);
  45. },
  46. );
  47. }else if (snapshot.hasError){
  48. print('error);
  49. }
  50.  
  51. return Center(
  52. child: CircularProgressIndicator(
  53. valueColor: AlwaysStoppedAnimation(Colors.blue),
  54. ),
  55. );
  56. }),
  57. )
  58.  
  59. Future<List<Contents>> _searchPost() async {
  60. final response = await http.get('${Constants.searchPosts}${_searchController.text}').timeout(Duration(seconds: 1));
  61. if(response.statusCode == 200){
  62. final responseString = json.decode(response.body) as List;
  63. List<Contents> res= responseString.map((j)=>Contents.fromJson(j)).toList();
  64.  
  65. print(json.decode(response.body));//<-- work fine and show result in console
  66. return res;
  67. }else{
  68. return null;
  69. }
  70. }
  71.  
  72. void _search() {
  73. if (_searchController.text.length <= 0) {
  74. }else{
  75. _searchPost();
  76. }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement