Flutter:如何将BottomNavigationBar添加到使用Hero创建的屏幕上?

时间:2020-04-28 20:14:37

标签: flutter flutter-bottomnavigation

我试图将bottomNavigationBar放到hero创建的屏幕上。英雄位于page1()和page2()上,它具有singlechildscrollview,其子容器的高度为size.height(MediaQuery size)。

class HeroScreen extends StatefulWidget {

  Object o;
  HeroScreen({Key key,this.o}) : super(key : key);

  @override
  _HeroScreenState createState() => _HeroScreenState(o);
}

class _HeroScreenState extends State<HeroScreenScreen> {

  Object _o;
  int _index;
  final PageController ctr = PageController();

  _HeroScreenState(this._o);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        flexibleSpace: Image(
          image: AssetImage(linktoimage),
          fit: BoxFit.cover,
        ),
        backgroundColor: Colors.transparent,
        title: BorderedText(
          strokeWidth: 3,
          child: Text(_o.name,
            style: TextStyle(fontFamily: 'Righteous', fontSize: 20),),
        ),
        centerTitle: true,
      ),
      body: PageView(
        scrollDirection: Axis.horizontal,
        controller: ctr,
        children: <Widget>[
          page1(),
          page2(),
        ],
      ),
      bottomNavigationBar: BottomNavigationBar(
        currentIndex: _index,
        items: [
          BottomNavigationBarItem(
            icon: Icon(Icons.person),
            title: Text("Test 1"),
            backgroundColor: Colors.amberAccent
          ),
          BottomNavigationBarItem(
              icon: Icon(Icons.backup),
              title: Text("Test 2"),
              backgroundColor: Colors.amberAccent
          ),
          BottomNavigationBarItem(
              icon: Icon(Icons.print),
              title: Text("Test 3"),
              backgroundColor: Colors.amberAccent
          ),
          BottomNavigationBarItem(
              icon: Icon(Icons.done),
              title: Text("Test 4"),
              backgroundColor: Colors.amberAccent
          )
        ],
        onTap: (index){
          setState(() {
            _index = index;
          });
        },
      ),
    );
  }

当用户在另一个带有卡片并由列表视图包裹的屏幕上点击时,我称英雄屏幕为

onTap: () => Navigator.push(context, MaterialPageRoute(builder: (context) => HeroScreen(o: someobjectList[index]))),

但是我得到了这个错误:

The following NoSuchMethodError was thrown building HeroScreen(dirty, dependencies: [MediaQuery], state: _HeroScreenState#7078c):
The method '>' was called on null.
Receiver: null
Tried calling: >(0)

如果我删除bottomNavigationBar,它的工作原理就很好。所以我不知道它是不是尺寸问题还是什么。

编辑:好的,我发现了错误,该错误在currentindex上,以某种方式我忘了在初始化状态下将其设置为0(我刚刚写了_index; ...是的,不要问我如何以及为什么)

0 个答案:

没有答案