0

I have a program for notes. I need to transfer data from the save note screen to the previous screen where it should be saved and displayed. I have assigned values ​​to two TextFilds via onChanged and I need to pass them to the previous screen

Screen for note and save it:

class _NoteState extends State<Note> {
    String zag = '';
  String vm = '';

   
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: Column(
        children: [
             Padding(
              padding: const EdgeInsets.only(top: 55, left: 11),
              child: Row(
                
                children: [
                  Wrap(
                    spacing: -6,
                    children: [
                  
                          IconButton(
                        
                icon: const Icon(
                  size: 27.0,
                  Icons.arrow_back, color: Colors.black,),
                onPressed: () {
                  Navigator.of(context).pop();
                },
              ),
                         
                            
                           Padding(
                             padding: const EdgeInsets.only(top:12.0),
                             child: Text('Notes', style: GoogleFonts.montserrat(
                textStyle: const  TextStyle(
                  fontSize: 19,
                  color: Colors.black,
                  fontWeight: FontWeight.w600
                )
               ),),
                           ),
                    ]     
                  ),  

                  Padding(
                    padding: const EdgeInsets.only(left:230.0, right: 11),
                    child: IconButton( 
                icon: const Icon(
                    size: 27.0,
                    Icons.more_horiz, color: Colors.black,),
                onPressed: () {
                    showModalBottomSheet(context: context,builder: (BuildContext context){
                        return SizedBox(
                          height: 150,
                          child: ListView(
                            children:  [
                              TextButton(onPressed: (){
                            FirebaseFirestore.instance.collection('items').add({'zag': zag, 'vm': vm});

                              }, child: Text('Зберегти нотатку', style: GoogleFonts.montserrat(
                                    textStyle: const  TextStyle(fontSize: 15, color: Colors.black, fontWeight: FontWeight.w600)
                              ),)),
                                 TextButton(onPressed: null, child: Text('Змінити нотатку',style: GoogleFonts.montserrat(
                                    textStyle: const  TextStyle(fontSize: 15, color: Colors.black, fontWeight: FontWeight.w600)
                              ))),
                                  TextButton(onPressed: null, child: Text('Поділитися нотаткою',style: GoogleFonts.montserrat(
                                    textStyle: const  TextStyle(fontSize: 15, color: Colors.black, fontWeight: FontWeight.w600)
                              )))
                            ],
                          ),
                        );
                    }
                    );
                },
              ),
                  ),    
                ],
              )
            ),



          Padding(
            padding: const EdgeInsets.only(top: 0.0, left: 11, right: 11),
            child: SizedBox(
              
              width: 390,
              child: TextField(
                onChanged: (value) {
                  setState(() {
                   zag = value;
                  });
                },
                maxLines: null,
               style: GoogleFonts.montserrat(
                textStyle: const  TextStyle(
                  wordSpacing: -1,
                  fontSize: 28.5,
                  color: Colors.black,
                  fontWeight: FontWeight.w700
                )
               ),
                decoration:   InputDecoration(
                  contentPadding: EdgeInsets.only(top: 0, left: 10 ),
                  filled: false,
                  hintText: 'Заголовок', 
                  hintStyle: GoogleFonts.montserrat(
                textStyle: const   TextStyle(
                  fontSize: 30,
                  color: Colors.grey,
                  fontWeight: FontWeight.w700
                )
               ),
                  
                  border: const  OutlineInputBorder(
                    borderSide: BorderSide.none
                  ) 
                ),
              ),
            ),
          ),

            Padding(
             padding: const EdgeInsets.only(top: 7, left: 11, right: 11),
             child: TextField(
              onChanged: (value) {
                  setState(() {
                    vm = value;
                  });
                },
              style: GoogleFonts.montserrat(
                textStyle: const  TextStyle(
                 fontSize: 16,
                 color: Colors.black,
                 fontWeight: FontWeight.w500
                )
              ),
                maxLines: null,
          keyboardType: TextInputType.multiline,
          decoration:  InputDecoration(
            hintText: 'Зміст вашого запису',
            hintStyle: GoogleFonts.montserrat(textStyle: const TextStyle(color: Colors.grey, fontSize: 19, fontWeight: FontWeight.w700)),
              contentPadding: const  EdgeInsets.only(left: 13, top: 0.0),
              border: const OutlineInputBorder(
                borderSide: BorderSide.none,
              ),
              
                ),
                    ),
           )
        ],
      )
    );
  }
}

Screen to display the note:

class Home extends StatefulWidget {
   const Home({Key? key}) : super(key: key);

  @override
  State<Home> createState() => _HomeState(); 
}

class _HomeState extends State<Home> {
  late String selectedNoteTitle;
  late String selectedNoteContent;
   List notes = [];
  String zag = '';
  String vm = '';
      void initFireBase() async {
      WidgetsFlutterBinding.ensureInitialized();
      await Firebase.initializeApp();
      }

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

              initFireBase();
        }

  @override
  Widget build(BuildContext context) {
    return StreamBuilder(
      stream: FirebaseFirestore.instance.collection('items').snapshots(),
      builder: (BuildContext context,AsyncSnapshot<QuerySnapshot> snapshot) {
        if(!snapshot.hasData) return const Text('Нема записів');
        return Scaffold(
          floatingActionButton: Padding(
            padding: const EdgeInsets.only(bottom: 35.0),
            child: Align(
              alignment: FractionalOffset.bottomCenter,
              child: SizedBox(
                  
                    width: 68,
                    height: 68,
    
                child: FloatingActionButton(
                  elevation: 0,
                            backgroundColor: const Color.fromARGB(255, 57, 26, 26),
    
                  onPressed: (){
                    Navigator.push(context, MaterialPageRoute(builder: (context) => const Note()));
                  },
                  child: const Icon(Icons.add, size: 66,),
                  
                ),
                
              ),
            ),
          ),
          resizeToAvoidBottomInset: false,
          backgroundColor: Colors.white,
          appBar: null,
          
          body:  SafeArea(
            child: Column(
              children:  [
                   Padding(
                  padding: const  EdgeInsets.only(top: 55,right: 252),
                  child: Text('Notely', 
                  style:  GoogleFonts.montserrat(textStyle:  const TextStyle(fontSize: 28,fontWeight: FontWeight.w700, color: Colors.black),)),
                ),
    
                  Padding(padding: const EdgeInsets.only(top: 12, left: 20, right: 20),
                  child: SizedBox(
                    height: 50.0,
                    width: 365,
                    child: TextField(
                      decoration: InputDecoration(
                        filled: true,
                        fillColor: const Color.fromARGB(255, 217, 217, 217),
                        border:  OutlineInputBorder(
                          borderRadius: BorderRadius.circular(16.0),
                          borderSide: BorderSide.none
                        ),
                        hintText: 'Пошук', 
                        hintStyle: GoogleFonts.montserrat(textStyle: const TextStyle(fontSize: 13.8, fontWeight: FontWeight.w500, color: Colors.black),),
                        contentPadding: const EdgeInsets.only(left: 16),
                      ),
                    ),
                  ),
                  ),
                  
                  Expanded(
                    child: Padding(
                      padding: const  EdgeInsets.only(top: 15),
                      child: ListView.builder(
                    itemCount: notes.length,
                          itemBuilder: (BuildContext context, int index) {
                         return ListTile(
            title: Text(zag),
            subtitle:Text(vm),
          onLongPress: () {
            showDialog(
              context: context,
              builder: (BuildContext context) {
                return AlertDialog(
                  title: Text('Видалити елемент?'),
                  content: Text('Ви впевнені, що хочете видалити цей елемент?'),
                  actions: <Widget>[
                    TextButton(
                      child: const Text('Ні'),
                      onPressed: () {
                        Navigator.of(context).pop();
                      },
                    ),
                    TextButton(
                      child: const Text('Так'),
                      onPressed: () {
                        setState(() {
                          notes.removeAt(index);
                        });
                        Navigator.of(context).pop();
                      },
                    ),
                  ],
                );
              },
            );
          },
        );
      },
    )
                      ) 
                  ),
               
             
              ],
               
            ),
            
          ),
        );
      }
    );
  }
}

FireBase applied in the program Help me please

  • Does [How can I navigate between 2 classes, one of them requires passing data? in flutter](https://stackoverflow.com/q/68605567/10157127) answer your question – Md. Yeasin Sheikh Apr 08 '23 at 19:29

1 Answers1

1

To pass data from the Note screen to Home screen, you can use the Navigator.pop() method along with the result parameter. Here's an example:

void onBackPressed() {
  Map<String, String> noteData = {'zag': zag, 'vm': vm};
  Navigator.pop(context, noteData);
}

In the Home screen, you can await for the result of the Navigator.push() method, which will return the data you passed using the result parameter.

FloatingActionButton(
    elevation: 0,
    backgroundColor: const Color.fromARGB(255, 57, 26, 26),
    onPressed: () async {
        Map<String, String> noteData = await Navigator.push(context, MaterialPageRoute(builder: (context) => const Note()));
        if (noteData != null) {
            zag = noteData['zag'];
            vm = noteData['vm'];
        }
    },
    child: const Icon(Icons.add, size: 66,),                 
)
Waseem Abbas
  • 341
  • 1
  • 6