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

Nbn*_*Nbn 8 flutter flutter-listview

我是颤振新手。我正在使用底部导航视图。在Dashboard类中,有底部导航。底部导航的第一个是具有选项卡视图的Home类。tabview 的第一页是NewRequest类。当应用程序首次运行时,选项卡视图的第一页不会加载,但是当进入其他类并返回 Home 时,会加载新请求。应用程序首次运行时如何加载tabview首页?我已实施如下:

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;
        }
      });
    },
  ) ;
 }
}
Run Code Online (Sandbox Code Playgroud)

我的主页

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")],
            )
          ],
        )),
  ),
);
}
Run Code Online (Sandbox Code Playgroud)

新请求页面

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,
                  ),
                )),
              ])
        ],
      ),
    ));
   }
Run Code Online (Sandbox Code Playgroud)

小智 6

这已经晚了,但我刚刚遇到了同样的问题。我不确定如何解决您的具体示例,但我在这里发布我的问题的解决方案,希望对其他人有帮助。

\n
return Row(children: [\n    Container(child: Text(\'\xe2\x80\xa2\'), margin: EdgeInsets.only(left: 25*indentLevel.toDouble(), right: 5),),\n    Expanded(child: TextField(\n      cursorColor: Colors.black,\n      autofocus: true,\n    ))\n  ]);\n
Run Code Online (Sandbox Code Playgroud)\n

我的问题是TextField- 断言失败:... \'hasSize.\'
\nTextField 没有显式大小选项,而是从其父级继承。(根据这篇关于使用容器调整 TextField 大小的文章假设。)如果父级也没有特定的大小(例如,行、列、ListView 垂直),则可能会发生错误。正如您在上面的代码中看到的,我能够通过将 包装TextField在扩展的 Widget 中来解决此问题,该 Widget 填充其父级以获取自己的尺寸,然后将其传递给TextField.

\n