在AppBar中使用构建功能

时间:2020-04-24 23:11:02

标签: flutter dart

我正在尝试使用搜索栏构建功能而不是标题。 作为appBar主体的一部分,它工作得很好。 这是我要适合AppBar的buildSearchBar的实现:

buildSearchBar(BuildContext context) {
  return SliverPadding(
    padding: EdgeInsets.only(left: 16.0, right: 16.0, top: 16.0),
    sliver: SliverList(
      delegate: SliverChildListDelegate([
        Card(
            color: Colors.transparent,
            elevation: 8,
            child: ClipRRect(
                borderRadius: BorderRadius.all(Radius.circular(16.0)),
                child: Container(
                  padding: EdgeInsets.symmetric(horizontal: 16.0),
                  color: Theme.of(context).accentColor,
                  child: TextField(
                    decoration: InputDecoration(
                        icon: Icon(Icons.search),
                        border: InputBorder.none,
                        hintStyle:
                            TextStyle(fontSize: 18, color: Colors.black54),
                        hintText: 'Search news'),
                    textInputAction: TextInputAction.search,
                    cursorColor: Colors.black54,
                    style: TextStyle(fontSize: 18, color: Colors.black54),
                    controller: TextEditingController(),
                    onSubmitted: (text) => searchLogic(context, text),
                  ),
                )))
      ]),
    ),
  );
}

searchbar in AppBar body

这就是我创建AppBar的方式

    Widget build(BuildContext context) {

    return Scaffold(
          appBar: AppBar(
          title: buildSearchBar(context),

          backgroundColor: Theme.of(context).brightness == Brightness.dark
                    ? Colors.grey[850]
                          : Theme.of(context).accentColor,



),
      body: CustomScrollView(

        controller: scrollControllerSearchPage,

        slivers: <Widget>[

          SliverToBoxAdapter(


          ),
          buildSearchBar(context),
        ],
      ),
    );
  }

我试图创建一列并将其放入SizedBox中,但遇到了以下异常:

flutter: Another exception was thrown: A _RenderAppBarTitleBox expected a child of type RenderBox but received a child of type RenderSliverPadding.
flutter: Another exception was thrown: NoSuchMethodError: The method 'layout' was called on null.
flutter: Another exception was thrown: RenderBox was not laid out: _RenderAppBarTitleBox#9675d relayoutBoundary=up16 NEEDS-PAINT
flutter: Another exception was thrown: RenderBox was not laid out: RenderSemanticsAnnotations#51f2d relayoutBoundary=up15 NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE
flutter: Another exception was thrown: Updated layout information required for RenderErrorBox#742c7 NEEDS-LAYOUT NEEDS-PAINT to calculate semantics.

0 个答案:

没有答案