在哪里定义命名参数“ sliver”

时间:2019-03-07 14:49:06

标签: flutter flutter-layout

因此,我基于发现的this示例创建基于银条的AppBar,并且遇到错误named parameter 'slivers' isn't defined

我的代码看起来像这样(我知道我可能把条子放在了错误的位置,但是对理解此问题的任何帮助都值得赞赏)。

Widget build(BuildContext context) {
return Scaffold(
  backgroundColor: Colors.white,
  floatingActionButton: FloatingActionButton(
    backgroundColor: Colors.white,
    onPressed: () {},
    child: Icon(
      CommunityMaterialIcons.plus,
      color: Colors.black,
    ),
  ),
  slivers: <Widget>[ // *problematic sliver here*
    SliverAppBar(
      pinned: true,
      expandedHeight: 256.0,
      flexibleSpace: FlexibleSpaceBar(
        title: Text('po.'),
      ),
    ),
  ],
  body: ListView.builder(
  itemCount: tracks.length,
  itemBuilder: (BuildContext context, int index) { // the rest of the itembuilder continued here

2 个答案:

答案 0 :(得分:3)

实际上没有为slivers定义Scaffold字段。将您的应用栏和列表视图包装在NestedScrollView中,如下所示:

Widget build(BuildContext context) => Scaffold(
        backgroundColor: Colors.white,
        floatingActionButton: FloatingActionButton(
          backgroundColor: Colors.white,
          onPressed: () {},
          child: Icon(
            Icons.plus_one,
            color: Colors.black,
          ),
        ),
        body: NestedScrollView(
            headerSliverBuilder:
                (BuildContext context, bool innerBoxIsScrolled) {
              return <Widget>[
                SliverAppBar(
                  pinned: true,
                  expandedHeight: 256.0,
                  flexibleSpace: FlexibleSpaceBar(
                    title: Text('po.'),
                  ),
                ),
              ];
            },
            body: ListView.builder(
              padding: EdgeInsets.all(8.0),
              itemExtent: 20.0,
              itemBuilder: (BuildContext context, int index) {
                return Text('entry $index');
              },
            )));

这是您提供的固定依据:

import 'package:flutter/material.dart';

void main() {
  runApp(MaterialApp(
    theme: ThemeData.dark(),
    debugShowCheckedModeBanner: false,
    home: Home(),
  ));
}

class Home extends StatelessWidget {
  final tracks = const [
    {'title': 'BCS Examination', 'subtitle': 'Something'},
    {'title': 'Dhaka University Admission'},
    {'title': 'Khulna University Admission'},
    {'title': 'Chottogram University Admission'},
    {'title': 'Bank Job Exam'},
    {'title': 'Bank Job Exam'}
  ];

  @override
  Widget build(BuildContext context) => Scaffold(
      floatingActionButton: FloatingActionButton(
        backgroundColor: Colors.white,
        onPressed: () {},
        child: Icon(
          Icons.info,
          color: Colors.black,
        ),
      ),
      body: NestedScrollView(
        headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
          return <Widget>[
            SliverAppBar(
              pinned: true,
              expandedHeight: 256.0,
              flexibleSpace: FlexibleSpaceBar(
                title: Text('po.'),
              ),
            ),
          ];
        },
        body: ListView.builder(
            itemCount: tracks.length,
            itemBuilder: (BuildContext context, int index) {
              var trackTitles = tracks[index];
              return Text(trackTitles['title']);
            }),
      ));
}

答案 1 :(得分:0)

您在体内冷设置一个SliverList并将子计数设置为0。 像这样:

SliverList(
  delegate: new SliverChildListDelegate(<Widget>[]),
)

通常我将它与CustomScrollView一起使用,但是您可以尝试。