首页 > 解决方案 > 如何在flutter中将类函数传递给另一个类中的小部件

问题描述

我有 3 页 1,2,3 我想通过单击第二页中实现第一页功能的按钮到达第三页

那么如何将函数传递到第二页

所以这是具有功能的类


class SharedPage extends StatefulWidget {
  _SharedPageState createState() => _SharedPageState();
}

class _SharedPageState extends State<SharedPage> {
  String _information = '';
   void updateInformation(String information) {
    setState(() => _information = information);
  }

  void moveToSecondPage() async {
    final information = await Navigator.push(
      context,
      MaterialPageRoute(builder: (context) {
        return AddShared(); /// this is the page that i want to reach 
      }),
    );
    updateInformation(information);
  }

这是我想将函数传递给的类

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {


  @override
  Widget build(BuildContext context) {
   
    var add = [
      
      FlatButton(
        shape: CircleBorder(),
        child: Icon(
          Icons.add,
          color: Colors.red,
          size: 30,
        ),
        onPressed: () {
          /// i want to implement the MoveToSecondScreen here 
        },
      ),
    
    ];

标签: androidfunctionflutterdartflutter-layout

解决方案


推荐阅读