首页 文章

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

提问于
浏览
1

我在Flutter中使用SliverAppBar,带有后台小部件 .

事情是当它被扩展时, Headers 和图标(前导和动作)应该是白色的,以便正确看到,当它被折叠时,它们应该变为黑色 .

关于如何从中获取 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),
        ],
      ),
    );
  }
}

2 回答

  • 1

    您可以使用LayoutBuilder获得条子AppBar的最大高度 . 当maximum.height = 80.0时,实际上意味着条子appbar已折叠 .

    这是一个完整的例子:

    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 == 80.0 ? 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

  • 0

    您需要使用ScrollController来实现所需的效果

    试试这段代码

    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
      // This widget is the root of your application.
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(
            primarySwatch: Colors.blue,
          ),
          home: SliverExample(
            bodyWidgets: Text('Hello Body gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg'),
            backgroundWidget: Text('Hello Background'),
          ),
        );
      }
    }
    class SliverExample extends StatefulWidget {
      final Widget backgroundWidget;
      final Widget bodyWidgets;
    
      SliverExample({
        this.backgroundWidget,
        this.bodyWidgets,
      });
      @override
      _SliverExampleState createState() => _SliverExampleState();
    }
    
    class _SliverExampleState extends State<SliverExample> {
    
      // I need something like this
      // To determine if SliverAppBar is expanded or not.
      ScrollController _scrollController;
      bool isAppBarExpanded = false;
    
      @override
      void initState() {
        super.initState();
    
        _scrollController = ScrollController()
          ..addListener(() => setState(() {
            print('Scroll view Listener is called offset ${_scrollController.offset}');
          }));
      }
      bool get _changecolor {
        return _scrollController.hasClients
            && _scrollController.offset > (200-kToolbarHeight);
      }
    
      @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(
            controller: _scrollController,
            slivers: <Widget>[
              SliverAppBar(
    
                pinned: true,
                leading: BackButton(
                  color: _changecolor? Colors.white: Colors.black, // Here
                ),
                actions: <Widget>[
                  IconButton(
                    icon: Icon(
                      Icons.shopping_cart,
                      color: _changecolor? Colors.white: Colors.black, // Here
                    ),
                    onPressed: () {},
                  ),
                ],
                expandedHeight: 200.0,
                flexibleSpace: FlexibleSpaceBar(
                  centerTitle: true,
                  title: Text(
                    'title',
                    style: TextStyle(
                      fontSize: 18.0,
                      color: _changecolor? Colors.white: Colors.black, // Here
                    ),
                  ),
                  // Not affecting the question.
                  background: widget.backgroundWidget,
    
                ),
              ),
              // Not affecting the question.
              SliverToBoxAdapter(child: widget.bodyWidgets),
            ],
          ),
        );
      }
    }
    

相关问题