/
SearchResultItem.dart
139 lines (125 loc) · 3.64 KB
/
SearchResultItem.dart
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
//Imports
import 'package:flutter/material.dart';
import 'package:client/apiInterface.dart';
import 'package:client/equationOverview.dart';
import 'package:flutter_tex/flutter_tex.dart';
import 'dart:convert';
import 'package:math_keyboard/math_keyboard.dart';
/*
NOTE
################################################################################
This forms the template of the returned results of the search from the home page
################################################################################
*/
//Code
class SearchResultItem extends StatefulWidget {
const SearchResultItem(
{Key? key,
required this.equation,
required this.conf_score,
required this.problemID})
: super(key: key);
final String equation, conf_score;
final int problemID;
@override
State<SearchResultItem> createState() => _SearchResultItemState();
}
class _SearchResultItemState extends State<SearchResultItem> {
bool isColored = false;
bool isLoggedIn = false;
bool saved = false;
bool removed = false;
checkIsSaved(int pid) async {
List<dynamic> savedResults = await apiObj.getSavedResults();
if (savedResults != null && savedResults.isNotEmpty) {
for (int i = 0; i < savedResults.length; i++) {
if (savedResults[i]['id'] == pid) {
return true;
}
}
return false;
} else {
return false;
}
}
checkIsLoggedIn() {
return apiObj.getIsLoggedIn();
}
@override
void initState() async {
super.initState();
isLoggedIn = checkIsLoggedIn();
if (isLoggedIn) {
isColored = await checkIsSaved(widget.problemID);
}
}
@override
Widget build(BuildContext context) {
return Card(
margin: const EdgeInsets.fromLTRB(50.0, 10.0, 50.0, 0),
child: Padding(
padding: const EdgeInsets.fromLTRB(10.0, 10.0, 10.0, 10.0),
child: ListTile(
onTap: goToEquation,
title: Text(
widget.equation,
style: TextStyle(
letterSpacing: 2.0,
wordSpacing: 4.5,
fontSize: 24.0,
),
),
subtitle: Text(
'Confidence Rating: ${widget.conf_score}',
style: TextStyle(
letterSpacing: 1.0,
wordSpacing: 2.5,
),
),
leading: (isLoggedIn)
? IconButton(
onPressed: saveToFavourites,
icon: (isColored)
? Icon(Icons.star, color: Colors.amberAccent)
: Icon(Icons.star_border_outlined),
)
: null,
trailing: Icon(Icons.arrow_forward_ios),
),
),
);
}
void saveToFavourites() {
setState(() async {
isColored = !isColored;
if (isColored) {
saved = await apiObj.addSavedResult(widget.problemID);
} else {
//removed = await apiObj.removeSavedResult(widget.problemID);
}
});
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: (saved || removed)
? Text('Yay! Success!')
: Text('Woops, Something went wrong...'),
width: 400,
behavior: SnackBarBehavior.floating,
duration: const Duration(milliseconds: 1500),
padding: EdgeInsets.all(10),
));
}
void goToEquation() {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => EquationOverview(
equation: widget.equation, problemID: widget.problemID)));
}
}
/*
For Rendering Properly
TeXView(
child: TeXViewDocument(widget.equation),
renderingEngine: TeXViewRenderingEngine.mathjax(),
),
*/