2017-05-25 20 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); 
)); 

あなたはThirdPageの最終メンバーとして結果Listを格納しwidget.resultsとしてThirdPageStateでそれにアクセスする必要があります。

通常、メンバー変数としてThirdPageStateへの直接参照を格納していないはずです。SecondPageState状態が別の状態への参照を取得することは可能ですが、GlobalKeyを使用し、currentStateを呼び出すことができます。可能であれば、データをコンストラクタ引数として渡すだけです。

関連する問題