Advertisement
akagi92

Untitled

Mar 31st, 2020
495
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.34 KB | None | 0 0
  1. import 'dart:async';
  2. import 'package:ebook/Utils/AppConfig.dart';
  3. import 'package:ebook/Utils/network_util.dart';
  4. import 'package:ebook/maps/CategoryBooksMaps.dart';
  5. import 'package:ebook/maps/LectureDiscMaps.dart';
  6. import 'package:ebook/maps/CourseMaps.dart';
  7. import 'package:ebook/model/LectureDiscussionModel.dart';
  8. import 'package:ebook/model/DefModel.dart';
  9.  
  10. import 'package:ebook/ui/home/CourseRow.dart';
  11. import 'package:ebook/ui/planet_detail/DiscLectureDetail.dart';
  12. import 'package:flutter/material.dart';
  13. import 'package:ebook/Theme.dart' as Theme;
  14. import 'package:shared_preferences/shared_preferences.dart';
  15. import 'package:flutter_html/flutter_html.dart';
  16.  
  17. class DiscCourses extends StatefulWidget {
  18. final String idCorse, idLecture;
  19.  
  20. DiscCourses(this.idCorse, this.idLecture);
  21.  
  22. @override
  23. _DiscCoursesState createState() => _DiscCoursesState();
  24. }
  25.  
  26. class _DiscCoursesState extends State<DiscCourses> {
  27. SharedPreferences sharedPreferences;
  28. Icon _searchIcon = new Icon(Icons.search);
  29. Widget _appBarTitle = new Text('Discussion Lecture');
  30. NetworkUtil _networkUtil;
  31. AppConfig _ac;
  32. static List<LectureDiscussionModel> _listdisc = <LectureDiscussionModel>[];
  33. ScrollController scrollController = new ScrollController();
  34.  
  35. bool isloading = false;
  36. var search;
  37. String page = '1';
  38. String title = '';
  39.  
  40. DefModel def;
  41.  
  42. @override
  43. void initState() {
  44. super.initState();
  45. _networkUtil = new NetworkUtil();
  46. CategoryBooksMaps.list.clear();
  47. _listdisc.clear();
  48. this.listenfordisc(page);
  49. scrollController.addListener(() {
  50. if (scrollController.position.pixels ==
  51. scrollController.position.maxScrollExtent) {
  52. int np = int.parse(page)+1;
  53. page = np.toString();
  54. //print("AHAHAHA");
  55. listenfordisc(page);
  56. }
  57. });
  58. }
  59.  
  60. @override
  61. void dispose() {
  62. scrollController.dispose();
  63. super.dispose();
  64. }
  65.  
  66. void listenfordisc(page) async {
  67. sharedPreferences = await SharedPreferences.getInstance();
  68. String token = sharedPreferences.getString("token");
  69. _networkUtil.discussLecture(token, widget.idCorse, widget.idLecture, page, '');
  70. Timer(Duration(milliseconds: 3000), () {
  71. _listdisc.addAll(LectureDiscMaps.list);
  72. setState(() {
  73. isloading = true;
  74.  
  75. });
  76. });
  77. }
  78.  
  79. void addDiscuss(title, content) async {
  80. sharedPreferences = await SharedPreferences.getInstance();
  81. String token = sharedPreferences.getString("token");
  82. def = await _networkUtil.addDiscuss(token, widget.idCorse, widget.idLecture, title, content);
  83. if(def.status == true) {
  84. _listdisc.clear();
  85. listenfordisc('');
  86. }
  87. }
  88.  
  89. Future<String> _asyncInputDialog(BuildContext context) async {
  90. int _rating = 0;
  91. String title, content;
  92. return showDialog<String>(
  93. context: context,
  94. barrierDismissible: false,
  95. // dialog is dismissible with a tap on the barrier
  96. builder: (BuildContext context) {
  97. return AlertDialog(
  98. title: Text('Add Discussion'),
  99. content: SingleChildScrollView(
  100. child: ListBody(
  101. children: <Widget>[
  102. new Container(
  103. child: new TextFormField(
  104. autofocus: true,
  105. validator: (val) {
  106. return val.isEmpty ? "Required" : null;
  107. },
  108. decoration: new InputDecoration(hintText: 'Title'),
  109. onChanged: (value) {
  110. title = value;
  111. },
  112. ),
  113. ),
  114.  
  115. new Container(
  116. child: new TextFormField(
  117. autofocus: true,
  118. maxLines: null,
  119. validator: (val) {
  120. return val.isEmpty ? "Required" : null;
  121. },
  122. decoration: new InputDecoration(hintText: 'Content'),
  123. onChanged: (value) {
  124. content = value;
  125. },
  126. ),
  127. ),
  128. ],
  129. ),
  130. ),
  131. actions: <Widget>[
  132. FlatButton(
  133. child: Text('Ok'),
  134. onPressed: () {
  135. addDiscuss(title, content);
  136. Navigator.of(context).pop();
  137. },
  138. ),
  139. FlatButton(
  140. child: Text('cancel'),
  141. onPressed: () {
  142. Navigator.of(context).pop();
  143. },
  144. ),
  145. ],
  146. );
  147. },
  148. );
  149. }
  150.  
  151.  
  152. @override
  153. Widget build(BuildContext context) {
  154. _ac = AppConfig(context);
  155.  
  156. var size = MediaQuery.of(context).size;
  157.  
  158. /*24 is for notification bar on Android*/
  159. final double itemHeight = (size.height - kToolbarHeight - 24) / 6;
  160. final double itemWidth = size.width / 2.2;
  161.  
  162. return new Scaffold(
  163. appBar: AppBar(
  164. title: _appBarTitle,
  165. backgroundColor: Color(0xff146BB1),
  166. actions: <Widget>[
  167. new IconButton(
  168. icon: Icon(Icons.add),
  169. onPressed: () => _asyncInputDialog(context),
  170. ),
  171. ],
  172. ),
  173.  
  174. body: Container(
  175. child: SingleChildScrollView(
  176. child: Column(
  177. crossAxisAlignment: CrossAxisAlignment.center,
  178. mainAxisAlignment: MainAxisAlignment.start,
  179. children: <Widget>[
  180.  
  181. Padding(
  182. padding: const EdgeInsets.fromLTRB(1, 10, 1, 0),
  183. child: Column(
  184. children: <Widget>[
  185.  
  186. isloading ? listDiscussion() : const Center(
  187. child: const CircularProgressIndicator(
  188. valueColor: AlwaysStoppedAnimation<Color>(Colors.grey),
  189. )
  190. ),
  191.  
  192.  
  193. ],
  194. ),
  195. ),
  196. ],
  197. ),
  198. ),
  199. ),
  200. );
  201. }
  202.  
  203. void searchAdd(page, title) async {
  204. _searchResult.clear();
  205. sharedPreferences = await SharedPreferences.getInstance();
  206. String token = sharedPreferences.getString("token");
  207. _networkUtil.discussLecture(token, widget.idCorse, widget.idLecture, page, title);
  208. Timer(Duration(milliseconds: 3000), () {
  209. _searchResult.clear();
  210. _searchResult.addAll(LectureDiscMaps.list);
  211. setState(() {
  212. //isloading = true;
  213. });
  214. });
  215. }
  216.  
  217. onSearchTextChanged(String text) async {
  218. _searchResult.clear();
  219. if (text.isEmpty) {
  220. setState(() {
  221.  
  222. });
  223. return;
  224. } else {
  225. _searchResult.clear();
  226. searchAdd(page, text);
  227. }
  228.  
  229. setState(()
  230. {
  231.  
  232. });
  233. }
  234.  
  235. List<LectureDiscussionModel> _searchResult = new List();
  236.  
  237. Widget discussion(int index) {
  238. return Container(
  239. margin: EdgeInsets.all(2.0),
  240. child: GestureDetector(
  241. onTap: () => Navigator.push( context, MaterialPageRoute(
  242. builder: (context) => LectureDiscussDetailPage(widget.idCorse, widget.idLecture, _listdisc[index]))),
  243. child: Container(
  244. color: Color(0xffF6F6F6),
  245. child: Padding(
  246. padding: const EdgeInsets.all(10.0),
  247. child: Row(
  248. children: <Widget>[
  249. Container(
  250. width: _ac.rW(20),
  251. child: Column(
  252. children: <Widget>[
  253. Container(
  254. width: _ac.rW(10),
  255. //child: Image.asset(images[0]),
  256. ),
  257.  
  258. Column(
  259. children: <Widget>[
  260. Container(
  261. padding: EdgeInsets.all(5),
  262. width: _ac.rW(20),
  263. child: Text(_listdisc[index].nameUser,
  264. style: TextStyle(fontSize: 12, color: Colors.black38)
  265. ),
  266. )
  267. ],
  268. ),
  269. ],
  270. ),
  271. ),
  272.  
  273. Container(
  274. width: _ac.rW(70),
  275. padding: EdgeInsets.only(left: 10.0),
  276. child: Column(
  277. mainAxisAlignment: MainAxisAlignment.start,
  278. crossAxisAlignment: CrossAxisAlignment.start,
  279. children: <Widget>[
  280. Container(
  281. child: Text(_listdisc[index].created,
  282. style: TextStyle(fontSize: 12, color: Colors.black38),
  283. )
  284. ),
  285.  
  286. Padding(
  287. padding: const EdgeInsets.all(8.0),
  288. child: Html(
  289. data: _listdisc[index].content,
  290. defaultTextStyle: Theme.TextStyles.threadDescription,
  291. ),
  292. ),
  293.  
  294. Padding(
  295. padding: EdgeInsets.all(2),
  296. child: Wrap(
  297. children: <Widget>[
  298. Icon(
  299. Icons.mode_comment,
  300. size: 15,
  301. color: Colors.black38,
  302. ),
  303.  
  304. Text(" "+_listdisc[index].count.toString(),
  305. style: TextStyle(fontSize: 12, color: Colors.black38,)),
  306. ],
  307. ),
  308. ),
  309.  
  310. ],
  311. ),
  312. ),
  313. ],
  314. ),
  315. ),
  316. ),
  317. ),
  318. );
  319. }
  320.  
  321. Widget listDiscussion() {
  322.  
  323. return Container(
  324. child: ListView.builder(
  325. controller: scrollController,
  326. itemCount: _listdisc.length,
  327. itemBuilder: (context, position) {
  328. return discussion(position);
  329. },
  330. physics: const BouncingScrollPhysics(),
  331. scrollDirection: Axis.vertical,
  332. shrinkWrap: true,
  333. ),
  334. );
  335.  
  336. }
  337.  
  338.  
  339.  
  340. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement