Skip to content

Instantly share code, notes, and snippets.

@PeterHdd
Last active April 11, 2021 12:17
Show Gist options
  • Save PeterHdd/809ff5ff58eb22858c5927a32498541b to your computer and use it in GitHub Desktop.
Save PeterHdd/809ff5ff58eb22858c5927a32498541b to your computer and use it in GitHub Desktop.
art
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_database/firebase_database.dart';
import 'package:firebase_database_tutorial/home.dart';
import 'package:flutter/material.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Pets',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Register Pet'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text("Please Register Your Pet ",
style: TextStyle(
fontWeight: FontWeight.w200,
fontSize: 30,
fontFamily: 'Roboto',
fontStyle: FontStyle.italic)),
RegisterPet(),
]),
)),
);
}
}
class RegisterPet extends StatefulWidget {
RegisterPet({Key key}) : super(key: key);
@override
_RegisterPetState createState() => _RegisterPetState();
}
class _RegisterPetState extends State<RegisterPet> {
final _formKey = GlobalKey<FormState>();
final listOfPets = ["Cats", "Dogs", "Rabbits"];
String dropdownValue = 'Cats';
final nameController = TextEditingController();
final ageController = TextEditingController();
final dbRef = FirebaseDatabase.instance.reference().child("pets");
@override
Widget build(BuildContext context) {
return Form(
key: _formKey,
child: SingleChildScrollView(
child: Column(children: <Widget>[
Padding(
padding: EdgeInsets.all(20.0),
child: TextFormField(
controller: nameController,
decoration: InputDecoration(
labelText: "Enter Pet Name",
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(10.0),
),
),
// The validator receives the text that the user has entered.
validator: (value) {
if (value.isEmpty) {
return 'Enter Pet Name';
}
return null;
},
),
),
Padding(
padding: EdgeInsets.all(20.0),
child: DropdownButtonFormField(
value: dropdownValue,
icon: Icon(Icons.arrow_downward),
decoration: InputDecoration(
labelText: "Select Pet Type",
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(10.0),
),
),
items: listOfPets.map((String value) {
return new DropdownMenuItem<String>(
value: value,
child: new Text(value),
);
}).toList(),
onChanged: (String newValue) {
setState(() {
dropdownValue = newValue;
});
},
validator: (value) {
if (value.isEmpty) {
return 'Please Select Pet';
}
return null;
},
),
),
Padding(
padding: EdgeInsets.all(20.0),
child: TextFormField(
keyboardType: TextInputType.number,
controller: ageController,
decoration: InputDecoration(
labelText: "Enter Pet Age",
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(10.0),
),
),
// The validator receives the text that the user has entered.
validator: (value) {
if (value.isEmpty) {
return 'Please Pet Age';
}
return null;
},
),
),
Padding(
padding: EdgeInsets.all(20.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
ElevatedButton(
onPressed: () {
if (_formKey.currentState.validate()) {
dbRef.push().set({
"name": nameController.text,
"age": ageController.text,
"type": dropdownValue
}).then((_) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Successfully Added')));
ageController.clear();
nameController.clear();
}).catchError((onError) {
ScaffoldMessenger.of(context)
.showSnackBar(SnackBar(content: Text(onError)));
});
}
},
child: Text('Submit'),
),
ElevatedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => Home(title: "Home Page")),
);
},
child: Text('Navigate'),
),
],
)),
])));
}
@override
void dispose() {
super.dispose();
ageController.dispose();
nameController.dispose();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment