0

I am totally flutter beginner.

What I want to do is pass the data (by TextController) from StatefulWidget to another one.

Here is my code (passive Widget)

import 'package:flutter/material.dart';

class VocabularyText extends StatefulWidget {
  final String text;

  // ignore: sort_constructors_first
  const VocabularyText ({ Key key, this.text }): super(key: key);

  @override
  _VocabularyTextState createState() => _VocabularyTextState();
}

class _VocabularyTextState extends State<VocabularyText> {
  Offset offset = Offset.zero;

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Positioned(
        left: offset.dx,
        top: offset.dy,
        child: GestureDetector(
            onPanUpdate: (details) {
              setState(() {
                offset = Offset(
                    offset.dx + details.delta.dx, offset.dy + details.delta.dy);
              });
            },
            child: const SizedBox(
              width: 300,
              height: 300,
              child: Padding(
                padding: EdgeInsets.all(8),
                child: Center(
                  child: Text(
                      'a',
                      textAlign: TextAlign.center,
                      style: TextStyle(
                          fontWeight: FontWeight.bold,
                          fontSize: 28,
                          color: Colors.red
                      )
                  ),
                ),
              ),
            )),
      ),
    );
  }
}

The thing is here

                  child: Text(
//
                      widget.text,
//
                      textAlign: TextAlign.center,
                      style: TextStyle(
                          fontWeight: FontWeight.bold,
                          fontSize: 28,
                          color: Colors.red
                      )
                  ),

According to my research, this should work, but it doesn't. Why did I make a mistake?

Here is references

Thank you in advance.

edit

a

b

1 Answers1

0

i answered before seeing the image it wasn't there after seeing the image what causing the problem is this

widget.text

the correct way to use it in Text widget is like this

Text('${widget.text}'),

i would suggest that you do the following

to send data to Statefull Widget first you use Navigator or any other method to open this widget to the user like so

               return GestureDetector(
                           onTap: () => {
                             Navigator.push(
                               context,
                               MaterialPageRoute(builder: (context) => 
                               //note here between the () you pass the variable
                              Categories(companies[index].id, companies[index].name)),
                             )},

and then to receive them you do like this in my case its Categories Class

class Categories extends StatefulWidget {
  //getting company id from home page
  final int companyId;
  final companyName;
  Categories(this.companyId , this.companyName);
  @override
  _CategoriesState createState() => _CategoriesState();
}



class _CategoriesState extends State<Categories> {
  @override
  Widget build(BuildContext context) {
...... rest of the code

and now to use the data you can do like this for example

widget.companyId

this was an example from my code now lets jump to your code

to receive the text from the text editing controller you do

class TextReceiver extends StatefulWidget {
  //getting company id from home page
  final String userInput;
  TextReceiver(this.userInput);
  @override
  TextReceiver createState() => _TextReceiver();
}
//to use it
widget.userInput

now to send it you send it through Material Navigator

                       onTap: () => {
                         Navigator.push(
                           context,
                           MaterialPageRoute(builder: (context) => TextReceiver(TextEditingController.text)),
                         )},

note that you should pass it as TextEditingController.text because the constructor in TextReceiver is specifying the type to String if you passed TextEditingController then the type wouldn't be String it will be TextEditingController type

all of this code is for example and it would't be like your code but it will give you the idea

refer to official docs https://flutter.dev/docs/cookbook/navigation/passing-data

Edit : remove const from this line

child: const SizedBox(
  rest of the code
)

to this

 child: SizedBox(
   rest of the code
 )
RYOK
  • 473
  • 7
  • 23