断言失败:第3722行第12行:' _state._element == null':不是真的

时间:2018-06-15 16:16:28

标签: user-interface flutter flutter-layout

我在底部导航栏中工作..但是当我尝试使用第二页时显示错误..那里(抛出另一个例外:' package:flutter / src / widgets / framework.dart' :失败的断言:第3722行第12行:' _state._element == null':不是真的。 )..我的页面编码如下:

主要飞镖是

import 'package:flutter/material.dart';
import 'package:typeolx/MainPage.dart';
import 'package:typeolx/colors.dart';
import 'package:typeolx/main_bottom_nav_bar.dart';
import 'package:typeolx/chatlistpage.dart';

void main() => runApp(
    new MaterialApp(
      theme: buildThemeData(),
      home: MainPage(),
    ));
ThemeData buildThemeData() {
  final baseTheme = ThemeData(
    fontFamily: "FiraCode",
  );

  return baseTheme.copyWith(
    primaryColor: kPrimaryColor,
    primaryColorDark: kPrimaryDark,
    primaryColorLight: kPrimaryLight,
    accentColor: kSecondaryColor,
    bottomAppBarColor: kSecondaryDark,
    buttonColor: kSecondaryColor,
    sliderTheme: SliderThemeData.fromPrimaryColors(
      primaryColor: kPrimaryColor,
      primaryColorDark: kPrimaryDark,
      primaryColorLight: kPrimaryLight,
      valueIndicatorTextStyle: TextStyle(),

    ),
    textTheme: TextTheme().copyWith(
        subhead: TextStyle(
          fontFamily: "SnackerComic",
        )),
  );
}
MainPageState mainPageState = new MainPageState();

class MainPage extends StatefulWidget {
  static final String routeName = '/MainPage';

  @override
  MainPageState createState() => mainPageState;
}


class MainPageState extends State<MainPage>
    with SingleTickerProviderStateMixin {

  void _rebuild() {
    setState(() {});
  }






  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      backgroundColor:
      currentIndex == 0 ? Theme.of(context).selectedRowColor : Colors.white,
      body: new SafeArea(
        child:
        currentIndex == 0 ? new MainScreen() : new ChatListPage(),
      ),
      bottomNavigationBar: new MainBottomNavBar(_rebuild),
      floatingActionButton:
      currentIndex != 0 ? null : buildSellButton(Icons.add_a_photo, 'eytrtv'),
      floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
    );
  }

  FloatingActionButton buildSellButton(IconData icon, String text) {
    return new FloatingActionButton.extended(
      onPressed: () {
        // TODO:
      },
      icon: new Icon(icon),
      label: new Text(text),
    );
  }


}

mainPage

import 'package:flutter/material.dart';
import 'package:typeolx/view1.dart';

class MainScreen extends StatefulWidget {


  @override
  MainScreenState createState() => new MainScreenState();
}

class MainScreenState extends State<MainScreen> {



  @override
    Widget build(BuildContext context) {
      return new Scaffold(
        body:new Stack(
            children: <Widget>[
        new Container(
        decoration: new BoxDecoration(
        image: new DecorationImage(
        image: new AssetImage("assets/shopping.jpg"),
        fit: BoxFit.cover,

      ),
    ),
    ),
        new SingleChildScrollView(
          child: new Center(
            child: new Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                new Padding(padding: const EdgeInsets.only(top: 5.0),
                  child: new Row(
                    mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                    children: <Widget>[
                      new InkWell(
                        onTap: () => onProductTapped(),
                        child: new Column(
                          children: <Widget>[
                            new CircleAvatar(
                              child: new Icon(Icons.local_offer, color: Colors.white),
                              backgroundColor: Colors.black,
                              radius: MediaQuery.of(context).orientation == Orientation.portrait
                                  ? MediaQuery.of(context).size.width / 8
                                  : MediaQuery.of(context).size.width / 12,
                            ),
                            new Padding(
                              padding: const EdgeInsets.only(top: 5.0),
                              child: new Text("aaa",
                                maxLines: 2,
                                overflow: TextOverflow.ellipsis,
                                style: new TextStyle(color: Colors.white, fontSize: 14.0,fontStyle: FontStyle.italic,decorationColor: Colors.black87),
                              ),
                            )
                          ],
                        ),
                      ),
                      new InkWell(
                        onTap: () {
                        },
                        child: new Column(
                          children: <Widget>[
                            new CircleAvatar(
                              child: new Icon(Icons.local_offer, color: Colors.white),
                              backgroundColor: Colors.black,
                              radius: MediaQuery.of(context).orientation == Orientation.portrait
                                  ? MediaQuery.of(context).size.width / 8
                                  : MediaQuery.of(context).size.width / 12,
                            ),
                            new Padding(
                              padding: const EdgeInsets.only(top: 5.0),
                              child: new Text("cccc",
                                maxLines: 2,
                                overflow: TextOverflow.ellipsis,
                                style: new TextStyle(color: Colors.white, fontSize: 14.0,fontStyle: FontStyle.italic,decorationColor: Colors.black87),
                              ),
                            )
                          ],
                        ),
                      ),
                    ],
                  ),),
                new Padding(padding: const EdgeInsets.only(top: 5.0),
                  child: new Row(
                    mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                    children: <Widget>[
                      new InkWell(
                        onTap: () {
                        },
                        child: new Column(
                          children: <Widget>[
                            new CircleAvatar(
                              child: new Icon(Icons.local_offer, color: Colors.white),
                              backgroundColor: Colors.black,
                              radius: MediaQuery.of(context).orientation == Orientation.portrait
                                  ? MediaQuery.of(context).size.width / 8
                                  : MediaQuery.of(context).size.width / 12,
                            ),
                            new Padding(
                              padding: const EdgeInsets.only(top: 5.0),
                              child: new Text("fffff",
                                maxLines: 2,
                                overflow: TextOverflow.ellipsis,
                                style: new TextStyle(color: Colors.white, fontSize: 14.0,fontStyle: FontStyle.italic,decorationColor: Colors.black87),
                              ),
                            )
                          ],
                        ),
                      ),
                      new InkWell(
                        onTap: () {
                        },
                        child: new Column(
                          children: <Widget>[
                            new CircleAvatar(
                              child: new Icon(Icons.local_offer, color: Colors.white),
                              backgroundColor: Colors.black,
                              radius: MediaQuery.of(context).orientation == Orientation.portrait
                                  ? MediaQuery.of(context).size.width / 8
                                  : MediaQuery.of(context).size.width / 12,
                            ),
                            new Padding(
                              padding: const EdgeInsets.only(top: 5.0),
                              child: new Text("T",
                                maxLines: 2,
                                overflow: TextOverflow.ellipsis,
                                style: new TextStyle(color: Colors.white, fontSize: 14.0,fontStyle: FontStyle.italic,decorationColor: Colors.black87),
                              ),
                            )
                          ],
                        ),
                      ),
                    ],
                  ),),
                new Padding(padding: const EdgeInsets.only(top: 5.0),
                  child: new Row(
                    mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                    children: <Widget>[
                      new InkWell(
                        onTap: () {
                        },
                        child: new Column(
                          children: <Widget>[
                            new CircleAvatar(
                              child: new Icon(Icons.local_offer, color: Colors.white),
                              backgroundColor: Colors.black,
                              radius: MediaQuery.of(context).orientation == Orientation.portrait
                                  ? MediaQuery.of(context).size.width / 8
                                  : MediaQuery.of(context).size.width / 12,
                            ),
                            new Padding(
                              padding: const EdgeInsets.only(top: 5.0),
                              child: new Text("Loca",
                                maxLines: 2,
                                overflow: TextOverflow.ellipsis,
                                style: new TextStyle(color: Colors.white, fontSize: 14.0,fontStyle: FontStyle.italic,decorationColor: Colors.black),
                              ),
                            )
                          ],
                        ),
                      ),
                      new InkWell(
                        onTap: () {
                        },
                        child: new Column(
                          children: <Widget>[
                            new CircleAvatar(
                              child: new Icon(Icons.local_offer, color: Colors.white),
                              backgroundColor: Colors.black,
                              radius: MediaQuery.of(context).orientation == Orientation.portrait
                                  ? MediaQuery.of(context).size.width / 8
                                  : MediaQuery.of(context).size.width / 12,
                            ),
                            new Padding(
                              padding: const EdgeInsets.only(top: 5.0),
                              child: new Text("titute",
                                maxLines: 2,
                                overflow: TextOverflow.ellipsis,
                                style: new TextStyle(color: Colors.white, fontSize: 14.0,fontStyle: FontStyle.italic,decorationColor: Colors.black87),
                              ),
                            )
                          ],
                        ),
                      ),
                    ],
                  ),),
                new Padding(padding: const EdgeInsets.only(top: 5.0),
                  child: new Row(
                    mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                    children: <Widget>[
                      new InkWell(
                        onTap: () {
                        },
                        child: new Column(
                          children: <Widget>[
                            new CircleAvatar(
                              child: new Icon(Icons.local_offer, color: Colors.white),
                              backgroundColor: Colors.black,
                              radius: MediaQuery.of(context).orientation == Orientation.portrait
                                  ? MediaQuery.of(context).size.width / 8
                                  : MediaQuery.of(context).size.width / 12,
                            ),
                            new Padding(
                              padding: const EdgeInsets.only(top: 5.0),
                              child: new Text("Homhings",
                                maxLines: 2,
                                overflow: TextOverflow.ellipsis,
                                style: new TextStyle(color: Colors.white, fontSize: 14.0,fontStyle: FontStyle.italic,decorationColor: Colors.black87),
                              ),
                            )
                          ],
                        ),
                      ),
                      new InkWell(
                        onTap: () {
                        },
                        child: new Column(
                          children: <Widget>[
                            new CircleAvatar(
                              child: new Icon(Icons.local_offer, color: Colors.white),
                              backgroundColor: Colors.black,
                              radius: MediaQuery.of(context).orientation == Orientation.portrait
                                  ? MediaQuery.of(context).size.width / 8
                                  : MediaQuery.of(context).size.width / 12,
                            ),
                            new Padding(
                              padding: const EdgeInsets.only(top: 5.0),
                              child: new Text("Lovice",
                                maxLines: 2,
                                overflow: TextOverflow.ellipsis,
                                style: new TextStyle(color: Colors.white, fontSize: 14.0,fontStyle: FontStyle.italic,decorationColor: Colors.black87),
                              ),
                            )
                          ],
                        ),
                      ),
                    ],
                  ),),
                new Padding(padding: const EdgeInsets.only(top: 5.0),
                  child: new Row(
                    mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                    children: <Widget>[
                      new InkWell(
                        onTap: () {
                        },
                        child: new Column(
                          children: <Widget>[
                            new CircleAvatar(
                              child: new Icon(Icons.local_offer, color: Colors.white),
                              backgroundColor: Colors.black,
                              radius: MediaQuery.of(context).orientation == Orientation.portrait
                                  ? MediaQuery.of(context).size.width / 8
                                  : MediaQuery.of(context).size.width / 12,
                            ),
                            new Padding(
                              padding: const EdgeInsets.only(top: 5.0),
                              child: new Text("obs",
                                maxLines: 2,
                                overflow: TextOverflow.ellipsis,
                                style: new TextStyle(color: Colors.white, fontSize: 14.0,fontStyle: FontStyle.italic,decorationColor: Colors.black87),
                              ),
                            )
                          ],
                        ),
                      ),
                      new InkWell(
                        onTap: () {
                        },
                        child: new Column(
                          children: <Widget>[
                            new CircleAvatar(
                              child: new Icon(Icons.local_offer, color: Colors.white),
                              backgroundColor: Colors.black,
                              radius: MediaQuery.of(context).orientation == Orientation.portrait
                                  ? MediaQuery.of(context).size.width / 8
                                  : MediaQuery.of(context).size.width / 12,
                            ),
                            new Padding(
                              padding: const EdgeInsets.only(top: 5.0),
                              child: new Text("Houury",
                                maxLines: 2,
                                overflow: TextOverflow.ellipsis,
                                style: new TextStyle(color: Colors.white, fontSize: 14.0,fontStyle: FontStyle.italic,decorationColor: Colors.black87),
                              ),
                            )
                          ],
                        ),
                      ),
                    ],
                  ),),
              ],
            ),
          ),
        )
    ]
        )


      );
    }

  onProductTapped() {
    setState(() {
      Navigator.of(context).push(new MaterialPageRoute(
          builder: (context) => new ViewOne()));
    });

  }

}

main_bottom_nav_bar

import 'package:flutter/material.dart';

MainBottomNavBarState mainBottomNavBarState = new MainBottomNavBarState();
int unReadMessageCount = 0;
int currentIndex = 0;

class MainBottomNavBar extends StatefulWidget {
  MainBottomNavBar(this.rebuild);

  final VoidCallback rebuild;

  @override
  MainBottomNavBarState createState() {
    return mainBottomNavBarState;
  }

  BottomNavigationBarItem buildBottomNavigationBarItem(BuildContext context,
      String title, IconData icon, bool shouldShowUnreadCount, int pos) {
    return new BottomNavigationBarItem(
      icon: !shouldShowUnreadCount
          ? new Icon(
        icon,
        size: 30.0,
        color: currentIndex == pos
            ? Theme.of(context).primaryColorDark
            : Theme.of(context).accentColor.withOpacity(0.7),
      )
          : new Stack(
        children: <Widget>[
          new Icon(
            icon,
            size: 30.0,
            color: currentIndex == pos
                ? Theme.of(context).primaryColorDark
                : Theme.of(context).accentColor.withOpacity(0.7),
          ),
          new Positioned(
            right: 0.0,
            top: 0.0,
            child: unReadMessageCount > 0
                ? new CircleAvatar(
              child: new Text(
                '$unReadMessageCount',
                style: new TextStyle(
                    fontSize: 11.0, color: Colors.white),
              ),
              backgroundColor: Colors.red,
              radius: 9.0,
            )
                : new Container(),
          ),
        ],
      ),
      title: new Text(title,
          style: new TextStyle(
            fontSize: 16.0,
            fontWeight: FontWeight.w600,
            color: currentIndex == pos
                ? Theme.of(context).primaryColorDark
                : Theme.of(context).accentColor.withOpacity(0.7),
          )),
    );
  }
}

class MainBottomNavBarState extends State<MainBottomNavBar> {


  onNavItemTapped(int pos) {
    currentIndex = pos;
    widget.rebuild();
  }

  @override
  Widget build(BuildContext context) {
    return new BottomNavigationBar(
      currentIndex: currentIndex,
      onTap: (selectedPosition) => onNavItemTapped(selectedPosition),
      items: <BottomNavigationBarItem>[
        widget.buildBottomNavigationBarItem(
            context, 'home', Icons.home, false, 0),
        widget.buildBottomNavigationBarItem(
            context, 'Chats', Icons.chat, true, 1),
      ],
    );
  }

}

ViewOne页面

import 'package:flutter/material.dart';
import 'package:flutter_statusbarcolor/flutter_statusbarcolor.dart';
import 'package:screentheme/screentheme.dart';
import 'package:flutter/services.dart';
import 'package:typeolx/chatlistpage.dart';
import 'package:typeolx/main_bottom_nav_bar.dart';
import 'package:typeolx/viewMain.dart';




class ViewOne extends StatefulWidget {
  @override
  ViewOneState createState() => new ViewOneState();
}

class ViewOneState extends State<ViewOne> with SingleTickerProviderStateMixin {

  void _rebuild1() {
    setState(() {});
  }

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


  changeStatusBarColor() async {
    try {
      await FlutterStatusbarcolor.setStatusBarColor(Colors.white);
      await ScreenTheme.darkStatusBar();
    } on PlatformException catch (e) {
      print(e);
    }
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      backgroundColor:
      currentIndex == 0 ? Theme
          .of(context)
          .selectedRowColor : Colors.white,
      body: new SafeArea(
        child:
        currentIndex == 0 ? new ViewMain() : new ChatListPage(),
      ),
      bottomNavigationBar: new MainBottomNavBar(_rebuild1),
      floatingActionButton:
      currentIndex != 0 ? null : buildSellButton(Icons.add_a_photo, 'Add Post'),
      floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
    );
  }


  FloatingActionButton buildSellButton(IconData icon, String text) {
    return new FloatingActionButton.extended(
      onPressed: () {},
      icon: new Icon(icon),
      label: new Text(text),
    );
  }
}

0 个答案:

没有答案