如何解决如何从另一个StatefulWidget调用有状态Widget中的函数?
你好,我想调用formCliente类的StatefulWidget内的函数来清理控制器。但是我想通过formFinanceiro类的StatefulWidget内的按钮来访问它。请帮帮我!谢谢。
class _CadastrarClienteState extends State<CadastrarCliente>
with TickerProviderStateMixin {
body: Form(
key: formkey,child: TabBarView(
physics: physics,//NeverScrollableScrollPhysics()
controller: _tabController,children: [
FormCliente(),FormDocumento(),FormVeiculo(),FormContrato(),FutureBuilder(
future: getTrabalhaComCota(),builder: (context,snapshot) {
if (snapshot.hasData && !snapshot.hasError) {
// print(' chamada cota:${snapshot.data}');
return FormFinanceiro(
itemsCota: snapshot.data,formKey: formkey);
} else {
return Center(
child: CircularProgressIndicator(),);
}
}),],),
} ]
解决方法
您可以使用流实现相同的效果,请参见下面的代码:
import 'package:flutter/material.dart';
import 'dart:async';
final Color darkBlue = Color.fromARGB(255,18,32,47);
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue),debugShowCheckedModeBanner: false,home: Scaffold(
appBar: AppBar(title: Text("Demo")),body: MyApps(),),);
}
}
class MyApps extends StatefulWidget {
@override
_MyAppsState createState() => _MyAppsState();
}
class _MyAppsState extends State<MyApps> {
final changeNotifier = new StreamController.broadcast();
@override
void dispose() {
changeNotifier.close();
super.dispose();
}
buttonClicked() => changeNotifier.sink.add(null);
@override
Widget build(BuildContext context) {
return Column(
children: [
FormCliente(
shouldTriggerChange: changeNotifier.stream,FormFinanceiro(buttonClicked: buttonClicked),],);
}
}
class FormCliente extends StatefulWidget {
final Stream shouldTriggerChange;
FormCliente({this.shouldTriggerChange});
@override
_FormClienteState createState() => _FormClienteState();
}
class _FormClienteState extends State<FormCliente> {
StreamSubscription streamSubscription;
@override
initState() {
super.initState();
if (widget.shouldTriggerChange != null) {
streamSubscription =
widget.shouldTriggerChange.listen((_) => clearYourFormMethod());
}
}
@override
didUpdateWidget(FormCliente old) {
super.didUpdateWidget(old);
if (widget.shouldTriggerChange != old.shouldTriggerChange) {
streamSubscription.cancel();
streamSubscription =
widget.shouldTriggerChange.listen((_) => clearYourFormMethod());
}
}
@override
dispose() {
super.dispose();
streamSubscription.cancel();
}
void clearYourFormMethod() {
print('Please clear your form here');
}
@override
Widget build(BuildContext context) {
return Text("FormCliente");
}
}
class FormFinanceiro extends StatefulWidget {
final Function buttonClicked;
FormFinanceiro({this.buttonClicked});
@override
_FormFinanceiroState createState() => _FormFinanceiroState();
}
class _FormFinanceiroState extends State<FormFinanceiro> {
@override
Widget build(BuildContext context) {
return Column(
children: <Widget>[
RaisedButton(
child: new Text("FormFinanceiro"),onPressed: widget.buttonClicked,)
],);
}
}
,
您需要抬起state
:
- 在
_CadastrarClienteState
类中创建控制器。 - 在
_CadastrarClienteState
中创建回调 - 将控制器传递给孩子
FormFinanceiro
。 - 将回调传递给子
FormCliente
。 - 现在,在
FormCliente
中修改onbuttonpressed函数,以便在按下按钮时调用回调。 - 最后,在
_CadastrarClienteState
中提供的回调方法中,使用控制器清除值。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。