Откройте новую страницу из TabBar и сбросьте индекс TabBar при возвращении - PullRequest
0 голосов
/ 02 мая 2020

В моем приложении есть CupertinoTabBar. Моя цель состоит в том, чтобы открыть новую страницу, нажав на вкладку (без панели вкладок внизу), и я могу сделать это, вызвав метод Navigator.pushNamed () в свойстве onTap CupertinoTabBar, проверяющего индекс (например, индекс 1). ).

Я хотел бы сбросить индекс вкладки (или вспомнить старый) при вызове Navigation.pop (context) с новой страницы. Я не знаю, является ли это правильным подходом к тому, что я хочу реализовать, и я не знаю, как это сделать.

Любое предложение будет оценено.

Это Виджет вкладок:

import 'package:RunControl/pages/home.dart';
import 'package:RunControl/pages/profile.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class Tabs extends StatefulWidget {

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

class _TabsPageState extends State<Tabs> {
  int index;

  @override
  void initState() {
    setIndex(0);
  }

  setIndex(int ind){
    setState((){this.index=ind;});
  }

  @override
  Widget build(BuildContext context) {
    return CupertinoTabScaffold(
      controller: CupertinoTabController(initialIndex: index),
      tabBar: CupertinoTabBar(
        onTap:(index){
        if (index==1) Navigator.pushNamed(context,"/prerun");},
      backgroundColor:  Color(0xff383838),
        iconSize: 24,
        items: <BottomNavigationBarItem>[
          BottomNavigationBarItem(
            icon: ImageIcon(
              AssetImage("assets/icons/home.png"),
              color: Color(0xFFf0c306),
            ),
            activeIcon: ImageIcon(
              AssetImage("assets/icons/home_filled.png"),
              color: Color(0xFFf0c306),
            ),
          ),
          BottomNavigationBarItem(
            icon: ImageIcon(
              AssetImage("assets/icons/run.png"),
              color: Color(0xFFf0c306),
            ),
            activeIcon: ImageIcon(
              AssetImage("assets/icons/run_filled.png"),
              color: Color(0xFFf0c306),
            ),
          ),
          BottomNavigationBarItem(
            icon: ImageIcon(
              AssetImage("assets/icons/user.png"),
              color: Color(0xFFf0c306),
            ),
            activeIcon: ImageIcon(
              AssetImage("assets/icons/user_filled.png"),
              color: Color(0xFFf0c306),
            ),
          ),
        ],
      ),
      tabBuilder: (BuildContext context, int index) {
        switch (index) {
          case 0:
            return Home();
            break;
          case 1:
            return Container();
            break;
          case 2:
            return ProfilePage();         
        }
      },
    );
  }       
}

А это виджет открытой страницы:

import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'currentmap.dart';

class PreRunPage extends StatefulWidget {
  @override
  _PreRunPageState createState() => _PreRunPageState();
}

class _PreRunPageState extends State<PreRunPage> {
  Icon customIcon;
  @override
  void initState() {
    super.initState();
  }

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Color(0xff212121),
        body: Stack(
          children: <Widget>[
            CurrentMap( currentLocation: null),
            Align(
              alignment: Alignment.bottomCenter,
              child: Container(
                margin: EdgeInsets.all(10),
                width: 80.0,
                height: 80.0,
                child: FloatingActionButton(
                  heroTag: null,
                  backgroundColor: Color(0xFFf0c306),
                  onPressed: () {
                    Navigator.pushNamed(context, "/run");
                  },
                  child: Text("START",
                      textAlign: TextAlign.center,
                      style: TextStyle(
                          color: Colors.white,
                          fontSize: 18,
                          fontWeight: FontWeight.bold)),
                ),
              ),
            ),
            Align(
              alignment: Alignment.topRight,
              child: Container(
                margin: EdgeInsets.all(10),
                width: 50.0,
                height: 50.0,
                child: FloatingActionButton(
                  heroTag: null,
                  backgroundColor: Colors.blueGrey,
                  onPressed: () {
                    Navigator.pop(context);
                  },
                  child: Icon(
                    Icons.arrow_back,
                    size: 30.0,
                  ),
                ),
              ),
            ),
            FlatButton(
              textColor: Colors.white,
              onPressed: () {
                Navigator.pushNamed(context, "/timer");
              },
              child: Text(
                "Flat Button",
              ),
            ),
          ],
        ));
  }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...