简体   繁体   中英

Grab the textfield data from another stateful widget class

I am trying to view the textfield data from another class by accessing the state class's method. I want to get the textfield value in another class when i click a button.

I tried calling the method which should return a string but i don't get any value when i try to access it from the other class.

class TitleWidget extends StatefulWidget{
  final IncidentComponent data;
  String titleVal;
  TitleWidget(this.data);
  @override
  TitleWidgetState createState() => TitleWidgetState();
}

class TitleWidgetState extends State<TitleWidget>{

  final titleController = TextEditingController();

  titleTextValue() {
    print("title text field: ${titleController.text}");
    return titleController.text;
  }

  @override
  void initState() {
    super.initState();
    titleController.addListener(titleTextValue);
  }

  @override
  Widget build(BuildContext context) {
    return  TextField(
      controller: titleController);

  }
}

I tried to access the above method to get the string in another class.

FloatingActionButton.extended(
   label: Text("Complete Report",
   onPressed: () { print('title is ' + TitleWidgetState().titleTextValue()); }
)

I was expecting the textfield value when ever i press the FAB button from the other class. But i get nothing from the other class method. But the titleTextValue() method's print statement prints the right value of the textfield.

Try this global store. It can save tons of work on passing all kind of objects. Eventually you need to master blocs.

import 'package:flutter_web/material.dart';
import 'package:flutter_web/widgets.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home: new Scaffold(
        body: new Column(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: <Widget>[
            new MyStatefulWidget1(),
            new MyStatefulWidget2(),
          ],
        ),
      ),
    );
  }
}

class MyStatefulWidget1 extends StatefulWidget {
  State createState() => new MyStatefulWidget1State();
}

class MyStatefulWidget1State extends State<MyStatefulWidget1> {
  final titleController = TextEditingController();

  titleTextValue() {
    print("title text field: ${titleController.text}");
    return titleController.text;
  }

  @override
  Widget build(BuildContext context) {
    store.set("titleTextValue", titleTextValue);
    return TextField(controller: titleController);
  }
}

class MyStatefulWidget2 extends StatefulWidget {
  State createState() => new MyStatefulWidget2State();
}

class MyStatefulWidget2State extends State<MyStatefulWidget2> {
  String _text = 'PRESS ME';

  @override
  Widget build(BuildContext context) {
    var titleTextValue = store.get("titleTextValue");
    return new Center(
      child: new RaisedButton(
          child: new Text(_text),
          onPressed: () {
            print('title is ' + titleTextValue());
          }),
    );
  }
}

class GlobalState {
  final Map<dynamic, dynamic> _data = <dynamic, dynamic>{};

  static GlobalState instance = GlobalState._();
  GlobalState._();

  set(dynamic key, dynamic value) => _data[key] = value;
  get(dynamic key) => _data[key];
}

final GlobalState store = GlobalState.instance;

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