Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'dart:async';
- import 'package:ebook/Utils/AppConfig.dart';
- import 'package:ebook/Utils/network_util.dart';
- import 'package:ebook/maps/CategoryBooksMaps.dart';
- import 'package:ebook/maps/LectureDiscMaps.dart';
- import 'package:ebook/maps/CourseMaps.dart';
- import 'package:ebook/model/LectureDiscussionModel.dart';
- import 'package:ebook/model/DefModel.dart';
- import 'package:ebook/ui/home/CourseRow.dart';
- import 'package:ebook/ui/planet_detail/DiscLectureDetail.dart';
- import 'package:flutter/material.dart';
- import 'package:ebook/Theme.dart' as Theme;
- import 'package:shared_preferences/shared_preferences.dart';
- import 'package:flutter_html/flutter_html.dart';
- class DiscCourses extends StatefulWidget {
- final String idCorse, idLecture;
- DiscCourses(this.idCorse, this.idLecture);
- @override
- _DiscCoursesState createState() => _DiscCoursesState();
- }
- class _DiscCoursesState extends State<DiscCourses> {
- SharedPreferences sharedPreferences;
- Icon _searchIcon = new Icon(Icons.search);
- Widget _appBarTitle = new Text('Discussion Lecture');
- NetworkUtil _networkUtil;
- AppConfig _ac;
- static List<LectureDiscussionModel> _listdisc = <LectureDiscussionModel>[];
- ScrollController scrollController = new ScrollController();
- bool isloading = false;
- var search;
- String page = '1';
- String title = '';
- DefModel def;
- @override
- void initState() {
- super.initState();
- _networkUtil = new NetworkUtil();
- CategoryBooksMaps.list.clear();
- _listdisc.clear();
- this.listenfordisc(page);
- scrollController.addListener(() {
- if (scrollController.position.pixels ==
- scrollController.position.maxScrollExtent) {
- int np = int.parse(page)+1;
- page = np.toString();
- //print("AHAHAHA");
- listenfordisc(page);
- }
- });
- }
- @override
- void dispose() {
- scrollController.dispose();
- super.dispose();
- }
- void listenfordisc(page) async {
- sharedPreferences = await SharedPreferences.getInstance();
- String token = sharedPreferences.getString("token");
- _networkUtil.discussLecture(token, widget.idCorse, widget.idLecture, page, '');
- Timer(Duration(milliseconds: 3000), () {
- _listdisc.addAll(LectureDiscMaps.list);
- setState(() {
- isloading = true;
- });
- });
- }
- void addDiscuss(title, content) async {
- sharedPreferences = await SharedPreferences.getInstance();
- String token = sharedPreferences.getString("token");
- def = await _networkUtil.addDiscuss(token, widget.idCorse, widget.idLecture, title, content);
- if(def.status == true) {
- _listdisc.clear();
- listenfordisc('');
- }
- }
- Future<String> _asyncInputDialog(BuildContext context) async {
- int _rating = 0;
- String title, content;
- return showDialog<String>(
- context: context,
- barrierDismissible: false,
- // dialog is dismissible with a tap on the barrier
- builder: (BuildContext context) {
- return AlertDialog(
- title: Text('Add Discussion'),
- content: SingleChildScrollView(
- child: ListBody(
- children: <Widget>[
- new Container(
- child: new TextFormField(
- autofocus: true,
- validator: (val) {
- return val.isEmpty ? "Required" : null;
- },
- decoration: new InputDecoration(hintText: 'Title'),
- onChanged: (value) {
- title = value;
- },
- ),
- ),
- new Container(
- child: new TextFormField(
- autofocus: true,
- maxLines: null,
- validator: (val) {
- return val.isEmpty ? "Required" : null;
- },
- decoration: new InputDecoration(hintText: 'Content'),
- onChanged: (value) {
- content = value;
- },
- ),
- ),
- ],
- ),
- ),
- actions: <Widget>[
- FlatButton(
- child: Text('Ok'),
- onPressed: () {
- addDiscuss(title, content);
- Navigator.of(context).pop();
- },
- ),
- FlatButton(
- child: Text('cancel'),
- onPressed: () {
- Navigator.of(context).pop();
- },
- ),
- ],
- );
- },
- );
- }
- @override
- Widget build(BuildContext context) {
- _ac = AppConfig(context);
- var size = MediaQuery.of(context).size;
- /*24 is for notification bar on Android*/
- final double itemHeight = (size.height - kToolbarHeight - 24) / 6;
- final double itemWidth = size.width / 2.2;
- return new Scaffold(
- appBar: AppBar(
- title: _appBarTitle,
- backgroundColor: Color(0xff146BB1),
- actions: <Widget>[
- new IconButton(
- icon: Icon(Icons.add),
- onPressed: () => _asyncInputDialog(context),
- ),
- ],
- ),
- body: Container(
- child: SingleChildScrollView(
- child: Column(
- crossAxisAlignment: CrossAxisAlignment.center,
- mainAxisAlignment: MainAxisAlignment.start,
- children: <Widget>[
- Padding(
- padding: const EdgeInsets.fromLTRB(1, 10, 1, 0),
- child: Column(
- children: <Widget>[
- isloading ? listDiscussion() : const Center(
- child: const CircularProgressIndicator(
- valueColor: AlwaysStoppedAnimation<Color>(Colors.grey),
- )
- ),
- ],
- ),
- ),
- ],
- ),
- ),
- ),
- );
- }
- void searchAdd(page, title) async {
- _searchResult.clear();
- sharedPreferences = await SharedPreferences.getInstance();
- String token = sharedPreferences.getString("token");
- _networkUtil.discussLecture(token, widget.idCorse, widget.idLecture, page, title);
- Timer(Duration(milliseconds: 3000), () {
- _searchResult.clear();
- _searchResult.addAll(LectureDiscMaps.list);
- setState(() {
- //isloading = true;
- });
- });
- }
- onSearchTextChanged(String text) async {
- _searchResult.clear();
- if (text.isEmpty) {
- setState(() {
- });
- return;
- } else {
- _searchResult.clear();
- searchAdd(page, text);
- }
- setState(()
- {
- });
- }
- List<LectureDiscussionModel> _searchResult = new List();
- Widget discussion(int index) {
- return Container(
- margin: EdgeInsets.all(2.0),
- child: GestureDetector(
- onTap: () => Navigator.push( context, MaterialPageRoute(
- builder: (context) => LectureDiscussDetailPage(widget.idCorse, widget.idLecture, _listdisc[index]))),
- child: Container(
- color: Color(0xffF6F6F6),
- child: Padding(
- padding: const EdgeInsets.all(10.0),
- child: Row(
- children: <Widget>[
- Container(
- width: _ac.rW(20),
- child: Column(
- children: <Widget>[
- Container(
- width: _ac.rW(10),
- //child: Image.asset(images[0]),
- ),
- Column(
- children: <Widget>[
- Container(
- padding: EdgeInsets.all(5),
- width: _ac.rW(20),
- child: Text(_listdisc[index].nameUser,
- style: TextStyle(fontSize: 12, color: Colors.black38)
- ),
- )
- ],
- ),
- ],
- ),
- ),
- Container(
- width: _ac.rW(70),
- padding: EdgeInsets.only(left: 10.0),
- child: Column(
- mainAxisAlignment: MainAxisAlignment.start,
- crossAxisAlignment: CrossAxisAlignment.start,
- children: <Widget>[
- Container(
- child: Text(_listdisc[index].created,
- style: TextStyle(fontSize: 12, color: Colors.black38),
- )
- ),
- Padding(
- padding: const EdgeInsets.all(8.0),
- child: Html(
- data: _listdisc[index].content,
- defaultTextStyle: Theme.TextStyles.threadDescription,
- ),
- ),
- Padding(
- padding: EdgeInsets.all(2),
- child: Wrap(
- children: <Widget>[
- Icon(
- Icons.mode_comment,
- size: 15,
- color: Colors.black38,
- ),
- Text(" "+_listdisc[index].count.toString(),
- style: TextStyle(fontSize: 12, color: Colors.black38,)),
- ],
- ),
- ),
- ],
- ),
- ),
- ],
- ),
- ),
- ),
- ),
- );
- }
- Widget listDiscussion() {
- return Container(
- child: ListView.builder(
- controller: scrollController,
- itemCount: _listdisc.length,
- itemBuilder: (context, position) {
- return discussion(position);
- },
- physics: const BouncingScrollPhysics(),
- scrollDirection: Axis.vertical,
- shrinkWrap: true,
- ),
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement