代码之家  ›  专栏  ›  技术社区  ›  Nabin Dhakal

失败的断言:第549行pos 12:'child.hasSize':不是真的

  •  0
  • Nabin Dhakal  · 技术社区  · 6 年前

    仪表板 家 新建请求 家 ,的 新请求

    class Dashboard extends StatefulWidget{
    
    @override
     State<StatefulWidget> createState() {
       return _DashboardState();
     }
    
    }
     class _DashboardState extends State<Dashboard>{
    
    int _currentIndex=0;
    bool home=true;
    final tabs=[
     Home(),
     Center(child: Text('History')),
     Center(child: Text('Wallet')),
       Center(child: Text('More')),
      ];
    
     final _title=[
      Center(child: Text("Dashboard"),),
      Center(child:Text("History"),),
      Center(child:Text("Wallet"),),
      Center(child:Text("More"),),];
    
    
      @override
       Widget build(BuildContext context) {
        return Scaffold(
            appBar: home ? null :AppBar(
             title: _title[_currentIndex],
             automaticallyImplyLeading: false,
        ),
        body: Container(
        child: tabs[_currentIndex],
      ),
      bottomNavigationBar: _bottomNavigationBar(),
    );
    }
    
    Widget _bottomNavigationBar(){
     return BottomNavigationBar(
        currentIndex: _currentIndex,
        type: BottomNavigationBarType.fixed,
        backgroundColor: Theme.of(context).primaryColor,
        selectedItemColor: Theme.of(context).accentColor,
        unselectedItemColor: Colors.white,
        items: [
          BottomNavigationBarItem(
            icon: Icon(Icons.home),
            title: Text("Home"),          
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.history),
            title: Text("History"),  
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.account_balance_wallet),
            title: Text("Wallet"), 
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.menu),
            title: Text("More"),
          )
        ],
        onTap: (index){
          setState(() {
            _currentIndex=index;
            if (index==0) {
              home=true;
            }else{
              home=false;
            }
          });
        },
      ) ;
     }
    }
    

    class Home extends StatefulWidget {
      @override
       State<StatefulWidget> createState() {
        return _HomeState();
       }
    }
    
     class _HomeState extends State<Home> {
      int index;
      @override
    void initState() {
    super.initState();
    index = 0;
    }
    
    final pages = [
    NewRequest(),
    new Column(
      children: <Widget>[new Text("Accepted Page")],
    ),
    new Column(
      children: <Widget>[new Text("Cooking Page")],
    ),
    new Column(
      children: <Widget>[new Text("Cooked Page")],
    )
    ];
    
    final tabs = [
    Text('Request'),
    Text('Accepted'),
    Text('Cooking'),
    Text('Cooked'),
    ];
    
    @override
    Widget build(BuildContext context) {
    return new MaterialApp(
      debugShowCheckedModeBanner: false,
      home: new DefaultTabController(
        length: 4,
        initialIndex: 0,
        child: new Scaffold(
            appBar: new PreferredSize(
              preferredSize: Size.fromHeight(56.0),
              child: new Container(
                color: Theme.of(context).primaryColor,
                child: new SafeArea(
                  child: Column(
                    children: <Widget>[
                      new Expanded(
                        child: new Container(
                            child: new TabBar(
                          unselectedLabelColor: Colors.white,
                          labelColor: Theme.of(context).accentColor,
                          indicatorColor: Theme.of(context).accentColor,
                          isScrollable: true,
                          tabs: List<Widget>.generate(4, (index) {
                            return tabs[index];
                          }),
                        )),
                      ),
                    ],
                  ),
                ),
              ),
            ),
            body: TabBarView(
              children: [
                NewRequest(),
                new Column(
                  children: <Widget>[new Text("Accepted Page")],
                ),
                new Column(
                  children: <Widget>[new Text("Cooking Page")],
                ),
                new Column(
                  children: <Widget>[new Text("Cooked Page")],
                )
              ],
            )),
      ),
    );
    }
    

    新建请求页

    class NewRequest extends StatelessWidget{
    
    @override
    Widget build(BuildContext context) {
    
     return ListView(
        shrinkWrap: true,
        children: <Widget>[_CardView(), _CardView(), _CardView(), _CardView()],
      );    
     }  
    }
    
     Widget _CardView() {
      return Card(
        margin: EdgeInsets.all(15.0),
        elevation: 15.0,
        child: Container(
          height: 185,
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Row(
                children: <Widget>[
                  Expanded(
                    child: FittedBox(
                      child: Image.asset('assets/images/momo.jpg'),
                      fit: BoxFit.fill,
                    ),
                  ),
                  Container(
                      child: Expanded(
                          child: Container(
                    margin: EdgeInsets.only(left: 10.0),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                      mainAxisSize: MainAxisSize.max,
                      children: <Widget>[
                        Row(
                          mainAxisAlignment: MainAxisAlignment.start,
                          children: <Widget>[
                            Text("Momo", style: TextStyle(fontSize: 16.0))
                          ],
                        ),
                        Row(
                          mainAxisAlignment: MainAxisAlignment.start,
                          children: <Widget>[
                            Text(
                              "Rs.100",
                              style: TextStyle(fontSize: 16.0),
                            )
                          ],
                        ),
                        Row(
                          mainAxisAlignment: MainAxisAlignment.start,
                          children: <Widget>[
                            Text(
                              "2 Plate",
                              style: TextStyle(fontSize: 16.0),
                            )
                          ],
                        ),
                      ],
                    ),
                  )))
                ],
              ),
              Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  crossAxisAlignment: CrossAxisAlignment.end,
                  children: <Widget>[
                    Expanded(
                        child: Container(
                      margin: EdgeInsets.only(
                          left: 5.0, right: 5.0, top: 5.0, bottom: 2.0),
                      child: RaisedButton(
                        color: Colors.red,
                        child: Text(
                          "Reject",
                          style: TextStyle(color: Colors.white),
                        ),
                        onPressed: () => null,
                      ),
                    )),
                    Expanded(
                        child: Container(
                      margin: EdgeInsets.only(
                          left: 5.0, right: 5.0, top: 5.0, bottom: 0.0),
                      child: RaisedButton(
                        color: Colors.green,
                        child: Text(
                          "Approve",
                          style: TextStyle(color: Colors.white),
                        ),
                        onPressed: () => null,
                      ),
                    )),
                  ])
            ],
          ),
        ));
       }
    
    0 回复  |  直到 6 年前
        1
  •  1
  •   Koya    6 年前

    虽然晚了,但我也遇到了同样的问题。我不知道如何解决你的具体例子,但我张贴我的解决方案,我的问题在这里,希望它可以帮助别人。

    return Row(children: [
        Container(child: Text('•'), margin: EdgeInsets.only(left: 25*indentLevel.toDouble(), right: 5),),
        Expanded(child: TextField(
          cursorColor: Colors.black,
          autofocus: true,
        ))
      ]);
    

    我的问题在于 TextField -失败的断言:…'哈西兹。”
    TextField没有显式的size选项,而是从其父级继承(从这个职位开始 sizing a TextField 如果父级也没有特定的大小(例如行、列、垂直视图),则可能会发生错误。正如你在我上面的代码中看到的, 通过包装 文本字段 在一个扩展的小部件中,它填充其父部件以获得自己的维度,然后将这些维度向下传递给 文本字段

        2
  •  0
  •   Brijesh Kumbhare    5 年前

    抛出错误的小部件可以包装在具有指定高度的容器中。它对我有用。

        3
  •  0
  •   Programmer    5 年前

    @Nbn尝试删除我的主页文件的TabBar小部件中的isScrollable属性。

    推荐文章