Home > OS >  How to pass text widget data from one screen to another screen in Flutter?
How to pass text widget data from one screen to another screen in Flutter?

Time:06-09

I Am trying to pass text widget data from one screen to another screen but I don't know how to do that in flutter.

I know how to do that from textfield and pass between screen. for example using a controller.

child: Text('Repetition', style: TextStyle(fontSize: 18, fontWeight: FontWeight.normal),)),

 onPressed: ()=>{
            Navigator.push(context, MaterialPageRoute(
                builder:(context) => FingerGoalSt()))
          },

I want to pass the Repetition String into next screen.

In other platform it's easy but in flutter i have no idea as i am new to it. Thanks.

CodePudding user response:

var yourString = 'Repetition'
child: Text(yourString, style: TextStyle(fontSize: 18, fontWeight: FontWeight.normal),)),

 onPressed: ()=>{
            Navigator.push(context, MaterialPageRoute(
                builder:(context) => FingerGoalSt(yourString)))
          },

And in yours FingerGoalSt add a constructor with parameter.

CodePudding user response:

You can pass the variable in which you are storing the data

    child: Text(snapshot.data.docs[index].get('name'), style: TextStyle(fontSize: 18, fontWeight: FontWeight.normal),)),

 onPressed: ()=>{
            Navigator.push(context, MaterialPageRoute(
                builder:(context) => FingerGoalSt(snapshot.data.docs[index].get('name'))))
          },

CodePudding user response:

Please refer to the below code

Navigator.push(
              context,
              MaterialPageRoute(
                builder: (context) => FingerGoalSt(
                  textVal: "Repetition",
                ),
              ),
            );



class FingerGoalSt extends StatefulWidget {
final String textVal;
  const FingerGoalSt({Key key, this.textVal}) : super(key: key);
 @override
  _FingerGoalStState createState() => _FingerGoalStState();
}

class _FingerGoalStState extends State<FingerGoalSt> {

}


Please try this below example code

Example 1:

onPressed: () async {
   Navigator.pushNamed(context, '/FingerGoalSt',arguments:{"textvalue":"any text"});}


class FingerGoalSt extends StatelessWidget {
  const FingerGoalSt({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {

    Map arguments = ModalRoute.of(context)?.settings.arguments as Map;

    return  Scaffold(
      appBar: AppBar(
        title:  Text('Value : ${arguments['textvalue']}'),
      ),
      body: Text("${arguments['textvalue']}"),
    );
  }
}

Example 2:

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

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

class _FirstScreenState extends State<FirstScreen> {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Center(
        child: InkWell(
          onTap: () {
            Navigator.push(
              context,
              MaterialPageRoute(
                builder: (context) => SecondScreen(
                  dataList: ["Data"],
                ),
              ),
            );
          },
          child: Text(
            "Second Screen",
          ),
        ),
      ),
    );
  }
}

class SecondScreen extends StatefulWidget {
  final List dataList;
  const SecondScreen({Key key, this.dataList}) : super(key: key);

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

class _SecondScreenState extends State<SecondScreen> {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Center(
        child: Text(
          widget.dataList[0].toString(),
        ),
      ),
    );
  }
}

For more info, please refer to this link description

  • Related