在哪里定义命名参数 'sliver'

Where to define named parameter 'sliver'

因此,我正在根据我发现的 this 示例创建一个基于 sliver 的 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

的确 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']);
            }),
      ));
}

在正文中,您冷设置了一个 SliverList 并将子项计数设置为 0。 类似于:

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

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