agungprabowo112

beritaView.dart

Nov 13th, 2019
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 5.12 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter/cupertino.dart';
  3. import 'package:flutter_bloc/flutter_bloc.dart';
  4. import 'package:intl/intl.dart';
  5. import 'package:shared_preferences/shared_preferences.dart';
  6. import 'package:sistesi/bloc/berita_bloc.dart';
  7. import 'package:sistesi/providers/ApiServices.dart';
  8.  
  9. import 'detailPengumumanView.dart';
  10.  
  11. class DaftarBerita extends StatefulWidget {
  12.   @override
  13.   _DaftarBeritaState createState() => _DaftarBeritaState();
  14. }
  15.  
  16. class _DaftarBeritaState extends State<DaftarBerita> {
  17.   ApiServices apiServices;
  18.   SharedPreferences sharedPreferences;
  19.   String urlFile = "";
  20.   BeritaBloc beritaBloc;
  21.   ScrollController scrollController = ScrollController();
  22.  
  23.   void onScroll() {
  24.     double maxScroll = scrollController.position.maxScrollExtent;
  25.     double currentScroll = scrollController.position.pixels;
  26.  
  27.     if (currentScroll == maxScroll) beritaBloc.add(BeritaEvent());
  28.   }
  29.  
  30.   initSharedPreferences() async {
  31.     sharedPreferences = await SharedPreferences.getInstance();
  32.     urlFile = sharedPreferences.getString("urlFile");
  33.   }
  34.  
  35.   @override
  36.   void initState() {
  37.     initSharedPreferences();
  38.     super.initState();
  39.   }
  40.  
  41.   @override
  42.   Widget build(BuildContext context) {
  43.     daftarBerita(berita) {
  44.       String urlImage = "";
  45.       if (berita.thumbnail == null) {
  46.         urlImage = "https://dev.sistesi.id/basic/public/img/nodata.jpg";
  47.       } else if (berita.thumbnail.toString().contains("https")) {
  48.         urlImage = berita.thumbnail;
  49.       } else {
  50.         urlImage =
  51.             urlFile.toString() + berita.thumbnail.toString();
  52.       }
  53.       return Container(
  54.         height: 100.0,
  55.         child: GestureDetector(
  56.           onTap: () {
  57.             Navigator.push(context, MaterialPageRoute(builder: (context) {
  58.               return PengumumanBeritaView(
  59.                 id: berita.id,
  60.                 tipe: "b",
  61.               );
  62.             }));
  63.           },
  64.           child: Row(
  65.             children: <Widget>[
  66.               Container(
  67.                 height: 80,
  68.                 width: 80,
  69.                 padding:
  70.                     EdgeInsets.only(left: 0, top: 10, bottom: 70, right: 20),
  71.                 decoration: BoxDecoration(
  72.                     image: DecorationImage(
  73.                         image: NetworkImage("$urlImage"), fit: BoxFit.cover)),
  74.               ),
  75.               Padding(
  76.                 padding: const EdgeInsets.all(8.0),
  77.                 child: Column(
  78.                   mainAxisAlignment: MainAxisAlignment.start,
  79.                   crossAxisAlignment: CrossAxisAlignment.start,
  80.                   children: <Widget>[
  81.                     Container(
  82.                       width: MediaQuery.of(context).size.width * 0.7,
  83.                       child: Text(
  84.                         berita.judul,
  85.                         style: TextStyle(
  86.                             fontWeight: FontWeight.w700, fontSize: 17),
  87.                       ),
  88.                     ),
  89.                     Text(
  90.                         DateFormat('dd MMMM yyyy').format(
  91.                             DateTime.parse(berita.tanggal)),
  92.                         style: TextStyle(color: Colors.grey, fontSize: 12.0)),
  93.                   ],
  94.                 ),
  95.               )
  96.             ],
  97.           ),
  98.         ),
  99.       );
  100.     }
  101.  
  102.     final listPengumuman =
  103.         BlocBuilder<BeritaBloc, BeritaState>(builder: (context, state) {
  104.       beritaBloc = BlocProvider.of<BeritaBloc>(context);
  105.       scrollController.addListener(onScroll);
  106.       if (state is BeritaUninitialized) {
  107.         return Center(
  108.           child: SizedBox(
  109.             width: 30,
  110.             height: 30,
  111.             child: CircularProgressIndicator(),
  112.           ),
  113.         );
  114.       } else {
  115.         BeritaLoaded beritaLoaded = state as BeritaLoaded;
  116.         return ListView.separated(
  117.           controller: scrollController,
  118.           separatorBuilder: (context, index) {
  119.             return Divider(
  120.               color: Colors.grey,
  121.             );
  122.           },
  123.           padding: EdgeInsets.all(6),
  124.           itemCount: (beritaLoaded.hasReachedMax || beritaLoaded.berita.length <= 6)
  125.               ? beritaLoaded.berita.length
  126.               : beritaLoaded.berita.length + 1,
  127.           itemBuilder: (context, index) =>
  128.               (index < beritaLoaded.berita.length)
  129.                   ? daftarBerita(beritaLoaded.berita[index])
  130.                   : Container(
  131.                       child: Center(
  132.                         child: SizedBox(
  133.                           width: 30,
  134.                           height: 30,
  135.                           child: CircularProgressIndicator(),
  136.                         ),
  137.                       ),
  138.                     ),
  139.         );
  140.       }
  141.     });
  142.  
  143.     final _appBAr = CupertinoNavigationBar(
  144.       middle: Text(
  145.         "Daftar Berita",
  146.         style: TextStyle(color: Colors.black),
  147.       ),
  148.       automaticallyImplyLeading: true,
  149.     );
  150.     return Scaffold(
  151.       appBar: _appBAr,
  152.       body: BlocProvider<BeritaBloc>(
  153.           builder: (context) => BeritaBloc()..add(BeritaEvent()),
  154.           child: listPengumuman),
  155.     );
  156.   }
  157. }
Add Comment
Please, Sign In to add comment