-1

这是从源代码一步一步复制的代码(Fidev 设计挑战),但有一个错误类怀疑是 MainPage 和 LeopardPage 已导入所有必要的包无法弄清楚缺少什么

class LeopardPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {

    //print(MediaQuery.of(context).size.width);
    return Consumer<PageOffsetNotifier>(

        builder:(context,value,child)
    {
      return Positioned(
        top: 100,
        left: -0.85 * value.offset,
        width: MediaQuery.of(context.watch()).size.width*1.6,
        child: child,
      );
    },child: IgnorePointer(child: Image.asset('assets/leopard.png')),
    );
  }}

4

2 回答 2

0

主类是

class _MainPageState extends State<MainPage> {
  final PageController _pageController = PageController();

  @override
  Widget build(BuildContext context) {

    return ChangeNotifierProvider(

      create: (_) {return PageOffsetNotifier(_pageController); },
      child: Scaffold(
        body: Stack(
          children: <Widget>[

            PageView(
              controller: _pageController,
              physics: ClampingScrollPhysics(),
              children: <Widget>[
                //LeopardPage(),
                VultturePage(),
              ],
            ),
            LeopardPage(),
          ],
        ),
      ),
    );
  }
}
于 2020-10-12T14:38:10.437 回答
0

要强制Consumer使用ChangeNotifier您应该在之前提供它,即

class LeopardPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (_) => PageOffsetNotifier(), // lazy creation of model
      child: Consumer<PageOffsetNotifier>(
        builder:(context, value, child) {
          return Positioned(
            top: 100,
            left: -0.85 * value.offset,
            width: MediaQuery.of(context.watch()).size.width*1.6,
            child: child,
          );
        },
        child: IgnorePointer(
          child: Image.asset('assets/leopard.png')
        ),
      ),
    );
  }
}
于 2021-05-17T07:21:07.087 回答