从另一个页面调用setState()-Flutter

时间:2020-01-17 07:08:31

标签: flutter dart call setstate

我有一个字符串_value,我想从另一个页面调用它。

这是我的players.dart

class MyWidgetPopup extends StatefulWidget {
  @override
  MyWidgetPopupState createState() => MyWidgetPopupState();
}

enum Answers{FIRST,SECOND}



class MyWidgetPopupState extends State<MyWidgetPopup> {      

String _value = '';

  void _setValue(String value) => setState(() => _value = value);

  Future selectGk(BuildContext context) async {
    switch(
    await showDialog(

...
...

               {
      case Answers.FIRST:
      _setValue('FIRST TEAM');
      break;
      case Answers.SECOND:
      _setValue('SECOND TEAM');
      break;

    }
}

因此,在players.dart中,我可以使用简单的代码显示选中的球队:Text(_value)

但是我想在select.dart中展示

import 'package:flutter/material.dart';
import 'package:myapp/players.dart';

MyWidgetPopupState dataSource = MyWidgetPopupState();



class Besdortbir extends StatefulWidget {


  @override
  _BesdortbirState createState() => _BesdortbirState();
}

class _BesdortbirState extends State<Besdortbir> {

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text(
            'My Title'),  
          centerTitle: true,
          backgroundColor: Colors.redAccent[700],
          elevation: 0.0,
        ),

  body: Stack(
    fit: StackFit.expand,
    children: <Widget>[
      Container(
        decoration: BoxDecoration(
            image: new DecorationImage(
                image: new AssetImage("assets/field.png"),
                fit: BoxFit.fill
            )
        ),

      ),

//FIRST PLAYER
Container(

  padding: EdgeInsets.only(bottom: 90.0),

child: Row(
  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
  crossAxisAlignment: CrossAxisAlignment.end,
   children: <Widget>[ 
     GestureDetector(
        onTap: (){dataSource.selectGk(context);},  //I have the FIRST TEAM and SECOND TEAM listed here.
        child: Container(
          child: ClipRRect(
            borderRadius: BorderRadius.circular(20.0),
            child: Image.asset('assets/PLUS.png',


                scale: 8),
          ),
        ),
      ),
      Text(//somehow I would like to show which team is selected by user), 
   ],

  ),
),
    ],
  ),
    );

}          

}

我试图回忆起setState()_value和其他一些选项。但是,我无法成功。

0 个答案:

没有答案