我想在另一屏幕上单击nextbutton时更改一页的索引值(pictogramindex)。我将简要说明一下,在我的场景中有2个屏幕,第一个屏幕包含图像,它的名称是textfield和nextbutton(i提供的虚拟数据包含图像列表及其名称)的逻辑是,当我们完成文本字段框并单击下一步(在验证之后)时,文本字段值将使用虚拟数据中给出的正确值进行检查,并显示它也提供的同义词。当我们单击下一步按钮时,我们将转到另一个包含正确答案的页面(从第一页传递)和其中的文本字段,当用户单击此页面的下一步按钮时,用户可以写出正确答案(已验证)(till这我的应用程序运行完美]]))我想加载索引已更新(+1)的第一页,该页的索引初始化为0(var pictogramindex = 0)。但是在我的情况下,当返回首页时,索引未更新,它将自动存储初始化值。 我想要的是我想在第二页上单击下一步按钮时更新第一页上的索引。
我的第一个屏幕的源代码显示在这里
class Pictogramscreen extends StatefulWidget { final int length; const Pictogramscreen({Key key, this.length}) : super(key: key); @override _PictogramscreenState createState() => _PictogramscreenState(); } class _PictogramscreenState extends State<Pictogramscreen> { @override final _Key = GlobalKey<FormState>(); Color defaultcolor = Colors.blue[50]; Color trueColor = Colors.green; Color falseColor = Colors.red; Widget defcorrect = Text(''); var pictogramindex = 0; TextEditingController usertitleInput = TextEditingController(); nextPictogram() { setState(() { pictogramindex++; }); } fillColor() { setState(() { usertitleInput.text == pictdata[pictogramindex]['pictcorrectword'] ? defaultcolor = trueColor : defaultcolor = falseColor; }); } correctText() { setState(() { usertitleInput.text == pictdata[pictogramindex]['pictcorrectword'] ? defcorrect = Text(pictdata[pictogramindex]['pictsynonym']) : defcorrect = Text(pictdata[pictogramindex]['pictcorrectword']); }); } reset() { setState(() { defaultcolor = Colors.blue[50]; defcorrect = Text(''); usertitleInput.clear(); }); } void description(BuildContext ctx) { Navigator.of(context).pushNamed('/user-description', arguments: { 'id': pictdata[pictogramindex]['pictid'], 'word': pictdata[pictogramindex]['pictcorrectword'] }); } Widget build(BuildContext context) { int length = pictdata.length; return Scaffold( body: pictogramindex < pictdata.length ? ListView( children: <Widget>[ Container( margin: EdgeInsets.only(top: 20), padding: EdgeInsets.all(15), child: Column( crossAxisAlignment: CrossAxisAlignment.center, children: <Widget>[ Card( margin: EdgeInsets.only(top: 20), child: Image.network( pictdata[pictogramindex]['pictimg']), ), SizedBox( height: 10, ), Text( pictdata[pictogramindex]['pictword'], style: TextStyle( fontSize: 25, ), ), SizedBox( height: 10, ), //Card( //color: Colors.blue, // child: TextField( // decoration: InputDecoration.collapsed( // hintText: 'type here'), //textAlign: TextAlign.center, // onSubmitted: (value) { // usertitleInput = value; // print(usertitleInput); // }, // ), //), Form( key: _Key, child: TextFormField( controller: usertitleInput, validator: (usertitleInput) { if (usertitleInput.isEmpty) { return 'Answer cannot be empty'; } else { return null; } }, textAlign: TextAlign.center, decoration: InputDecoration( enabledBorder: OutlineInputBorder( borderSide: BorderSide(color: Colors.blueAccent), borderRadius: BorderRadius.all( Radius.circular(15), )), labelText: 'Type your Answer', filled: true, fillColor: defaultcolor, ), onFieldSubmitted: (value) { usertitleInput.text = value; fillColor(); correctText(); print(usertitleInput.text); }), ), SizedBox( height: 10, ), defcorrect, SizedBox( height: 10, ), RaisedButton( onPressed: () { if (_Key.currentState.validate()) { description(context); // nextPictogram(); reset(); } // //if (_Key.currentState.validate() == correctText()) { // nextPictogram; // } }, child: Text('Next'), ) ], ), ), ], ) : Center( child: Text('completed'), )); } }
我的第二个屏幕的源代码显示在这里
class Userinputscreen extends StatefulWidget {
final String id;
final String word;
const Userinputscreen({Key key, this.id, this.word}) : super(key: key);
@override
_UserinputscreenState createState() => _UserinputscreenState();
}
class _UserinputscreenState extends State<Userinputscreen> {
final _Keey = GlobalKey<FormState>();
TextEditingController userdescription = TextEditingController();
var pictogramindex;
void nextpict(BuildContext context) {
Navigator.of(context).pushNamed('/main-screen');
}
// void nextpict(BuildContext context, int index) {
// Navigator.push(
// context,
// MaterialPageRoute(
// builder: (ctx) => Pictogramscreen(
// index: i = 0,
// )));
// }
@override
Widget build(BuildContext context) {
final routeArgs =
ModalRoute.of(context).settings.arguments as Map<String, String>;
final correctWord = routeArgs['word'];
return MaterialApp(
home: Scaffold(
body: ListView(children: <Widget>[
Padding(
padding: EdgeInsets.only(top: 50),
child: Center(
child: Container(
padding: EdgeInsets.all(20),
margin: EdgeInsets.only(top: 100),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
correctWord,
style: TextStyle(fontSize: 26),
),
SizedBox(
height: 10,
),
Form(
key: _Keey,
child: TextFormField(
controller: userdescription,
validator: (userdescription) {
if (userdescription.isEmpty) {
return 'Answer cannot be empty';
} else {
return null;
}
},
textAlign: TextAlign.center,
decoration: InputDecoration(
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(color: Colors.blueAccent),
borderRadius: BorderRadius.all(
Radius.circular(15),
)),
labelText: 'Type your Answer',
filled: true,
),
onFieldSubmitted: (value) {
userdescription.text = value;
print(userdescription.text);
}),
),
SizedBox(
height: 10,
),
RaisedButton(
onPressed: () {
if (_Keey.currentState.validate()) {
nextpict(context);
}
},
child: Text('Next'),
)
],
),
),
),
),
])),
);
}
}
我想在另一屏幕上单击nextbutton时更改一页的索引值(象形图索引)。我将简要说明,在我的场景中有2个屏幕,第一个屏幕包含图像,并且...]]
[如果我做对了,您基本上想告诉初始页它的状态在其他地方已更新(索引)。在构建一个简单的应用程序时,我遇到了共享状态的类似问题,因此不得不学习“ BloC” 。对于您来说,简单的解决方案是您需要使应用程序“具有反应性”。
您需要某种状态管理。正如Google Developers Tutorial中所述: