Untitled

 avatar
unknown
plain_text
2 years ago
6.7 kB
6
Indexable
import 'package:firebase_database/ui/firebase_animated_list.dart';
import 'package:flutter/material.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_database/firebase_database.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  runApp(MyApp());
}

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

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  final DatabaseReference _database = FirebaseDatabase.instance.ref('');
  var titleController = TextEditingController();
  var descriptionController = TextEditingController();

  List<String> _items = [];

  @override
  void initState() {
    super.initState();
    _database.onChildAdded.listen((event) {
      setState(() {
        _items.add(event.snapshot.value.toString());
      });
    });
  }

  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: () {
                _addItem(title, description);
                Navigator.of(context).pop();
              },
              child: Text('Save'),
            ),
          ],
        );
      },
    );
  }

  void _addItem(String title, String description) {
    _database.push().set({'title': title, 'description': description});
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Rafi Atha Ridel_TP12'),
      ),
      body: FirebaseAnimatedList(
          query: _database,
          itemBuilder: (BuildContext context, snapshot, animation, index) {
            var data = snapshot.value as Map<dynamic, dynamic>;
            return ListTile(
              onTap: () {
                showDialog(
                  context: context,
                  builder: (BuildContext context) {
                    titleController.text = data['title'].toString();
                    descriptionController.text = data['description'].toString();
                    return AlertDialog(
                      title: Text('Update Item'),
                      content: Column(
                        mainAxisSize: MainAxisSize.min,
                        children: [
                          TextField(
                            controller: titleController,
                            decoration: InputDecoration(labelText: 'Title'),
                          ),
                          TextField(
                            controller: descriptionController,
                            decoration:
                                InputDecoration(labelText: 'Description'),
                          ),
                        ],
                      ),
                      actions: [
                        TextButton(
                          onPressed: () {
                            _updateItem(
                                snapshot.key.toString(),
                                titleController.text,
                                descriptionController.text);
                            Navigator.of(context).pop();
                          },
                          child: Text('Save'),
                        ),
                      ],
                    );
                  },
                );
              },
              onLongPress: () {
                showDialog(
                  context: context,
                  builder: (BuildContext context) {
                    return AlertDialog(
                      title: Text('Update Item'),
                      content: Column(
                        mainAxisSize: MainAxisSize.min,
                        children: [
                          Text('Apakah anda yakin ingin menghapus pesan?'),
                        ],
                      ),
                      actions: [
                        Row(
                          children: [
                            TextButton(
                              onPressed: () {
                                _deleteItem(
                                    snapshot.key.toString(),
                                    titleController.text,
                                    descriptionController.text);
                                Navigator.of(context).pop();
                              },
                              child: Text('Save'),
                            ),
                            TextButton(
                              onPressed: () {
                                Navigator.of(context).pop();
                              },
                              child: Text('Cancel'),
                            ),
                          ],
                        ),
                      ],
                    );
                  },
                );
              },
              subtitle: Text(data['description'].toString()),
              title: Text(data['title'].toString()),
            );
          }),
      floatingActionButton: FloatingActionButton(
        onPressed: _showDialog,
        child: Icon(Icons.add),
      ),
    );
  }

  Future _updateItem(String id, String title, String description) async {
    // _database.push().set({'title': title, 'description': description}
    _database
        .child(id.toString())
        .update({'title': title, 'description': description});
  }

  Future _deleteItem(String id, String title, String description) async {
    // _database.push().set({'title': title, 'description': description}
    _database.child(id).remove();
  }
}
Editor is loading...