1
votes

I am creating a simple app in flutter where there are two widgets in main function. one is display text widget which is used to display text to the screen and another one is getting input from the user ie., TextField. this is simple. now i created a stateless widget sacaffold in main.dart and in the body i called two classes. one is 'textfield' stateless widget (as a separate dart file) and another is 'display' stateful widget (as a separate dart file) , since i wanted to update the display for every change in input textfield. now i get the setup and created textfield and send button in textfield.dart and i need to send that text to display_text.dart which has a stateful widget. how to do that. No Laughing pls.. new to programming and flutter.

  class TextFieldInput extends StatelessWidget {
  final txtController = TextEditingController();
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Column(
        children: <Widget>[
          TextField(
            controller: txtController,
            style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
          ),
          RaisedButton(
            child: Text('Click Me'),
            onPressed: () {
              //want to send txtController.text to DisplayText (display_text.dart);
            },
          )
        ],
      ),
    );
  }
}


class DisplayText extends StatefulWidget {
  //DisplayText({Key key}) : super(key: key);
  _DisplayTextState createState() => _DisplayTextState();
}

class _DisplayTextState extends State<DisplayText> {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Text(
        'text from iput text fied here',
        style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
      ),
    );
  }
}
1

1 Answers

1
votes

you can pass your data through navigator and access using widget object in stateful method.

i added the solution in following code.

import 'package:flutter/material.dart';

class TextFieldInput extends StatelessWidget {
    final txtController = TextEditingController();
    @override
    Widget build(BuildContext context) {
      return Container(
        child: Column(
          children: <Widget>[
            TextField(
              controller: txtController,
              style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
            ),
            RaisedButton(
              child: Text('Click Me'),
              onPressed: () {
                Navigator.push(
                    context, new MaterialPageRoute(
                    builder: (context) => new DisplayText(text: txtController.text,)));
              },
            )
          ],
        ),
      );
    }
  }


  class DisplayText extends StatefulWidget {
    String text;
      DisplayText({Key key,this.text}) : super(key: key);
    _DisplayTextState createState() => _DisplayTextState();
  }

  class _DisplayTextState extends State<DisplayText> {
    @override
    Widget build(BuildContext context) {
      return Container(
        child: Text(
          widget.text,
          style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
        ),
      );
    }
  }