Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
3.6 kB
0
Indexable
Never
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Earthquake List',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: EarthquakeList(),
    );
  }
}

class EarthquakeList extends StatefulWidget {
  @override
  _EarthquakeListState createState() => _EarthquakeListState();
}

class _EarthquakeListState extends State<EarthquakeList> {
  List data = [];
  bool isLoading = true;
  var titleController = TextEditingController();
  var descriptionController = TextEditingController();

  @override
  void initState() {
    super.initState();
    fetchData();
  }

  Future<List<dynamic>> fetchData() async {
    final response = await http
        .get(Uri.parse('http://praktikum.bionus.co.id/?nim=1302204058'));

    return json.decode(response.body);
  }

  Future postdata(String title, String description) async {
    return await http.post(
        Uri.parse('http://praktikum.bionus.co.id/?add&nim=1302204058'),
        body: {'title': title, 'description': description});
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Earthquake List'),
      ),
      body: FutureBuilder<List<dynamic>>(
          future: fetchData(),
          builder: (BuildContext context, AsyncSnapshot snapshot) {
            if (snapshot.hasData) {
              return ListView.separated(
                  itemBuilder: (BuildContext context, index) {
                    return Text(snapshot.data[index]['title']);
                  },
                  separatorBuilder: (BuildContext context, index) {
                    return Divider();
                  },
                  itemCount: snapshot.data.length);
            } else {
              print("test");
              return Text('data tidak ditemukan');
            }
          }),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          showDialog(
            context: context,
            builder: (BuildContext context) {
              return AlertDialog(
                title: Text('Add New Data'),
                content: Column(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    TextField(
                      controller: titleController,
                      decoration: InputDecoration(labelText: 'Title'),
                    ),
                    TextField(
                      controller: descriptionController,
                      decoration: InputDecoration(labelText: 'Description'),
                    ),
                  ],
                ),
                actions: [
                  TextButton(
                    onPressed: () {
                      postdata(
                          titleController.text, descriptionController.text);
                      Navigator.push(
                        context,
                        MaterialPageRoute(
                            builder: (context) => EarthquakeList()),
                      );
                    },
                    child: Text('Save'),
                  ),
                ],
              );
            },
          );
        },
        backgroundColor: Colors.green,
        child: const Icon(Icons.add),
      ),
    );
  }
}