美文网首页
5-4 Flutter 轮播图Banner功能开发

5-4 Flutter 轮播图Banner功能开发

作者: 小码农小世界 | 来源:发表于2019-11-12 17:35 被阅读0次

    5-4 轮播图Banner功能开发

    import 'package:flutter/material.dart';
    import 'package:flutter_swiper/flutter_swiper.dart';
    
    const APPBAR_SCROLL_OFFSET = 100;
    
    class HomePage extends StatefulWidget {
      @override
      _HomePageState createState() => _HomePageState();
    }
    
    class _HomePageState extends State<HomePage> {
    
      double appBarAlpha = 0;
    
      List _imageUrls = [
        'http://pages.ctrip.com/commerce/promote/20180718/yxzy/img/640sygd.jpg',
        'https://dimg04.c-ctrip.com/images/700u0r000000gxvb93E54_810_235_85.jpg',
        'https://dimg04.c-ctrip.com/images/700c10000000pdili7D8B_780_235_57.jpg',
      ];
    
      _onScroll(offset) {
        double alpha = offset / APPBAR_SCROLL_OFFSET;
        if ( alpha < 0 ) {
          alpha = 0;
        } else if ( alpha > 1 ) {
          alpha = 1;
        }
        setState(() {
          appBarAlpha = alpha;
        });
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Stack(
            children: <Widget>[
              MediaQuery.removePadding(
                removeTop: true,
                context: context,
                child: NotificationListener(
                  onNotification: (scrollNotification) {
                    if ( scrollNotification is ScrollUpdateNotification && scrollNotification.depth == 0 ) {
                      // 滚动且是列表滚动的时候
                      _onScroll(scrollNotification.metrics.pixels);
                    }
                    return;
                  },
                  child: ListView(
                    children: <Widget>[
                      Container(
                        height: 160,
                        child: Swiper(
                          itemCount: _imageUrls.length,
                          autoplay: true,
                          itemBuilder: (BuildContext context, int index) {
                            return Image.network(
                              _imageUrls[index],
                              fit: BoxFit.fill,
                            );
                          },
                          pagination: SwiperPagination(),
                        ),
                      ),
                      Container(
                        height: 800,
                        child: ListTile(title: Text('哈哈')),
                      )
                    ],
                  ),
                ),
              ),
              Opacity(
                opacity: appBarAlpha,
                child: Container(
                  height: 80,
                  decoration: BoxDecoration(color: Colors.white),
                  child: Center(
                    child: Padding(
                      padding: EdgeInsets.only(top: 20),
                      child: Text('首页'),
                    ),
                  ),
                ),
              )
            ],
          ),
        );
      }
    }
    

    运行效果如下

    Simulator Screen Shot - iPhone 11 Pro Max - 2019-11-12 at 17.35.28.png Simulator Screen Shot - iPhone 11 Pro Max - 2019-11-12 at 17.35.34.png

    相关文章

      网友评论

          本文标题:5-4 Flutter 轮播图Banner功能开发

          本文链接:https://www.haomeiwen.com/subject/asmqictx.html