gpt4 book ai didi

Flutter 如何检查 Sliver AppBar 是展开还是折叠?

转载 作者:IT老高 更新时间:2023-10-28 12:33:59 41 4
gpt4 key购买 nike

我正在使用 SliverAppBar在 Flutter 中,带有背景小部件。

问题是,当它展开时,标题和图标(引导和操作)应该是白色以便正确看到,当它折叠时,它们应该改为黑色

关于如何从中获得 bool 的任何想法?或其他解决此问题的方法。

谢谢。

class SliverExample extends StatefulWidget {
final Widget backgroundWidget;
final Widget bodyWidgets;

SliverExample({
this.backgroundWidget,
this.body,
});
@override
_SliverExampleState createState() => _SliverExampleState();
}

class _SliverExampleState extends State<SliverExample> {

// I need something like this
// To determine if SliverAppBar is expanded or not.
bool isAppBarExpanded = false;

@override
Widget build(BuildContext context) {

// To change the item's color accordingly
// To be used in multiple places in code
Color itemColor = isAppBarExpanded ? Colors.white : Colors.black;

// In my case PrimaryColor is white,
// and the background widget is dark

return Scaffold(
body: CustomScrollView(
slivers: <Widget>[
SliverAppBar(
pinned: true,
leading: BackButton(
color: itemColor, // Here
),
actions: <Widget>[
IconButton(
icon: Icon(
Icons.shopping_cart,
color: itemColor, // Here
),
onPressed: () {},
),
],
expandedHeight: 200.0,
flexibleSpace: FlexibleSpaceBar(
centerTitle: true,
title: Text(
'title',
style: TextStyle(
fontSize: 18.0,
color: itemColor, // Here
),
),
// Not affecting the question.
background: widget.backgroundWidget,
),
),
// Not affecting the question.
SliverToBoxAdapter(child: widget.body),
],
),
);
}
}

最佳答案

您可以使用 LayoutBuilder 来获取 sliver AppBar 的最大高度。当maximum.height = MediaQuery.of(context).padding.top + kToolbarHeight 时,其实就是sliver appbar被折叠了。

这是一个完整的例子,在这个例子中 MediaQuery.of(context).padding.top + kToolbarHeight = 80.0:

import 'package:flutter/material.dart';

void main() => runApp(MaterialApp(
home: MyApp(),
));

class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
var top = 0.0;

@override
Widget build(BuildContext context) {
return Scaffold(
body: NestedScrollView(
headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
return <Widget>[
SliverAppBar(
expandedHeight: 200.0,
floating: false,
pinned: true,
flexibleSpace: LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraints) {
// print('constraints=' + constraints.toString());
top = constraints.biggest.height;
return FlexibleSpaceBar(
centerTitle: true,
title: AnimatedOpacity(
duration: Duration(milliseconds: 300),
//opacity: top == MediaQuery.of(context).padding.top + kToolbarHeight ? 1.0 : 0.0,
opacity: 1.0,
child: Text(
top.toString(),
style: TextStyle(fontSize: 12.0),
)),
background: Image.network(
"https://images.unsplash.com/photo-1542601098-3adb3baeb1ec?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=5bb9a9747954cdd6eabe54e3688a407e&auto=format&fit=crop&w=500&q=60",
fit: BoxFit.cover,
));
})),
];
},body: ListView.builder(
itemCount: 100,
itemBuilder: (context,index){
return Text("List Item: " + index.toString());
},
),
));
}
}

最终结果:

enter image description here

关于Flutter 如何检查 Sliver AppBar 是展开还是折叠?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53372276/

41 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com