|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:flutter_app_cse/shared_constant.dart'; |
| 3 | +import 'package:flutter_app_cse/search_data_source.dart'; |
| 4 | +import 'package:url_launcher/url_launcher.dart'; |
| 5 | + |
| 6 | +class WebSearchResultCard extends StatelessWidget { |
| 7 | + const WebSearchResultCard( |
| 8 | + {@required this.searchResult, |
| 9 | + this.webSearchLayout = WebSearchLayout.CSE}); |
| 10 | + |
| 11 | + final SearchResult searchResult; |
| 12 | + final WebSearchLayout webSearchLayout; |
| 13 | + |
| 14 | + Widget _generateTitleTile(BuildContext context) { |
| 15 | + final ThemeData theme = Theme.of(context); |
| 16 | + return ListTile( |
| 17 | + title: Padding( |
| 18 | + padding: EdgeInsets.only(top: 6.0), |
| 19 | + child: Text( |
| 20 | + this.searchResult.result.title, |
| 21 | + style: theme.textTheme.headline.copyWith( |
| 22 | + fontSize: 15.0, fontWeight: FontWeight.bold, color: Colors.blue), |
| 23 | + ), |
| 24 | + ), |
| 25 | + subtitle: new Text( |
| 26 | + this.searchResult.result.link, |
| 27 | + style: |
| 28 | + theme.textTheme.body1.copyWith(fontSize: 14.0, color: Colors.green), |
| 29 | + ), |
| 30 | + ); |
| 31 | + } |
| 32 | + |
| 33 | + Widget _generateBodyTile(BuildContext context) { |
| 34 | + final ThemeData theme = Theme.of(context); |
| 35 | + bool haveThumbnail = this.searchResult.result.pagemap['thumbnail'] != null; |
| 36 | + if (!haveThumbnail) { |
| 37 | + return Container( |
| 38 | + padding: const EdgeInsets.only( |
| 39 | + left: 14.0, |
| 40 | + bottom: 8.0, |
| 41 | + ), |
| 42 | + child: Container( |
| 43 | + padding: const EdgeInsets.only(right: 10.0), |
| 44 | + child: Text( |
| 45 | + this.searchResult.result.snippet, |
| 46 | + style: theme.textTheme.body1, |
| 47 | + textAlign: TextAlign.left, |
| 48 | + )), |
| 49 | + ); |
| 50 | + } else { |
| 51 | + return Container( |
| 52 | + padding: const EdgeInsets.only( |
| 53 | + left: 15.0, |
| 54 | + bottom: 8.0, |
| 55 | + ), |
| 56 | + child: new Row(children: [ |
| 57 | + Image.network( |
| 58 | + this.searchResult.result.pagemap['thumbnail'][0]['src']), |
| 59 | + Expanded( |
| 60 | + child: Container( |
| 61 | + padding: const EdgeInsets.only(left: 10.0, right: 12.0), |
| 62 | + child: Text( |
| 63 | + this.searchResult.result.snippet, |
| 64 | + style: theme.textTheme.body1, |
| 65 | + textAlign: TextAlign.left, |
| 66 | + ))), |
| 67 | + ]), |
| 68 | + ); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + Widget _buildSimpleLayout(BuildContext context) { |
| 73 | + final ThemeData theme = Theme.of(context); |
| 74 | + return new Card( |
| 75 | + child: new Padding( |
| 76 | + padding: const EdgeInsets.all(8.0), |
| 77 | + child: new ListTile( |
| 78 | + leading: this.searchResult.result.pagemap['thumbnail'] != null |
| 79 | + ? new Image.network( |
| 80 | + this.searchResult.result.pagemap['thumbnail'][0]['src']) |
| 81 | + : null, |
| 82 | + title: new Text( |
| 83 | + this.searchResult.result.title, |
| 84 | + style: theme.textTheme.headline |
| 85 | + .copyWith(fontSize: 12.0, fontWeight: FontWeight.bold), |
| 86 | + ), |
| 87 | + subtitle: new Text( |
| 88 | + this.searchResult.result.snippet, |
| 89 | + style: theme.textTheme.body1.copyWith(fontSize: 12.0), |
| 90 | + ), |
| 91 | + ), |
| 92 | + )); |
| 93 | + } |
| 94 | + |
| 95 | + Widget _buildCSELayout(BuildContext context) { |
| 96 | + return new Container( |
| 97 | + decoration: new BoxDecoration(boxShadow: [ |
| 98 | + new BoxShadow( |
| 99 | + color: Colors.grey, |
| 100 | + blurRadius: 1.0, |
| 101 | + ), |
| 102 | + ]), |
| 103 | + child: new Card( |
| 104 | + child: Column( |
| 105 | + children: [ |
| 106 | + _generateTitleTile(context), |
| 107 | + new Divider(color: Colors.black26), |
| 108 | + _generateBodyTile(context), |
| 109 | + ], |
| 110 | + ), |
| 111 | + ), |
| 112 | + ); |
| 113 | + } |
| 114 | + |
| 115 | + Widget _buildFromLayout(BuildContext context) { |
| 116 | + switch (this.webSearchLayout) { |
| 117 | + case WebSearchLayout.simple: |
| 118 | + return _buildSimpleLayout(context); |
| 119 | + case WebSearchLayout.CSE: |
| 120 | + return _buildCSELayout(context); |
| 121 | + default: |
| 122 | + return new Text('Invalid Layout For WebSearchResult!'); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + @override |
| 127 | + Widget build(BuildContext context) { |
| 128 | + return new GestureDetector( |
| 129 | + onTap: () async { |
| 130 | + if (await canLaunch(searchResult.result.link)) { |
| 131 | + await launch(searchResult.result.link); |
| 132 | + } |
| 133 | + }, |
| 134 | + child: _buildFromLayout(context)); |
| 135 | + } |
| 136 | +} |
0 commit comments