0

我正在尝试根据 SingleChildScrollView 的滚动位置动态更改容器的宽度。这就是我尝试处理的方式:

...
Container(
     width: 20.0 + position,
     height: deviceHeight*10,
     child: Container(
        decoration: BoxDecoration(
           borderRadius: BorderRadius.circular(25.0),
           color: Color(0xf7f7f7f7).withOpacity(1),
        ),
     ),
   ),

   NotificationListener<ScrollNotification>(
        onNotification: (scrollNotification) {
            if (scrollNotification is ScrollUpdateNotification) {
                if (scrollNotification.metrics.pixels < 300){
                    setState(() {
                       position = scrollNotification.metrics.pixels;
                       //topPosition = deviceHeight*47 - position;
                       print(position);
                    });
                }
            }
            return true;
         },
         child: Positioned(
            top: deviceHeight*47,
            width: deviceWidth*100,
            height: deviceHeight*50,
            child: Container(
               decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(25.0),
                  color: Color(0xf7f7f7f7).withOpacity(1),
            ),

            child: SingleChildScrollView(
               child: Container(
                  padding: EdgeInsets.only(left: deviceWidth*7, top: deviceHeight*4),
                  child: Column(
                     crossAxisAlignment: CrossAxisAlignment.start,
                     children: <Widget>[
                                      ...

我正在打印滚动位置,它是完全正确的,但它对容器的宽度没有影响。

所有这些小部件都在 Stack 小部件下。

我做错了什么?

提前感谢您的支持!

塞尔吉

4

1 回答 1

1

我做了一个测试来模拟你正在测试的东西是这样的,看看它是否有助于解决你的问题。

import 'package:flutter/material.dart';

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,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(),
    );
  }
}

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

class _MyAppState extends State<MyHomePage> {

  double size;
  @override
  void initState() {
    super.initState();

    setState(() {
      size = 100;
    });
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold (
        body: NotificationListener<ScrollNotification>(
          onNotification: (scrollNotification) {
            setState(() {
              if (scrollNotification.metrics.pixels > 300) {
                size = scrollNotification.metrics.pixels;
              }
            });
            return true;
          },
          child:SingleChildScrollView(
            child: Column(
              children: [
                Container(
                  height: 600,
                  color: Colors.redAccent,
                ),
                Container(
                  color: Colors.black38,
                  width: size,
                  height: size,
                ),
                Container(
                  height: 600,
                  color: Colors.blueAccent,
                ),
                Container(
                  height: 600,
                  color: Colors.yellowAccent,
                ),
              ],
            ),
          )
        )
    );
  }
}
于 2020-05-24T04:12:08.740 回答