Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'package:cloud_firestore/cloud_firestore.dart';
- import 'package:firebase_auth/firebase_auth.dart';
- import 'package:firebase_core/firebase_core.dart';
- import 'package:flutter/material.dart';
- import 'dart:async';
- import 'package:plantual_1/utils/utils.dart';
- import '../notificationsController.dart';
- class myPlants extends StatefulWidget{
- @override
- State<myPlants> createState() => _myPlantsState();
- }
- void _main()async{
- WidgetsFlutterBinding.ensureInitialized();
- await Firebase.initializeApp();
- }
- class _myPlantsState extends State<myPlants>{
- final auth = FirebaseAuth.instance;
- Timer? countdownTimer;
- Duration myDuration = Duration(days: 3);
- @override
- Widget build(BuildContext context) {
- final _auth = auth.currentUser!.uid.toString();
- return Column(
- children: [
- StreamBuilder<QuerySnapshot>(
- stream: FirebaseFirestore.instance.collection(_auth).snapshots(),
- builder: (context, snapshot){
- List<Column> plantWidgets = [];
- if(snapshot.hasData){
- final plants = snapshot.data?.docs.reversed.toList();
- for(var plant in plants!){
- final _auth = FirebaseAuth.instance;
- var date = int.parse(plant['step']!);
- var dateN = new DateTime.now().add(Duration(days: date)).toString();
- var dateNext = DateTime.parse(dateN);
- var asd = DateTime.now().difference(dateNext);
- DateTime now = DateTime.now();
- final plantWidget = Column(
- children: [
- Container(
- width: MediaQuery.of(context).size.width * 0.8,
- height: 1,
- color: Colors.grey[300]
- ),
- Container(
- color: Colors.white,
- width: double.infinity,
- height: MediaQuery.of(context).size.width * 0.29 + 20,
- child: Row(
- mainAxisAlignment: MainAxisAlignment.start,
- children: [
- const Padding(padding: EdgeInsets.only(left: 10)),
- Column(
- mainAxisAlignment: MainAxisAlignment.center,
- crossAxisAlignment: CrossAxisAlignment.center,
- children: [
- Container(
- width: MediaQuery.of(context).size.width * 0.23,
- height: MediaQuery.of(context).size.width * 0.23,
- child: _kartinka(ElementImage: plant['image']),
- )
- ],
- ),
- const Padding(padding: EdgeInsets.only(left: 10)),
- Flexible(
- child: Container(
- width: MediaQuery.of(context).size.width * 0.62,
- child: Column(
- mainAxisAlignment: MainAxisAlignment.center,
- crossAxisAlignment: CrossAxisAlignment.start,
- children: [
- Text(plant['name'], style: const TextStyle(fontSize: 18)),
- Text(dateNext.minute > 9 ? 'Дата следующего полива ${plant['dateDay']}-${plant['dateMonth']}-${plant['dateYear']} в ${plant['dateHour']}:${plant['dateMinute']}' : 'Дата следующего полива ${plant['dateDay']}-${plant['dateMonth']}-${plant['dateYear']} в ${plant['dateHour']}:0${plant['dateMinute']}', style: const TextStyle(color: Colors.grey, fontSize: 16),),
- Container(
- width: double.infinity,
- child: ElevatedButton(
- onPressed: (){
- NotificationService().showNotification(
- title: 'samole',
- body: 'dadad'
- );
- DateTime now = DateTime.now();
- var date = int.parse(plant['step']!);
- var dateN = new DateTime.now().add(Duration(days: date)).toString();
- var dateNext = DateTime.parse(dateN);
- var asd = DateTime.now().difference(dateNext);
- assert(date is int);
- String user = _auth.currentUser!.uid.toString();
- CollectionReference collectionReference = FirebaseFirestore.instance.collection(user);
- print(asd.inSeconds);
- if(asd.inSeconds < 0){
- utilsError().toastMessage('Полейте позже');
- }else{
- if((dateNext.hour + 5) > 24){
- collectionReference.doc(plant['name']).set({
- 'name': '${plant['name']}',
- 'image': '${plant['image']}',
- 'dateDay': '${dateNext.day.toString().padLeft(2,'0')}',
- 'dateMonth': '${dateNext.month.toString().padLeft(2,'0')}',
- 'dateYear': '${dateNext.year}',
- 'dateHour': '${(24 - (dateNext.hour + 5)).toString().padLeft(2,'0')}',
- 'dateMinute': dateNext.minute > 9 ? '${dateNext.minute.toString().padLeft(2,'0')}' : '0${dateNext.minute.toString().padLeft(2,'0')}',
- 'step': '${plant['step']}',
- 'page': '${plant['page']}'
- });
- }else{
- collectionReference.doc(plant['name']).set({
- 'name': '${plant['name']}',
- 'image': '${plant['image']}',
- 'dateDay': '${dateNext.day.toString().padLeft(2,'0')}',
- 'dateMonth': '${dateNext.month.toString().padLeft(2,'0')}',
- 'dateYear': '${dateNext.year}',
- 'dateHour': '${(dateNext.hour + 5).toString().padLeft(2,'0')}',
- 'dateMinute': dateNext.minute > 9 ? '${dateNext.minute.toString().padLeft(2,'0')}' : '0' + '${dateNext.minute.toString().padLeft(2,'0')}',
- 'step': '${plant['step']}',
- 'page': '${plant['page']}'
- });
- }}
- },
- child: Text('Полил'),
- )
- ),
- ///Text(plant['date'], style: const TextStyle(color: Colors.grey, fontSize: 16),)
- ],
- ),
- ),
- ),
- Column(
- mainAxisAlignment: MainAxisAlignment.start,
- children: [
- IconButton(onPressed: (){
- FirebaseFirestore.instance.collection(
- _auth.currentUser!.uid.toString())
- .doc(plant['name'])
- .delete();
- },
- icon: Icon(Icons.delete))
- ],
- )
- ],
- ),
- )
- ],
- );
- plantWidgets.add(plantWidget);
- }
- }
- return Expanded(
- child: ListView(
- children: plantWidgets,
- ),
- );
- }
- ),
- ],
- );
- }
- }
- class _kartinka extends StatelessWidget {
- final String ElementImage;
- const _kartinka({super.key, required this.ElementImage});
- @override
- Widget build(BuildContext context) {
- return ClipRRect(
- borderRadius: BorderRadius.circular(16),
- child: SizedBox.fromSize(
- size: const Size.fromRadius(16),
- child: Image.network(ElementImage, fit: BoxFit.fitWidth)
- )
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement