实现PreferredSizeWidget以用作Appbar

时间:2018-04-22 03:57:50

标签: flutter

我正在尝试构建一个可用于识别listview中两列的组件。我希望能够将这个标题栏实现为Scaffold中的appbar,这样我就可以在Scaffold的正文部分实现ListView。我想这样做是因为我希望在我的应用程序中多次使用此模式,并且不希望每次执行粗体标题栏。

因为AppBar组件所需的全部是PreferredSizeWidget的实现,我认为这应该非常简单:

import 'package:flutter/material.dart';

class ListTitleBar extends StatefulWidget implements PreferredSizeWidget {

  final String _left;
  final String _right;

  ListTitleBar(this._left, this._right);

  @override
  State<StatefulWidget> createState() => new ListTitleBarState(_left, _right);

  @override
  Size get preferredSize {
    new Size.fromHeight(20.0);
  }

}

class ListTitleBarState extends State<ListTitleBar> {

  String _leftTitle;
  String _rightTitle;

  ListTitleBarState(this._leftTitle, this._rightTitle);

  @override
  Widget build(BuildContext context) {

return new Container(

  decoration: new BoxDecoration(
    color: Colors.redAccent,
    border: new Border.all(color: Colors.black),
  ),

  child: new Row(
    mainAxisAlignment: MainAxisAlignment.spaceEvenly,
    children: <Widget>[

      ///Left Column Title
      new Column(
        children: <Widget>[
          new Container(
            color: Colors.redAccent,
            padding: const EdgeInsets.all(10.0),
            child: new Text(_leftTitle,
              style: new TextStyle(
                  color: Colors.white,
                  fontSize: 18.0
              ),
            ),
          )
        ],
      ),

      ///Right Column Title
      new Column(
        children: <Widget>[
          new Container(
            color: Colors.redAccent,
            padding: const EdgeInsets.all(10.0),
            child: new Text(_rightTitle,
              style: new TextStyle(
                  color: Colors.white,
                  fontSize: 18.0
              ),
            ),
          )
        ],
      ),

    ],
  ),
);

}

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

 @override
 void dispose() {
   super.dispose();
 }

}

我的问题是成功实施PreferredSizeWidget或实现目标还需要什么。现在我得到以下堆栈跟踪,这似乎表明我忽略了实现的几个方面:

  

= =由WIDGETS LIBRARY引起的异常╞══════════════════════════════════════════════════════════════════════════════════ ═════════════════════   I / flutter(9130):抛出以下NoSuchMethodError构建Scaffold(脏,状态:   I / flutter(9130):ScaffoldState#7c75d(代码:跟踪1股票代码)):   I / flutter(9130):getter'height'在null上调用。   I / flutter(9130):接收者:null   I / flutter(9130):试着打电话:身高   I / flutter(9130):   I / flutter(9130):当抛出异常时,这就是堆栈:   I / flutter(9130):#0 Object.noSuchMethod(dart:core / runtime / libobject_patch.dart:46:5)   I / flutter(9130):#1 ScaffoldState.build(包:flutter / src / material / scaffold.dart:1447:57)   I / flutter(9130):#2 StatefulElement.build(package:flutter / src / widgets / framework.dart:3713:27)   I / flutter(9130):#3 ComponentElement.performRebuild(包:flutter / src / widgets / framework.dart:3625:15)   I / flutter(9130):#4 Element.rebuild(包:flutter / src / widgets / framework.dart:3478:5)   I / flutter(9130):#5 ComponentElement._firstBuild(package:flutter / src / widgets / framework.dart:3605:5)   I / flutter(9130):#6 StatefulElement._firstBuild(package:flutter / src / widgets / framework.dart:3752:11)   I / flutter(9130):#7 ComponentElement.mount(包:flutter / src / widgets / framework.dart:3600:5)   I / flutter(9130):#8 Element.inflateWidget(包:flutter / src / widgets / framework.dart:2890:14)   I / flutter(9130):#9 Element.updateChild(包:flutter / src / widgets / framework.dart:2693:12)   I / flutter(9130):#10 ComponentElement.performRebuild(package:flutter / src / widgets / framework.dart:3636:16)

2 个答案:

答案 0 :(得分:6)

我认为您忘记了return中的preferredSize语句。应该是这样的:

Size get preferredSize { return new Size.fromHeight(20.0); }

答案 1 :(得分:2)

正如他说的那样,@ Giraldi您忘记了return语句,因此您可以这样做,也可以这样做。

class ListTitleBar extends StatefulWidget implements PreferredSizeWidget {

  ListTitleBar({Key key}) : preferredSize = Size.fromHeight(50.0), super(key: key);
  
  @override
  final Size preferredSize;

  @override
  State<StatefulWidget> createState() => new ListTitleBarState();

}