1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
| import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:flutter/material.dart'; import 'package:netflix_clone_lecture_note/model/model_movie.dart'; import 'package:netflix_clone_lecture_note/screen/detail_screen.dart';
class SearchScreen extends StatefulWidget { _SearchScreenState createState() => _SearchScreenState(); }
class _SearchScreenState extends State<SearchScreen> { final TextEditingController _filter = TextEditingController(); FocusNode focusNode = FocusNode(); String _searchText = "";
_SearchScreenState() { _filter.addListener(() { setState(() { _searchText = _filter.text; }); }); }
Widget _buildBody(BuildContext context) { return StreamBuilder<QuerySnapshot>( stream: Firestore.instance.collection('movie').snapshots(), builder: (context, snapshot) { if (!snapshot.hasData) return LinearProgressIndicator(); return _buildList(context, snapshot.data.documents); }, ); }
Widget _buildList(BuildContext context, List<DocumentSnapshot> snapshot) { List<DocumentSnapshot> searchResults = []; for (DocumentSnapshot d in snapshot) { if (d.data.toString().contains(_searchText)) { searchResults.add(d); } } return Expanded( child: GridView.count( crossAxisCount: 3, childAspectRatio: 1 / 1.5, padding: EdgeInsets.all(3), children: searchResults .map((data) => _buildListItem(context, data)) .toList()), ); }
Widget _buildListItem(BuildContext context, DocumentSnapshot data) { final movie = Movie.fromSnapshot(data); return InkWell( child: Image.network(movie.poster), onTap: () { Navigator.of(context).push(MaterialPageRoute<Null>( fullscreenDialog: true, builder: (BuildContext context) { return DetailScreen(movie: movie); })); }, ); }
@override Widget build(BuildContext context) { return Container( child: Column( children: <Widget>[ Padding( padding: EdgeInsets.all(30), ), Container( color: Colors.black, padding: EdgeInsets.fromLTRB(5, 10, 5, 10), child: Row( children: <Widget>[ Expanded( flex: 6, child: TextField( focusNode: focusNode, style: TextStyle( fontSize: 15, ), autofocus: true, controller: _filter, decoration: InputDecoration( filled: true, fillColor: Colors.white12, prefixIcon: Icon( Icons.search, color: Colors.white60, size: 20, ), suffixIcon: focusNode.hasFocus ? IconButton( icon: Icon( Icons.cancel, size: 20, ), onPressed: () { setState(() { _filter.clear(); _searchText = ""; }); }, ) : Container(), hintText: '검색', labelStyle: TextStyle(color: Colors.white), focusedBorder: OutlineInputBorder( borderSide: BorderSide(color: Colors.transparent), borderRadius: BorderRadius.all(Radius.circular(10))), enabledBorder: OutlineInputBorder( borderSide: BorderSide(color: Colors.transparent), borderRadius: BorderRadius.all(Radius.circular(10))), border: OutlineInputBorder( borderSide: BorderSide(color: Colors.transparent), borderRadius: BorderRadius.all(Radius.circular(10))), ), ), ), focusNode.hasFocus ? Expanded( child: FlatButton( child: Text('취소'), onPressed: () { setState(() { _filter.clear(); _searchText = ""; focusNode.unfocus(); }); }, ), ) : Expanded( flex: 0, child: Container(), ) ], ), ), _buildBody(context) ], ), ); } }
|