Как отозвать новый индекс просмотра страниц после закрытия виджета stfl, которого нет в индексе просмотров страниц? - PullRequest
0 голосов
/ 12 июня 2019

У меня есть этот пример, который работает, чтобы перейти на новую индексную страницу, но я не знаю, как вызвать обратный вызов, если мой виджет не находится в просмотре страниц [].

Моя цель - перейти квторой индекс в PageView после закрытия моего «Otherwidget», которого нет в индексе просмотра страниц.

В настоящее время, когда я закрываю Otherwidget, я вызываю виджет Parent, но индекс init равен 1, и я не хочу измененийthis.

Пример:

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'),
            ),
          ),
        );
      }
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...