无法在Flutter中正确隐藏滚动时的应用栏标题

huangapple go评论70阅读模式
英文:

Cannot properly hide the appbar title on scroll in flutter

问题

我想在滚动时隐藏AppBar。搜索图标已正确隐藏,滚动时不透明度也减小。但是对于标题,它不起作用。

如下所示,当我向下滚动时,搜索按钮的不透明度逐渐降低,但标题没有。

我尝试使用preferred height、动画控制器,但情况变得更糟。

import 'package:flutter/material.dart';
import 'package:vet_mobile/screens/chat.dart';
import 'package:vet_mobile/screens/logot.dart';

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

  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
      length: 3,
      child: Scaffold(
        body: NestedScrollView(
          headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
            return <Widget>[
              SliverAppBar(
                title: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text(
                      'WhatsApp',
                      style: TextStyle(
                        color: Theme.of(context).textTheme.bodyLarge!.color,
                      ),
                    ),
                    IconButton(
                      onPressed: () {},
                      icon: Icon(
                        Icons.search,
                        color: Theme.of(context).textTheme.bodyLarge!.color,
                      ),
                    ),
                  ],
                ),
                pinned: true,
                floating: true,
                elevation: 5,
                bottom: TabBar(
                  indicatorSize: TabBarIndicatorSize.tab,
                  indicatorWeight: 4,
                  indicatorColor: Theme.of(context).textTheme.bodyLarge!.color,
                  labelStyle:
                      TextStyle(fontSize: 13, fontWeight: FontWeight.w600),
                  labelColor: Theme.of(context).textTheme.bodyLarge!.color,
                  unselectedLabelColor:
                      Theme.of(context).textTheme.bodySmall!.color,
                  dividerColor: Colors.transparent,
                  tabs: const [
                    Tab(text: 'CHATS'),
                    Tab(text: 'STATUS'),
                    Tab(text: 'CALLS'),
                  ],
                ),
              ),
            ];
          },
          body: const TabBarView(
            children: [
              Center(child: LogoutScreen()),
              Center(child: ChatScreen()),
              Center(child: Text('Patient')),
            ],
          ),
        ),
      ),
    );
  }
}

无法在Flutter中正确隐藏滚动时的应用栏标题
如您所见,搜索按钮的不透明度在滚动时逐渐减小,但标题不会。

我尝试使用preferred height、动画控制器,但情况变得更糟。

英文:

I want to hide the AppBar on scroll. The search icon is hidden properly and also the opacity decreases on scroll. But for the title, it is not working.

import &#39;package:flutter/material.dart&#39;;
import &#39;package:vet_mobile/screens/chat.dart&#39;;
import &#39;package:vet_mobile/screens/logot.dart&#39;;

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

  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
      length: 3,
      child: Scaffold(
        body: NestedScrollView(
          headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
            return &lt;Widget&gt;[
              SliverAppBar(
                title: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text(
                      &#39;WhatsApp&#39;,
                      style: TextStyle(
                        color: Theme.of(context).textTheme.bodyLarge!.color,
                      ),
                    ),
                    IconButton(
                      onPressed: () {},
                      icon: Icon(
                        Icons.search,
                        color: Theme.of(context).textTheme.bodyLarge!.color,
                      ),
                    ),
                  ],
                ),
                pinned: true,
                floating: true,
                elevation: 5,
                bottom: TabBar(
                  indicatorSize: TabBarIndicatorSize.tab,
                  indicatorWeight: 4,
                  indicatorColor: Theme.of(context).textTheme.bodyLarge!.color,
                  labelStyle:
                      TextStyle(fontSize: 13, fontWeight: FontWeight.w600),
                  labelColor: Theme.of(context).textTheme.bodyLarge!.color,
                  unselectedLabelColor:
                      Theme.of(context).textTheme.bodySmall!.color,
                  dividerColor: Colors.transparent,
                  tabs: const [
                    Tab(text: &#39;CHATS&#39;),
                    Tab(text: &#39;STATUS&#39;),
                    Tab(text: &#39;CALLS&#39;),
                  ],
                ),
              ),
            ];
          },
          body: const TabBarView(
            children: [
              Center(child: LogoutScreen()),
              Center(child: ChatScreen()),
              Center(child: Text(&#39;Patient&#39;)),
            ],
          ),
        ),
      ),
    );
  }
}

无法在Flutter中正确隐藏滚动时的应用栏标题
As we can see the opacity of the search button decreases slowly as I scroll down but not for the title.

I tried using the preferred height, animation controller, but it messed up more.

答案1

得分: 1

似乎在设置自定义样式时,此效果不起作用。 从这里删除固定样式设置:

```dart
Text(
  'PawCare',
  // remove this
  /*style: TextStyle(
    color: Theme.of(context).textTheme.bodyLarge!.color,
  ),*/
),

要设置标题文本的样式,请使用SliverAppBartitleTextStyle配置:

SliverAppBar(
  titleTextStyle: TextStyle(
   color: Theme.of(context).textTheme.bodyLarge!.color),
...

<details>
<summary>英文:</summary>

Seems that this effect does not work when you set a custom style. Remove the fixed style setting from here:

Text(
'PawCare',
// remove this
/style: TextStyle(
color: Theme.of(context).textTheme.bodyLarge!.color,
),
/
),


To set the style of the title text, use the `titleTextStyle` configuration of `SliverAppBar`:

SliverAppBar(
titleTextStyle: TextStyle(
color: Theme.of(context).textTheme.bodyLarge!.color),
...



</details>



huangapple
  • 本文由 发表于 2023年6月1日 12:20:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76378633.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定