144 lines
5.1 KiB
Dart
144 lines
5.1 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:dio/dio.dart';
|
|
import 'package:monitoring/config.dart';
|
|
import 'package:monitoring/models/berita_model.dart';
|
|
import 'package:monitoring/screens/features/berita_detail_screen.dart';
|
|
|
|
class BeritaScreen extends StatefulWidget {
|
|
final String token;
|
|
const BeritaScreen({super.key, required this.token});
|
|
|
|
@override
|
|
State<BeritaScreen> createState() => _BeritaScreenState();
|
|
}
|
|
|
|
class _BeritaScreenState extends State<BeritaScreen> {
|
|
List<BeritaModel> beritaList = [];
|
|
bool loading = true;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
fetchBerita();
|
|
}
|
|
|
|
Future<void> fetchBerita() async {
|
|
try {
|
|
final dio = Dio(
|
|
BaseOptions(
|
|
baseUrl: '$baseUrl',
|
|
headers: {
|
|
'Accept': 'application/json',
|
|
'Authorization': 'Bearer ${widget.token}',
|
|
},
|
|
),
|
|
);
|
|
|
|
final res = await dio.get('/berita');
|
|
final List raw = res.data['data'];
|
|
setState(() {
|
|
beritaList = raw.map((e) => BeritaModel.fromJson(e)).toList();
|
|
loading = false;
|
|
});
|
|
} catch (e) {
|
|
debugPrint('Gagal: $e');
|
|
setState(() => loading = false);
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: const Text('Berita'),
|
|
backgroundColor: Color(0xFF43A047),
|
|
foregroundColor: Colors.white,
|
|
),
|
|
body:
|
|
loading
|
|
? const Center(child: CircularProgressIndicator())
|
|
: ListView.builder(
|
|
itemCount: beritaList.length,
|
|
padding: const EdgeInsets.all(12),
|
|
itemBuilder: (context, index) {
|
|
final berita = beritaList[index];
|
|
return Card(
|
|
margin: const EdgeInsets.only(bottom: 12),
|
|
shape: RoundedRectangleBorder(
|
|
borderRadius: BorderRadius.circular(12),
|
|
),
|
|
elevation: 2,
|
|
child: Column(
|
|
children: [
|
|
if (berita.gambarUrl != null)
|
|
ClipRRect(
|
|
borderRadius: const BorderRadius.vertical(
|
|
top: Radius.circular(12),
|
|
),
|
|
child: Image.network(
|
|
berita.gambarUrl!,
|
|
height: 160,
|
|
width: double.infinity,
|
|
fit: BoxFit.cover,
|
|
errorBuilder:
|
|
(_, __, ___) => Image.asset('assets/404.png'),
|
|
),
|
|
),
|
|
Padding(
|
|
padding: const EdgeInsets.all(12),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Text(
|
|
berita.judul,
|
|
style: const TextStyle(
|
|
fontSize: 18,
|
|
fontWeight: FontWeight.bold,
|
|
),
|
|
),
|
|
const SizedBox(height: 6),
|
|
Text(
|
|
berita.ringkasan ?? '-',
|
|
maxLines: 2,
|
|
overflow: TextOverflow.ellipsis,
|
|
),
|
|
const SizedBox(height: 8),
|
|
Text(
|
|
'Kategori: ${berita.kategori ?? '-'} - oleh ${berita.penulis ?? '-'}',
|
|
style: const TextStyle(
|
|
fontSize: 12,
|
|
color: Colors.grey,
|
|
),
|
|
),
|
|
Align(
|
|
alignment: Alignment.centerRight,
|
|
child: GestureDetector(
|
|
onTap: () {
|
|
Navigator.push(
|
|
context,
|
|
MaterialPageRoute(
|
|
builder:
|
|
(context) => BeritaDetailScreen(
|
|
berita: berita,
|
|
),
|
|
),
|
|
);
|
|
},
|
|
child: Text(
|
|
'Selengkapnya...',
|
|
style: TextStyle(color: Colors.blue),
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
},
|
|
),
|
|
);
|
|
}
|
|
}
|