Flutter-永久导航栏

时间:2020-10-23 09:05:43

标签: flutter bottomnavigationview

我希望你一切都好。

亲爱的,您会找到一些我正在测试的代码。对于那些正在使用Google Mail应用程序的人,我正在尝试复制它们。它有一个底部导航栏,即使我使用抽屉并选择其中一个选项,也始终在屏幕上。

现在,我收到此错误。我不知道该如何解决。非常感谢您的帮助。

方法'[]'在null上被调用。 接收者:null 尝试致电:

//Home

import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:persistent_bottom_nav_bar/persistent-tab-view.dart';

import 'myDrawer_Widget.dart';


class HomePage extends StatefulWidget {
  HomePage ({Key key}) : super(key : key);

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

class _HomePageState extends State<HomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      drawer:  new MyMenu(),
      appBar: new AppBar(
        title: new Text('Home Page'),
      ),
      body: Column(
        children: [
          Expanded(
              child: Text("Home Page")
          ),
        ],
      ),
     // bottomNavigationBar: ,
    );
    throw UnimplementedError();
  }
}
//Main
import 'package:flutter/material.dart';


import 'Home.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Persistent Bottom Navigation Bar example project',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: HomePage(),
      initialRoute: '/',
    );
  }
}

//MyDrawer

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import 'View1.dart';
import 'View2.dart';
import 'View3.dart';
import 'View4.dart';

class MyMenu extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Drawer(
      elevation: 5.0,

      child: Column(
        children: [
          Expanded(
            flex: 1,
            child: Container(

              child: UserAccountsDrawerHeader()
            ),
          ),
          Expanded(
            flex: 4,
            child: ListView(
              //padding: EdgeInsets.zero,
              children: [
                ListTile(
                    dense: true,
                    //tileColor: Colors.lightBlue[300],
                    title: new Row(children: <Widget>
                    [new Text("View1",
                        style: new TextStyle(
                            color: Colors.blue[900],
                            fontWeight: FontWeight.bold, fontSize: 13.0),)
                    ],
                      mainAxisAlignment: MainAxisAlignment.start,),

                    onTap: () {
                      Navigator.pop(context);
                      Navigator.push(
                          context,
                          MaterialPageRoute(builder: (context) => View1(),));
                    }),

                ListTile(
                    dense: true,

                    leading: Icon(Icons.home),

                    title: Transform(transform: Matrix4.translationValues(distanceEntreTextAndIcon, alignementTextWithIcon, 0.0),
                        child: new Row(children: <Widget>
                        [new Text("View2",
                            style: new TextStyle(
                                color: Colors.black,
                                fontWeight: FontWeight.normal, fontSize: 13.0),)
                        ],
                          mainAxisAlignment: MainAxisAlignment.start,)
                    ),

                    onTap: () {

                     Navigator.pop(context);
                      Navigator.push(
                          context,
                          MaterialPageRoute(builder: (context) => View2(),));
                    }
                ),

                Divider(),

                ListTile(
                    dense: true,

                    // tileColor: Colors.lightBlue[300],
                    title: new Row(children: <Widget>[new Text("View3",
                      style: new TextStyle(
                          color: Colors.blue[900],
                          fontWeight: FontWeight.bold, fontSize: 13.0),)
                    ], mainAxisAlignment: MainAxisAlignment.start,),

                    onTap: () {
                      Navigator.pop(context);
                      Navigator.push(
                          context,
                          MaterialPageRoute(builder: (context) => View3(),));
                    }
                ),

                Divider(),

                ListTile(
                  dense: true,
                  // tileColor: Colors.lightBlue[300],
                  title: new Row(children: <Widget>[new Text("View4",
                    style: new TextStyle(
                        color: Colors.blue[900],
                        fontWeight: FontWeight.bold, fontSize: 13.0),
                  )
                  ],
                    mainAxisAlignment: MainAxisAlignment.start,),
                    onTap: () {
                      Navigator.pop(context);
                      Navigator.push(
                          context,
                          MaterialPageRoute(builder: (context) => View4(),));
                    }

                ),

              ],
              padding: EdgeInsets.all(0.0),
            ),
          ),
        ],
      ),
    );
  }
}
//persistentBottomNavBar

import 'package:flutter/material.dart';

import 'View1.dart';
import 'View2.dart';
import 'View3.dart';


class PersistentBottomNavBar extends StatefulWidget {
  @override
  _PersistentBottomNavBarState createState() => _PersistentBottomNavBarState();
}

class _PersistentBottomNavBarState extends State<PersistentBottomNavBar> {
  final Key keyOne = PageStorageKey('View1');
  final Key keyTwo = PageStorageKey('View2');
  final Key keyThree = PageStorageKey('View3');


  int currentTab = 0;

  View1 one;
  View2 two;
  View3 three;

  List<Widget> pages;
  Widget currentPage;

  final PageStorageBucket bucket = PageStorageBucket();

  @override
  void initState() {

    one = View1(
      key: keyOne,
    );
    two = View2(
      key: keyTwo,
    );
    three = View3(
      key: keyThree,
    );

    pages = [one, two, three, ];

    currentPage = one;

    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(

      body: PageStorage(
        child: currentPage,
        bucket: bucket,
      ),
      bottomNavigationBar: BottomNavigationBar(
        currentIndex: currentTab,
        onTap: (int index) {
          setState(() {
            currentTab = index;
            currentPage = pages[index];
          });
        },
        type: BottomNavigationBarType.fixed,
        items: <BottomNavigationBarItem>[
           BottomNavigationBarItem(
            icon: currentTab==0? Icon(Icons.home,size: 40.0):Icon(Icons.home,size: 40.0),
            title: Text('View1',
              style: currentTab==0?
              TextStyle(color:(Colors.blue ), fontSize: 12.0):
              TextStyle(color:(Colors.black ), fontSize: 12.0)) ),


          BottomNavigationBarItem(
            icon: currentTab==1? Icon(Icons.save,size: 40.0):Icon(Icons.save,size: 40.0),
                title: Text('View2',
                  style: currentTab==1?
                TextStyle(color:(Colors.blue ), fontSize: 12.0):
                TextStyle(color:(Colors.black ), fontSize: 12.0)) ),

          BottomNavigationBarItem(
          icon: currentTab==2? Icon(Icons.insert_invitation,size: 40.0):Icon(Icons.insert_invitation,size: 40.0),
          title: new Text("View3",
          style: currentTab==2?
               TextStyle(color:(Colors.blue ), fontSize: 12.0):
               TextStyle(color:(Colors.black ), fontSize: 12.0)) ),
        ],
      ),
    );
  }
}
//View1
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:persistent_bottom_nav_bar/persistent-tab-view.dart';

import 'myDrawer_Widget.dart';


class View1 extends StatefulWidget {
  View1 ({Key key}) : super(key : key);

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

class _View1State extends State<View1> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      drawer:  new MyMenu(),
      appBar: new AppBar(
        title: new Text('View1'),
      ),
      body: Column(
        children: [
          Expanded(
              child: Text("View1")
          ),
        ],
      ),
      bottomNavigationBar: PersistentBottomNavBar(),
    );
    throw UnimplementedError();
  }
}

class BottomNavB extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return PersistentBottomNavBar();
  }
  void initState() {}
}
//View2

import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';

import 'myDrawer_Widget.dart';


class View2 extends StatefulWidget {
  View2 ({Key key}) : super(key : key);

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

class _View2State extends State<View2> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      drawer:  new MyMenu(),
      appBar: new AppBar(
        title: new Text('View2'),
      ),
      body: Column(
        children: [
          Expanded(
              child: Text("View2")
          ),
        ],
      ),
      //bottomNavigationBar: MyBottomAppBar(),
    );
    throw UnimplementedError();
  }
}
//View3
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';

import 'myDrawer_Widget.dart';


class View3 extends StatefulWidget {
  View3 ({Key key}) : super(key : key);

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

class _View3State extends State<View3> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      drawer:  new MyMenu(),
      appBar: new AppBar(
        title: new Text('View3'),
      ),
      body: Column(
        children: [
          Expanded(
              child: Text("View3")
          ),
        ],
      ),
      //bottomNavigationBar: MyBottomAppBar(),
    );
    throw UnimplementedError();
  }
}
//View4
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';

import 'myDrawer_Widget.dart';


class View4 extends StatefulWidget {
  View4 ({Key key}) : super(key : key);

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

class _View4State extends State<View4> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      drawer:  new MyMenu(),
      appBar: new AppBar(
        title: new Text('View4'),
      ),
      body: Column(
        children: [
          Expanded(
            child: Text("View4")
          ),
        ],
      ),
      //bottomNavigationBar: MyBottomAppBar(),
    );
    throw UnimplementedError();
  }
}
//View5
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';

import 'myDrawer_Widget.dart';


class View5 extends StatefulWidget {
  View5 ({Key key}) : super(key : key);

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

class _View5State extends State<View5> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      drawer:  new MyMenu(),
      appBar: new AppBar(
        title: new Text('View4'),
      ),
      body: Column(
        children: [
          Expanded(
              child: Text("View4")
          ),
        ],
      ),
      //bottomNavigationBar: MyBottomAppBar(),
    );
    throw UnimplementedError();
  }
}

1 个答案:

答案 0 :(得分:1)

由于NoSuchMethodError: The method '[]' was called on null告诉您已将索引([])运算符调用为null,因此您需要调查堆栈跟踪。

可能重复的问题 flutter - The method '[]' was called on null (parse json)