我们可以在Flutter中对SliverAppBar应用圆形边框吗?

bf1o4zei  于 2023-01-27  发布在  Flutter
关注(0)|答案(1)|浏览(180)

[我想实现此用户界面,并在商店应用程序中使用圆形应用程序栏](https://i.stack.imgur.com/YoIIR.jpg
当我使用ClipRRect应用圆形边框到SliverAppBar时,我得到这个错误。但是,我可以将ClipRRect应用到普通的AppBar。我需要一个SliverAppBar来隐藏标题,并且只在向上滚动时显示我的选项卡,以节省屏幕空间。

════════ Exception caught by widgets library ═══════════════════════════════════
The following assertion was thrown building RawGestureDetector-[LabeledGlobalKey<RawGestureDetectorState>#782cb](state: RawGestureDetectorState#5a576(gestures: <none>, behavior: opaque)):
A RenderNestedScrollViewViewport expected a child of type RenderSliver but received a child of type RenderClipRRect.

RenderObjects expect specific types of children because they coordinate with their children during layout and paint. For example, a RenderSliver cannot be the child of a RenderBox because a RenderSliver does not understand the RenderBox layout protocol.
The RenderNestedScrollViewViewport that expected a RenderSliver child was created by: NestedScrollViewViewport ← IgnorePointer-[GlobalKey#845ec] ← Semantics ← Listener ← _GestureSemantics ← RawGestureDetector-[LabeledGlobalKey<RawGestureDetectorState>#782cb] ← Listener ← _ScrollableScope ← _ScrollSemantics-[GlobalKey#9de96] ← NotificationListener<ScrollMetricsNotification> ← Transform ← ClipRect ← ⋯
The RenderClipRRect that did not match the expected child type was created by: ClipRRect ← NestedScrollViewViewport ← IgnorePointer-[GlobalKey#845ec] ← Semantics ← Listener ← _GestureSemantics ← RawGestureDetector-[LabeledGlobalKey<RawGestureDetectorState>#782cb] ← Listener ← _ScrollableScope ← _ScrollSemantics-[GlobalKey#9de96] ← NotificationListener<ScrollMetricsNotification> ← Transform ← ⋯
The relevant error-causing widget was
NestedScrollView
lib\…\screens\news_feed.dart:27
When the exception was thrown, this was the stack

这是我的代码。

class NewsFeed extends StatelessWidget {
  const NewsFeed({Key? key}) : super(key: key);

  static const List<Widget> screens = [
    RetailsScreen(),
    ElonsScreen(),
    RentalsScreen(),
  ];

  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
      length: 3,
      initialIndex: 0,
      child: Scaffold(
        backgroundColor: AppColors.semiWhiteBackground,
        body: NestedScrollView(
          headerSliverBuilder: (context, innerBoxIsScrolled) {
            return [
              ClipRRect(
                borderRadius: const BorderRadius.only(
                  bottomLeft: Radius.circular(25),
                  bottomRight: Radius.circular(25),
                ),
                child: SliverAppBar(
                  title: const LocaleText('newsFeed'),
                  pinned: true,
                  floating: true,
                  forceElevated: innerBoxIsScrolled,
                  bottom: const TabBar(
                    indicatorSize: TabBarIndicatorSize.tab,
                    indicatorWeight: 5,
                    indicatorColor: AppColors.secondary,
                    labelStyle: TextStyle(
                      fontWeight: FontWeight.bold,
                      fontSize: 16,
                    ),
                    unselectedLabelStyle: TextStyle(
                      fontWeight: FontWeight.normal,
                      fontSize: 15,
                    ),
                    tabs: [
                      Tab(
                        child: LocaleText(
                          'buyAndSell',
                          style: TextStyle(color: Colors.white),
                        ),
                      ),
                      Tab(
                        child: LocaleText(
                          'elons',
                          style: TextStyle(color: Colors.white),
                        ),
                      ),
                      Tab(
                        child: LocaleText(
                          'rental',
                          style: TextStyle(color: Colors.white),
                        ),
                      ),
                    ],
                  ),
                ),
              ),
            ];
          },
          body: const TabBarView(
            children: screens,
          ),
        ),
      ),
    );
  }
}

我使用了一个SliverAppBar,以便能够在向上滚动时隐藏appBar的scrollBar部分。

2jcobegt

2jcobegt1#

请使用以下内容更新您的代码:
sliverAppBar中删除ClipRRect并使用sliverAppBar的形状属性

shape: const ContinuousRectangleBorder(
                          borderRadius: BorderRadius.only(
                              bottomLeft: Radius.circular(60),
                              bottomRight: Radius.circular(60))),

完整代码:

NestedScrollView(
            headerSliverBuilder: (context, innerBoxIsScrolled) {
              return [
                SliverAppBar(
                  title: const Text('newsFeed'),
                  pinned: true,
                  floating: true,
                  shape: const ContinuousRectangleBorder(
                      borderRadius: BorderRadius.only(
                          bottomLeft: Radius.circular(60),
                          bottomRight: Radius.circular(60))),
                  forceElevated: innerBoxIsScrolled,
                  bottom: const TabBar(
                    indicatorSize: TabBarIndicatorSize.tab,
                    indicatorWeight: 5,
                    indicatorColor: Colors.yellow,
                    labelStyle: TextStyle(
                      fontWeight: FontWeight.bold,
                      fontSize: 16,
                    ),
                    unselectedLabelStyle: TextStyle(
                      fontWeight: FontWeight.normal,
                      fontSize: 15,
                    ),
                    tabs: [
                      Tab(
                        child: Text(
                          'buyAndSell',
                          style: TextStyle(color: Colors.white),
                        ),
                      ),
                      Tab(
                        child: Text(
                          'elons',
                          style: TextStyle(color: Colors.white),
                        ),
                      ),
                      Tab(
                        child: Text(
                          'rental',
                          style: TextStyle(color: Colors.white),
                        ),
                      ),
                    ],
                  ),
                ),
              ];
            },
            body: Container(),
          ),

相关问题