Untitled
unknown
plain_text
6 months ago
5.2 kB
2
Indexable
Never
import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:http/http.dart' as http; void main() { runApp(const MyApp()); } class Data { final String title; final String description; Data({required this.title, required this.description}); factory Data.fromJson(Map<String, dynamic> json) { return Data( title: json['properties']['title'] as String, description: json['properties']['description'] as String, ); } } class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo', theme: ThemeData( primarySwatch: Colors.blue, ), home: const MyHomePage(title: 'Flutter Demo Home Page'), ); } } class MyHomePage extends StatefulWidget { const MyHomePage({super.key, required this.title}); final String title; @override State<MyHomePage> createState() => _MyHomePageState(); } class _MyHomePageState extends State<MyHomePage> { // const MyApp({super.key}); Future<List<Data>> fetchAlbum() async { final response = await http .get(Uri.parse('http://praktikum.bionus.co.id/?nim=1302200010')); if (response.statusCode == 200) { final data = jsonDecode(response.body); final features = data['features'] as List; return features.map((json) => Data.fromJson(json)).toList(); } else { throw Exception('Gagal load album'); } } int _counter = 0; void _incrementCounter() { setState(() { showDialog<void>( context: context, builder: (BuildContext context) { return Center( child: Container( // width: 100, height: 300, child: AlertDialog( title: const Text( 'Add New Data', style: TextStyle(fontWeight: FontWeight.bold), ), content: Column( children: [ TextField( decoration: InputDecoration( border: OutlineInputBorder(), labelText: 'Title', ), ), SizedBox( height: 10, ), TextField( decoration: InputDecoration( border: OutlineInputBorder(), labelText: 'Description', ), ) ], ), actions: <Widget>[ Center( child: TextButton( style: ButtonStyle( backgroundColor: MaterialStatePropertyAll<Color>(Colors.green)), child: const Text( 'Save', style: TextStyle(color: Colors.white), ), onPressed: () { Navigator.of(context).pop(); }, ), ), ], ), ), ); // }, ); }); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text(widget.title), ), body: Center( child: FutureBuilder<List<Data>>( future: fetchAlbum(), builder: (context, snapshot) { if (snapshot.hasData) { final data = snapshot.data!; return ListView.builder( itemCount: data.length, itemBuilder: (context, index) { final album = data[index]; return ListTile( title: Container( width: 50, child: Text( '${data[index].title}', style: TextStyle( fontSize: 16.0, ), ), ), trailing: SizedBox( width: 100.0, child: Text( '${data[index].description}', textAlign: TextAlign.right, style: TextStyle(fontSize: 16.0, color: Colors.black54), ), ), ); }, ); } else if (snapshot.hasError) { return Text('Data Kosong'); } else { return const CircularProgressIndicator(); } }, ), ), floatingActionButton: FloatingActionButton( onPressed: _incrementCounter, backgroundColor: Colors.green, tooltip: 'Increment', child: const Icon(Icons.add), ), ); } }