【问题标题】:Flutter: How to make a Card overlap the AppBar?Flutter:如何让卡片与 AppBar 重叠?
【发布时间】:2018-03-24 18:46:22
【问题描述】:

如何在 Flutter 中创建一个与 AppBar 重叠的 Card?据我所知,负边距是不可能的。

看清楚图片。

【问题讨论】:

  • @Idol 没那么容易。通常在这种情况下,您也希望应用栏滚动。
  • @hendra 我认为目前这不可能开箱即用。您应该使用 CustomScrollView 和 FlexibleSpace appbar 部分实现这一点。

标签: layout flutter floating


【解决方案1】:

对于一张卡,Stack widget 可以轻松完成

例如

import 'package:flutter/material.dart';

class Home extends StatefulWidget {
  Home({Key key}) : super(key: key);

  @override
  HomeState createState() {
    return new HomeState();
  }
}

class HomeState extends State<Home> {
  bool _hasCard;

  @override
  void initState() {
    super.initState();
    _hasCard = false;
  }

  @override
  Widget build(BuildContext context) {
    List<Widget> children = new List();

    children.add(_buildBackground());
    if (_hasCard) children.add(_buildCard());

    return MaterialApp(
      home: Stack(
        children: children,
      ),
    );
  }

  void _showCard() {
    setState(() => _hasCard = true);
  }

  void _hideCard() {
    setState(() => _hasCard = false);
  }

  Widget _buildCard() => new Container(
        child: new Center(
          child: new Container(
            height: 700.0,
            width: 200.0,
            color: Colors.lightBlue,
            child: new Center(
              child: new Text("Card"),
            ),
          ),
        ),
      );

  Widget _buildBackground() => new Scaffold(
        appBar: new AppBar(
          title: new Text("AppBar"),
        ),
        body: new Container(
          child: _hasCard
              ? new FlatButton(
                  onPressed: _hideCard, child: new Text("Hide card"))
              : new FlatButton(
                  onPressed: _showCard, child: new Text("Show card")),
        ),
      );
}

void main() {
  runApp(
    new Home(),
  );
}

如果有很多卡片,可以将它们包装成ListView

【讨论】:

    【解决方案2】:
    class Sample2 extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return SafeArea(
          child: Material(
            child: CustomScrollView(
              slivers: [
                SliverPersistentHeader(
                  delegate: MySliverAppBar(expandedHeight: 200),
                  pinned: true,
                ),
                SliverList(
                  delegate: SliverChildBuilderDelegate(
                    (_, index) => ListTile(
                          title: Text("Index: $index"),
                        ),
                  ),
                )
              ],
            ),
          ),
        );
      }
    }
    
    class MySliverAppBar extends SliverPersistentHeaderDelegate {
      final double expandedHeight;
    
      MySliverAppBar({@required this.expandedHeight});
    
      @override
      Widget build(
          BuildContext context, double shrinkOffset, bool overlapsContent) {
        return Stack(
          fit: StackFit.expand,
          overflow: Overflow.visible,
          children: [
            Image.network(
              "https://images.pexels.com/photos/396547/pexels-photo-396547.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500",
              fit: BoxFit.cover,
            ),
            Center(
              child: Opacity(
                opacity: shrinkOffset / expandedHeight,
                child: Text(
                  "MySliverAppBar",
                  style: TextStyle(
                    color: Colors.white,
                    fontWeight: FontWeight.w700,
                    fontSize: 23,
                  ),
                ),
              ),
            ),
            Positioned(
              top: expandedHeight / 2 - shrinkOffset,
              left: MediaQuery.of(context).size.width / 4,
              child: Opacity(
                opacity: (1 - shrinkOffset / expandedHeight),
                child: Card(
                  elevation: 10,
                  child: SizedBox(
                    height: expandedHeight,
                    width: MediaQuery.of(context).size.width / 2,
                    child: FlutterLogo(),
                  ),
                ),
              ),
            ),
          ],
        );
      }
    
      @override
      double get maxExtent => expandedHeight;
    
      @override
      double get minExtent => kToolbarHeight;
    
      @override
      bool shouldRebuild(SliverPersistentHeaderDelegate oldDelegate) => true;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-02
      • 2020-03-02
      • 2020-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多