我已经在我的一个小部件屏幕中创建了一个FlatButton,我希望其状态由同一屏幕中的另一个有状态小部件进行管理。这是一个文本字段,当它开始编辑时,我要启用该按钮。我怎样才能做到这一点?我做了一个函数来切换按钮的布尔值,但是当我尝试从我的有状态窗口小部件类访问此函数时,它给出了错误,“不能使用静态访问来访问实例成员”。我该如何解决这个问题?
我的主屏幕代码
import 'package:flutter/material.dart';
import 'package:flutter_convertor/ItemBought.dart';
class task extends StatefulWidget{
@override
taskState createState() => new taskState();
}
class taskState extends State<task> {
int current_step = 0;
bool isButtonDisabled;
@override void initState() {
super.initState();
isButtonDisabled = false;
}
formReady(){
setState(() {
isButtonDisabled = !isButtonDisabled ;
});
}
@override
Widget build(BuildContext context) {
Column taskScreen = Column(
children: <Widget>[
ItemBought(), //STATEFUL widget which contains another Stateful widget addImage()
//some other implementation
FlatButton(
color: Colors.red,
textColor: Colors.black,
shape: new RoundedRectangleBorder(borderRadius: new BorderRadius.circular(20.0)),
disabledColor: Color(0XFFf9c3c1),
disabledTextColor: Colors.white,
padding: EdgeInsets.all(8.0),
splashColor: Colors.red[400],
onPressed: isButtonDisabled ? null : _completePage
,
child: Text(
"Completed",
style: TextStyle(color: Colors.white, fontSize: 15.0, fontWeight: FontWeight.bold),
),
)
,
],
)
]);
return taskScreen;
}
}
我要从中更改FlatButton的状态的小部件:
import 'dart:async';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:flutter_convertor/task.dart';
class addImage extends StatefulWidget{
@override
_addImageState createState() => _addImageState();
}
class _addImageState extends State<addImage> {
Column itemDetails = Column(
//some implementation
);
Column OPObservations = Column(
children: <Widget>[
//otherlayout widgets
Padding(
padding: const EdgeInsets.only(left: 8.0, right: 8.0),
child: TextField(
onChanged: taskState.formReady(), //GIVES ERROR
maxLines: 6 ,
style: new TextStyle(
fontSize: 13.0,
),
controller: TextEditingController(),
decoration: InputDecoration(
alignLabelWithHint: true,
labelText: "Enter the details",
labelStyle: TextStyle(color: Colors.grey[400], fontSize: 13.0),
contentPadding: EdgeInsets.only(top:8.0, bottom: 8.0,left: 10.0),
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.grey[400]),
borderRadius: BorderRadius.all(Radius.circular(8.0))),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.red),
)
)
),
),
],
);
@override
Widget build(BuildContext context) {
return Container(
child: _image == null ? Column (
//some implementation
)
: Column(
children: <Widget>[
itemDetails,
OPObservations,
]
)
);
}
}
答案 0 :(得分:0)
没有阅读您的代码(对不起),但是根据您的问题,有两种可能的方法:
1)您可以遵循提升状态的原则,并将按钮的状态存储在其他两个组件的父窗口小部件中。
2)您实现了(公共)方法来更改按钮的状态,并从另一个小部件中调用它。您可以通过分配按钮小部件GlobalKey来访问该方法,然后执行类似buttonKey.currentState.changeStateMethod(newState);
的操作。
希望方法帮助之一:)