首页 > 解决方案 > 在 Flutter 的 SliverAppBar 中添加 AppBar

问题描述

我是 Flutter 的新手。我想在 SliverAppBar 内显示搜索框,并在底部的网格视图中显示项目。这样当用户向上滑动时,它只会显示 sliver 应用程序内的搜索框。现在看起来像这样https://ibb.co/MB4Ww6v。我想在 sliver 应用程序底部制作搜索框,如图中的红色所示。

这是我的代码。

  Widget build(BuildContext context) {
    return Scaffold(
      body: CustomScrollView(
        slivers: <Widget>[
          SliverAppBar(
            pinned: true,
            expandedHeight: 200,
            centerTitle: true,
            flexibleSpace: AppBar(
              title: Container(
                height: 45,
                child: TextField(
                  decoration: InputDecoration(
                    contentPadding: EdgeInsets.only(top: 5, left: 15),
                    suffixIcon: IconButton(
                      icon: Icon(Icons.search),
                      onPressed: () {
                        print('sesarch');
                      },
                    ),
                    filled: true,
                    fillColor: Colors.white,
                    hintText: "What are you looking for ?",
                    border: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(5),
                    ),
                  ),
                ),
              ),
              elevation: 20,
            ),
          ),
          SliverGrid(
            gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
              crossAxisCount: 4,
            ),
            delegate: SliverChildBuilderDelegate(
              (BuildContext context, int index) {
                return Container(
                  height: 100,
                  child: Center(
                    child: Text('eee'),
                  ),
                );
              },
            ),
          ),
        ],
      ),
    );
  }

标签: flutterflutter-layoutflutter-sliver

解决方案


您可以在下面复制粘贴运行完整代码
您可以使用bottom属性SliverAppBar

代码片段

SliverAppBar(
                pinned: true,
                expandedHeight: 200,
                centerTitle: true,
                bottom: AppBar(
                  title: Container(

工作演示

在此处输入图像描述

完整代码

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: CustomScrollView(
        slivers: <Widget>[
          SliverAppBar(
            pinned: true,
            expandedHeight: 200,
            centerTitle: true,
            bottom: AppBar(
              title: Container(
                height: 45,
                child: TextField(
                  decoration: InputDecoration(
                    contentPadding: EdgeInsets.only(top: 5, left: 15),
                    suffixIcon: IconButton(
                      icon: Icon(Icons.search),
                      onPressed: () {
                        print('sesarch');
                      },
                    ),
                    filled: true,
                    fillColor: Colors.white,
                    hintText: "What are you looking for ?",
                    border: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(5),
                    ),
                  ),
                ),
              ),
              elevation: 20,
            ),
          ),
          SliverGrid(
            gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
              crossAxisCount: 4,
            ),
            delegate: SliverChildBuilderDelegate(
              (BuildContext context, int index) {
                return Container(
                  height: 100,
                  child: Center(
                    child: Text('eee'),
                  ),
                );
              },
            ),
          ),
        ],
      ),
    );
  }
}

推荐阅读