Untitled
unknown
plain_text
2 years ago
4.7 kB
5
Indexable
import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:http/http.dart' as http; void main() { runApp(MyApp()); } class Album { final String title; final String description; Album({required this.title, required this.description}); factory Album.fromJson(Map<String, dynamic> json) { return Album( title: json['properties']['title'], description: json['properties']['description'], ); } } class MyApp extends StatelessWidget { Widget build(BuildContext context) { return MaterialApp( title: 'Jurnal 12 Rafi Atha Ridel', theme: ThemeData( primarySwatch: Colors.blue, ), home: HomePage(), ); } } class HomePage extends StatefulWidget { @override _HomePageState createState() => _HomePageState(); } class _HomePageState extends State<HomePage> { var titleController = TextEditingController(); var descriptionController = TextEditingController(); @override void initState() { super.initState(); fetchAlbum(); } List data = []; Future<List> fetchAlbum() async { final response = await http .get(Uri.parse('http://praktikum.bionus.co.id/?nim=1302204110')); if (response.statusCode == 200) { final jsonList = jsonDecode(response.body); setState(() { data = jsonDecode(response.body); }); print(jsonList); // return jsonList.map((json) => Album.fromJson(json)).toList(); return jsonDecode(response.body); } else { throw Exception('Failed to load album'); } } Future<http.Response> createAlbum(String title, String description) { return http.post( Uri.parse('http://praktikum.bionus.co.id/?add&nim=1302204110'), headers: <String, String>{ 'Content-Type': 'application/json; charset=UTF-8', }, body: jsonEncode( <String, String>{'title': title, 'description': description}), ); } void addData(String title, String description) async { var url = 'http://praktikum.bionus.co.id/?add&nim=1302204110'; await http.post(Uri.parse(url), body: {'title': title, 'description': description}); } void _showDialog() { String title = ''; String description = ''; showDialog( context: context, builder: (BuildContext context) { return AlertDialog( title: Text('Add Item'), content: Column( mainAxisSize: MainAxisSize.min, children: [ TextField( onChanged: (value) { title = value; }, decoration: InputDecoration(labelText: 'Title'), ), TextField( onChanged: (value) { description = value; }, decoration: InputDecoration(labelText: 'Description'), ), ], ), actions: [ TextButton( onPressed: () { setState(() { addData(title, description); }); fetchAlbum(); Navigator.of(context).pop(); }, child: Text('Save'), ), ], ); }, ); } @override Widget build(BuildContext context) { return MaterialApp( title: 'TP_!3', theme: ThemeData( primarySwatch: Colors.amber, ), home: Scaffold( appBar: AppBar( title: Text( 'Rafi Atha Ridel', style: TextStyle(color: Colors.white), ), ), body: ListView.builder( itemCount: data.length, itemBuilder: (context, index) { return ListTile( title: Text('${data[index]['title']}'), subtitle: Row( crossAxisAlignment: CrossAxisAlignment.start, children: [ Expanded( child: Column( crossAxisAlignment: CrossAxisAlignment.start, ), ), SizedBox(width: 16), // Add some spacing between the columns Text( '${data[index]['description']}', textAlign: TextAlign.right, ), ], ), ); }, ), floatingActionButton: FloatingActionButton( onPressed: _showDialog, child: Icon(Icons.add), ), ), ); } }
Editor is loading...