2017-05-25 111 views
0

目前我是自我飞镖,主要是应用程序开发与扑。我遇到了将元素存储到字符串数组中,从另一个类获取数组并成功显示的问题。我的目标是建立一个简单的应用程序,提出3个问题并以非常有趣的方式显示它们!将元素存入数组

class SecondPage extends StatefulWidget 
{ 
    @override 
    SecondPageState createState() => new SecondPageState(); 
} 

class SecondPageState extends State<SecondPage> 
{ 
    final TextEditingController controller = new TextEditingController(); 
    int counter = 0; 
    var ask = ["What is your name?", "How old are you?", "What is your favorite hobby?"]; 
    var results = ["","",""]; 
    @override 
    Widget build(BuildContext context) 
    { 
    return new Scaffold(
     appBar: new AppBar(title: new Text("Tell us about yourself"), backgroundColor: Colors.deepOrange), 
     body: new Container(
     padding: new EdgeInsets.all(20.0), 
     child: new Center(
      child: new Column(
      mainAxisAlignment: MainAxisAlignment.center, 
      children: <Widget>[ 
       new Text(ask[counter], style: new TextStyle(fontSize: 15.0, fontWeight: FontWeight.bold)), 
       new TextField(
       decoration: new InputDecoration(
        hintText: "Type Here" 
       ), 
       onSubmitted: (String str) 
       { 
       setState(() 
       { 
        results[counter] = ask[counter]; //Assigning 
        if(counter < 2) 
        { 
        counter = counter + 1; 
        } 
        else 
        { 
        counter = 0; 
        } 
       }); 
       }, 
      ), 
       new IconButton(
       padding: new EdgeInsets.only(right: 50.0), 
       icon: new Icon(Icons.library_books, size: 70.0, color: Colors.blueAccent), 
        onPressed:()  {Navigator.of(context).pushNamed("/ThirdPage");} 
      ), 
      ] 
     ) 
     ) 
    ) 
    ); 
    } 
} 

class ThirdPageState extends State<ThirdPage> 
{ 
    SecondPageState _second = new SecondPageState(); 

    // ignore: unnecessary_getters_setters 
    SecondPageState get second => _second; 

    // ignore: unnecessary_getters_setters, avoid_return_types_on_setters 
    void set second(SecondPageState second) { 
    _second = second; 
    } 
    @override 
    Widget build(BuildContext context) 
    { 

    //_second.results[0] = "christopher"; 
    return new Scaffold(
    appBar: new AppBar(title: new Text("Your Biography"), backgroundColor: Colors.deepOrange), 
     body: new Container(
     padding: new EdgeInsets.all(20.0), 
     child: new Center(
      child: new Column(
      mainAxisAlignment: MainAxisAlignment.center, 
      children: <Widget>[ 
       new MyCard(
        tittle: new Text(second.results[0], style: new  TextStyle(
         fontSize: 25.0 
       )), 
        icon: new Icon(Icons.favorite, size: 40.0, color: Colors.redAccent) 
      ), 
       new MyCard(
        tittle: new Text(second.results[1], style: new TextStyle(
         fontSize: 25.0 
       )), 
        icon: new Icon(Icons.local_pizza, size: 40.0, color: Colors.brown) 
      ), 
       new MyCard(
        tittle: new Text(second.results[2], style: new TextStyle(
         fontSize: 25.0 
       )), 
        icon: new Icon(Icons.visibility, size: 40.0, color:  Colors.blue) 
     ) 
     ] 
    ) 
    ) 
) 
); 

}}

回答

1

不需要调用Navigator.pushNamed,你应该叫Navigator.push。这将允许您将参数传递给您的ThirdPage构造函数。

Navigator.push(context, new MaterialPageRoute<Null>(
    builder: (context) => new ThirdPage(results: results); 
)); 

您应该将结果List存储为上ThirdPage最后的成员,访问它在ThirdPageState作为widget.results

通常,您应该没有将ThirdPageState作为成员变量存储的SecondPageState。尽管一个州有可能获得对另一个州的参考,但是您希望使用GlobalKey并呼叫currentState,并且如果可以的话,只需传递数据作为构造函数参数就可维护得多。