如何在Flutter中的同级小部件之间传递数据?

时间:2020-03-07 13:30:37

标签: flutter dart

我是新人。

我有3个具有TextFormField和RaisedButton的小部件表单。 TextFormField和RaisedButon小部件都在它们自己的文件中。我想将TextFormField中的文本发送到http服务器。有人可以解释一下我如何在两个小部件之间传递数据以及它们何时位于不同的文件中吗?

TextSearchBox.dart

class TextSearchBox extends StatefulWidget {
    State<StatefulWidget> createState() {
    return _TextSearchBox();
  }
}
class _TextSearchBox extends State<TextSearchBox>{
 final textFieldController = TextEditingController();
   Widget build(BuildContext context) {
    return TextFormField(
      decoration: InputDecoration(
          labelText: "Search Movie Title",
          labelStyle: SearchLabelStyle,
          border: UnderlineInputBorder(
              borderSide: BorderSide(
                  color: Colors.pink
              )
          ),
          focusedBorder: UnderlineInputBorder(
              borderSide: BorderSide(
                  color: Colors.pinkAccent
              )
          )
      ),
      cursorColor: Colors.blueAccent,
      validator: _validateEmpty,
      style: SearchTextStyle,
      controller: textFieldController,
    );
  }
}

FormButton.dart

class FormButton extends StatelessWidget {
  final _hPad = 20.0;
  final _vPad = 10.0;
  final formState;

  FormButton(this.formState);

  @override
  Widget build(BuildContext context) {
    return Container(
      child: RaisedButton(
        color: Colors.pink,
        child: Text("Search", style: ButtonTextStyle,),
        onPressed: (){
          if(formState.currentState.validate()){

          }else{

          }
        },
      ),
      padding: EdgeInsets.fromLTRB(_hPad, _vPad, _hPad, _vPad),
      width: 40,
    );
  }
}

然后我有容纳以上小部件的父小部件。

HomeScreen.dart

class Home extends StatelessWidget {

  final formKey = GlobalKey<FormState>();

  @override
  Widget build(BuildContext context) {


    return Scaffold(
      appBar: AppBar(
        title: Text("App",),
        backgroundColor: Colors.pinkAccent,
      ),
      body: Form(
        key: formKey,
        child: Container(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: <Widget>[
              TextSearchBox(),
              FormButton(formKey)
            ],
          ),
          decoration: BoxDecoration(
              color: Color.fromRGBO(0, 1, 13, 1)
          ),
        ),
      ),
    );
  }
}

我想做的是,因为我的TextSearchBox将具有文本字段,我想知道如何从FormButton.dart文件访问它。我该怎么办?

0 个答案:

没有答案