需要帮助在 flutter 中传递数据

问题描述 投票:0回答:1

在代码中,有一个 StatefulWidget 'SpectatorPage',我希望页面看到 StatefulWidgets 'ChatPage' 的所有小部件方法 'chatScreenContainer'。 我还希望当“ChatPage”决定重建时,SpectatorPage 能够看到“chatScreenContainer”的重建。 这是我的代码:

class SpectatorPage extends StatefulWidget {

  Game game;
  SpectatorPage(Key? key,this.game) : super(key: key);

  @override
  State<SpectatorPage> createState() => SpectatorPageState();
}

class SpectatorPageState extends State<SpectatorPage> {

  List<ChatPage> runningMatches = [];

  ScrollController _scrollController = ScrollController();

  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      controller: _scrollController,
      child: Container(
        alignment: Alignment.center,
        child: GridView.builder(
          gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: 2
          ),
          itemCount: widget.game.players.length,
          itemBuilder: (context, index) {
            return Container(
              child: runningMatches[index],
            );
          },
        ),
      ),
    );
  }
}

然后是“聊天页面”:

class ChatPage extends StatefulWidget {
  // Player p1;
  // Player p2;

  // ChatPage(this.p1, this.p2);

  @override
  State<ChatPage> createState() => ChatPageState();
}

class ChatPageState extends State<ChatPage> {

  bool isYourTurn = Random().nextBool();
  List<String> messages_stack = [];
  List<Widget> widget_stack = [];

  List<String> current_props = [];

  @override
  void initState() {
    
    super.initState();
    sendMessage('Salut ça va ?');
  }

  @override
  Widget build(BuildContext context) {
    List<String> propositions = []; // generateProposition();
    return Container(
      color: Color.fromARGB(255, 236, 131, 166),
      child: Container(
        color: const Color.fromARGB(255, 236, 131, 166),
        width: 1000,
        height: double.infinity,
        child: Column(
          mainAxisSize: MainAxisSize.max,
          children: [
            SizedBox(height: 36.0),
            chatScreenContainer(),
            SizedBox(height: 4.0),
            Container(
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(24.0),
                color: Colors.pinkAccent,
              ),
              width: 340,
              height: 40,
              child: Row(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  SizedBox(width: 10),
                  Container(
                    decoration: BoxDecoration(
                      color: Color.fromARGB(255, 236, 131, 166),
                      borderRadius: BorderRadius.circular(20.0),
                    ),
                    width: 160,
                    height: 16,
                    child: Padding(
                      padding: const EdgeInsets.only(top: 3.0, left: 6.0),
                      child: Text('...', style: TextStyle(color: Color.fromARGB(255, 235, 2, 80),fontSize: 10.0, decoration: TextDecoration.none),),
                    ),
                  ),
                  const Padding(padding: EdgeInsets.symmetric(horizontal: 42.0)),
                  Container(
                    width: 30,
                    height: 30,
                    child: ElevatedButton(
                      onPressed: () {
                        setState(() {
                          showDialog(
                            context: context,
                            builder: (context) {
                              return AlertDialog(
                                content: Container(
                                  alignment: Alignment.center,
                                  decoration: BoxDecoration(
                                    color: Colors.white,
                                    borderRadius: BorderRadius.circular(24.0),
                                  ),
                                  child: Column(
                                    children: [
                                      
                                    ],
                                  ),
                                ),
                              );
                            });
                        }); 
                      },
                      child: Icon(Icons.lightbulb, color: Colors.yellow, size: 16),
                      style: ElevatedButton.styleFrom(
                        elevation: 8.0,
                        shape: CircleBorder(),
                        padding: EdgeInsets.zero,
                        backgroundColor: const Color.fromARGB(255, 236, 131, 166),
                      ),
                    ),
                  ),
                  SizedBox(width: 10),
                  Container(
                    width: 30,
                    height: 30,
                    child: ElevatedButton(
                      onPressed: () {
                        
                      },
                      child: Icon(Icons.send, color: Colors.white, size: 16,),
                        style: ElevatedButton.styleFrom(
                        elevation: 8.0,
                        shape: CircleBorder(),
                        padding: EdgeInsets.zero,
                        backgroundColor: const Color.fromARGB(255, 236, 131, 166), // <-- Button color
                        
                      ),
                    ),
                  ),
                ],
              ),
            ),
            SizedBox(height: 22.0),
            Container(
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(24.0),
                color: Colors.pinkAccent,
              ),
              width: 360,
              height: 220,
              child: ListView(
                children: <Widget>[
                  
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget chatScreenContainer() {
    return SingleChildScrollView(
      scrollDirection: Axis.vertical,
      child: Container(
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(24.0),
          color: Colors.pinkAccent,
        ),
        margin: const EdgeInsets.all(16.0),
        width: 400,
        height: 460,
        child: Column(
          children: <Widget>[
            Container(
              width: 360,
              height: 440,
              child: Container(
                /* child: ListView.builder(
                  itemCount: widget_stack.length,
                  itemBuilder: ((context, index) {
                    return widget_stack[index];
                  })
                ), */
              )
            )
          ],
        ),
      ),
    );
  }
flutter dart widget
1个回答
0
投票

将此方法添加到您的“SpectatorPageState”

 reload (){
    ChatPage(reload:reload);
    if(mounted){
      setState(() {
      });
    }
  }

将此添加到您的“ChatPage”声明中

  const ChatPage({Key? key, required this.reload ,}) : super(key: key);
 final Function() reload;
  @override
  State<ChatPage> createState() => ChatPageState();
}

当您在 runningMatches 中初始化“ChatPage”时

runningMatches.add(ChatPage(reload:reload));

当您需要在“聊天页面”重新加载时

widget.reload();
© www.soinside.com 2019 - 2024. All rights reserved.