繁体   English   中英

无法调用另一个 dart 文件中的方法

[英]Unable to call a method in another dart file

我有一个带有 IndexedStack 的 dart 文件和同一文件中的以下 function 来更改堆栈。

文件方法如下-

  class RootApp extends StatefulWidget  with selectedTab {
  @override
  _RootAppState createState() => _RootAppState();
}


class _RootAppState extends State<RootApp> {
  int pageIndex = 0;
  List<Widget> pages = [
    ......
  ];

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    ....

    return AnimatedBottomNavigationBar(
      .......
      onTap: (index) {
        selectedTab(index);
      },
    );
  }

  selectedTab(index) {
    setState(() {
      pageIndex = index;
    });
  }
}

还有另一个 dart 文件,我想从中调用值为 0 的 selectedTab 方法。另一个文件如下---

class CreatBudgetPage extends StatefulWidget {
  @override
  _CreatBudgetPageState createState() => _CreatBudgetPageState();
}

class _CreatBudgetPageState extends State<CreatBudgetPage> {
  .......
    FirebaseFirestore.instance
        .collection('expenses/' + userId + '/' + todayDate)
        .add({
   ....
    }).then((_) {
      print("collection created");
      void rootApp() => selectedTab(0);
    }).catchError((error) {
      print(error);
    });
  }

我如何从其他 dart 文件中调用此方法?

PS:我是新手

您可以将 function 回调作为参数从源 class 传递到预期的 class 并根据需要调用 function。

这是一个简单的例子。

class Example extends StatefulWidget {
  const Example({Key? key}) : super(key: key);

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

class _ExampleState extends State<Example> {
  int pageIndex = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('BottomNavigationBar Sample'),
      ),
      body: IndexedStack(
        index: pageIndex,
        children: [
          Tab1Page(),
          Tab2Page(
            /// solution 1
            onPressed1: () {
              selectedTab(0);
            },

            /// solution 2
            // onPressed2: selectedTab,
          ),
        ],
      ),
      bottomNavigationBar: BottomNavigationBar(
        items: const <BottomNavigationBarItem>[
          BottomNavigationBarItem(
            icon: Icon(Icons.home),
            label: 'TAB 1',
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.business),
            label: 'TAB 2',
          ),
        ],
        currentIndex: pageIndex,
        selectedItemColor: Colors.amber[800],
        onTap: selectedTab,
      ),
    );
  }

  selectedTab(index) {
    setState(() {
      pageIndex = index;
    });
  }
}

class Tab1Page extends StatelessWidget {
  const Tab1Page({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.red,
      child: Center(
        child: Text(
          "Tab1",
          style: TextStyle(fontSize: 20),
        ),
      ),
    );
  }
}

class Tab2Page extends StatelessWidget {
  final VoidCallback? onPressed1;

  // final Function(int)? onPressed2;
  const Tab2Page({
    Key? key

    /// solution 1
    ,
    this.onPressed1,

    /// solution 2
    // this.onPressed2
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.red,
      child: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(
              "Tab2",
              style: TextStyle(fontSize: 20),
            ),
            ElevatedButton(
              onPressed: () {
                /// solution 1
                if (onPressed1 != null) {
                  onPressed1!();
                }

                /// solution 2
                // if(onPressed2!=null){
                //   onPressed2!(0);
                //
                // }
              },
              child: Text("click to navigate to another tab",
                  style: TextStyle(fontSize: 20)),
            ),
          ],
        ),
      ),
    );
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM