ali ma
ali ma

Reputation: 25

transter data from statefull widget to another

I want to transfer the data from text field in floatingActionButton witch is thefloatingactionbutton in MainWidget to MainWidget class in to the text

kind of want to give and get the data from these two class

void main() async {
runApp(new MyApp());
}

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
  title: 'Flutter Demo',
  theme: new ThemeData(
    primarySwatch: Colors.blue,
  ),
  home: MainWidget(),
 );
 }
}
          // MainWidget class 
        class MainWidget extends StatefulWidget {
       const MainWidget({super.key});

      @override 
      State<MainWidget> createState() => _MainWidgetState();
     }

     class _MainWidgetState extends State<MainWidget> {
     @override
     Widget build(BuildContext context) {
       return Scaffold(
      body: Container(
    child: Center(
     // I want to get it from here
      child: Text(),
    ),
       ),
            floatingActionButton: floatingActionButton(),
         );
       }
     }
     
     // the floatingActionButton class
    class floatingActionButton extends StatefulWidget {
      const floatingActionButton({super.key});

    @override
     State<floatingActionButton> createState() => _floatingActionButtonState();
     }

     class _floatingActionButtonState extends State<floatingActionButton> {
          @override
        Widget build(BuildContext context) {
     return FloatingActionButton(
       onPressed: () {
       showDialog(
       context: context,
       builder: (context) {
        return StatefulBuilder(
          builder: (context, setState) {
            return AlertDialog(
              content: Center(
                  child: Container(
                child: Column(
                  children: [
                   I want to send data from here
                    TextField(),
                    IconButton(onPressed: () {}, icon: Icon(Icons.send))
                  ],
                ),
              )),
                          );
                 },
                           );
                  },
                 );
               },
             );
              }
           }

I tried the constructor

but I cant becouse I use the class in here :

 void main() async {
  runApp(new MyApp());
  }

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

I want to send and get the data without constructor

Upvotes: 0

Views: 30

Answers (1)

Shehzad Ahmad
Shehzad Ahmad

Reputation: 131

To convert your statefull widget to a stateless widget you have to use some sort of State Management Solution, I will recommend you to use Provider.

And to transfer the text from Floating Action Button to the the Text field, you can take a variable and assign it a value that you are fetching from the textfield.

Try the following code and you will understand its basics:

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  static const String _title = 'Flutter Code Sample';

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      title: _title,
      home: MyStatefulWidget(),
    );
  }
}

class MyStatefulWidget extends StatefulWidget {
  const MyStatefulWidget({super.key});

  @override
  State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
}

class _MyStatefulWidgetState extends State<MyStatefulWidget> {
  late TextEditingController _controller;

  @override
  void initState() {
    super.initState();
    _controller = TextEditingController();
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: TextField(
          controller: _controller,
          onSubmitted: (String value) async {
            await showDialog<void>(
              context: context,
              builder: (BuildContext context) {
                return AlertDialog(
                  title: const Text('Thanks!'),
                  content: Text(
                      'You typed "$value", which has length ${value.characters.length}.'),
                  actions: <Widget>[
                    TextButton(
                      onPressed: () {
                        Navigator.pop(context);
                      },
                      child: const Text('OK'),
                    ),
                  ],
                );
              },
            );
          },
        ),
      ),
    );
  }
}

Upvotes: 0

Related Questions