简体   繁体   中英

how to send data from a stateless widget to stateful widget?

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),
      ),
    );
  }
}

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),
        ),
      );
    }
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM