setState的原理

来源:8-2 APP首页框架搭建-Scaffold与PageView【搭了个框架】

THEONEjyf

2020-03-26

跟着敲 没效果 不知道为什么

import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter_swiper/flutter_swiper.dart';
class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}
const APPBAR_SCROLL_OFFSET = 100;

class _HomePageState extends State<HomePage> {
  @override
  Widget build(BuildContext context){
    double appBarAlpha = 0;
    _onScroll(offset){
      double alpha = offset/APPBAR_SCROLL_OFFSET;
      if(alpha<0){
        alpha = 0;
      } else if (alpha>1){
        alpha = 1;
      }
      setState(() {
        appBarAlpha = alpha;
      });
      print(appBarAlpha);
    }
    return new Scaffold(
        body: Stack(
          children: <Widget>[
            MediaQuery.removePadding(
                removeTop: true,
                context: context,
                child: NotificationListener(
                    onNotification: (ScrollNotification notification){
                      if(notification is ScrollUpdateNotification && notification.depth == 0){
                        ScrollMetrics metrics = notification.metrics;
                        _onScroll(metrics.pixels);
                      }
                    },
                    child: ListView(
                        children: <Widget>[
                          Container(
                              height: 160,
                              color: Colors.blue,
                              child: Center(
                                  child: Text(
                                    'banner',
                                    style: TextStyle(
                                      color: Colors.black,
                                      fontSize: 34,
                                    ),
                                  )
                              )
                          ),
                          Container(
                            height: 1100,
                            child: ListTile(
                              title: Text('sdfsda'),
                            ),
                          )
                        ]
                    )

                )
            ),
            Opacity(
              opacity: appBarAlpha,
              child: Container(
                height: 80,
//                decoration: BoxDecoration(
//                  color: Color.fromARGB((appBarAlpha * 255).toInt(), 255, 255, 255),
//                ),
                child: Center(
                  child: Padding(
                    padding: EdgeInsets.only(top: 20),
                    child: Text(
                      appBarAlpha.toString(), // 这个值没变   是为什么?
                    ),
                  ),
                ),
              ),
            )
          ],
        )
    );
  }
}

写回答

1回答

CrazyCodeBoy

2020-03-26

代码设置的问题,对照下这块的课程源码检查下你的代码看能否有出入的地方呢https://git.imooc.com/coding-321/flutter_trip/src/67d80ded38512543c663fdf4b932294062f053cd/lib/pages/home_page.dart


0
0

Flutter从入门到进阶 实战携程网App 一网打尽核心技术

解锁Flutter开发新姿势,,系统掌握Flutter开发核心技术。

4788 学习 · 3274 问题

查看课程