关闭不在网页浏览索引中的stfl小部件后,如何回调新的网页浏览索引?

时间:2019-06-12 09:47:12

标签: flutter callback

我有这个示例,他致力于跳转到新的索引页面,但是如果我的小部件不在综合浏览量[]中,我不知道如何调用回调。

我的目标是在关闭不在页面视图索引中的“ Otherwidget”之后跳到页面视图上的第二个索引。

当前,当我关闭Otherwidget时,我调用了Parent小部件,但是init索引是1,并且我不想更改它。

示例:

class Parent extends StatefulWidget {
  @override
  _ParentState createState() => _ParentState();
}

class _ParentState extends State<Parent> {
  int bottomSelectedIndex = 0;

  List<BottomNavigationBarItem> buildBottomNavBarItems() {
    return [
      BottomNavigationBarItem(
          icon: new Icon(Icons.home), title: new Text('First')),
      BottomNavigationBarItem(
        icon: new Icon(Icons.search),
        title: new Text('Second'),
      ),
    ];
  }

  PageController pageController = PageController(
    initialPage: 0,
    keepPage: true,
  );

  Widget buildPageView() {
    return PageView(
      controller: pageController,
      onPageChanged: (index) {
        pageChanged(index);
      },
      children: <Widget>[
        FirstWidget()
        SecondWidget(),
      ],
    );
  }

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

  void pageChanged(int index) {
    setState(() {
      bottomSelectedIndex = index;
    });
  }

  void bottomTapped(int index) {
    setState(() {
      bottomSelectedIndex = index;
      pageController.animateToPage(index,
          duration: Duration(milliseconds: 500), curve: Curves.ease);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Test'),
      ),
      body: buildPageView(),
      bottomNavigationBar: BottomNavigationBar(
        currentIndex: bottomSelectedIndex,
        onTap: (index) {
          bottomTapped(index);
        },
        items: buildBottomNavBarItems(),
      ),
    );
  }
}

class FirstWidget extends StatefulWidget {
  @override
  _FirstWidgetState createState() => _FirstWidgetState();
}

class _FirstWidgetState extends State<FirstWidget> {
  @override
  Widget build(BuildContext context) {
       return Container(color: Colors.green);
  }
}

class SecondWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(color: Colors.green);
  }
}

class otherwidget extends StatelessWidget {
 final VoidCallback onButtonPressed;

      otherwidget ({@required this.onButtonPressed});
      @override
       Widget build(BuildContext context) {
        return Container(
          color: Colors.red,
          child: Center(
            child: FlatButton(
              onPressed: widget.onButtonPressed,
              child: Text('Go to second page'),
            ),
          ),
        );
      }
    }

0 个答案:

没有答案