gpt4 book ai didi

google-maps - GoogleMap 的 onCameraIdle 在 SingleChildScrollView 中停止触发

转载 作者:行者123 更新时间:2023-12-04 04:39:45 69 4
gpt4 key购买 nike

我有一个设计,其中 SingleChildScrollView 在屏幕的前 50% 上有一个 GoogleMap,在下 50% 中有一个项目列表。用户可以向上滚动整个 View 以查看所有列表。但是,有时如果用户滚动页面, map 会停止触发 onCameraIdle,并且它不会再次开始触发它。

onCameraMove 和 onTap 工作得很好。只是 onCameraIdle 不会触发。

 SingleChildScrollView(
child: Column(
children: <Widget>[
Stack(
children: <Widget>[
Container(
height: screenSize.height / 2,
child: GoogleMap(
key: Key("GMap"),
mapType: MapType.normal,
markers: Set<Marker>.of(markers.values),
gestureRecognizers: Set()
..add(Factory<PanGestureRecognizer>(
() => PanGestureRecognizer()))
..add(
Factory<VerticalDragGestureRecognizer>(
() => VerticalDragGestureRecognizer()),
)
..add(
Factory<HorizontalDragGestureRecognizer>(
() => HorizontalDragGestureRecognizer()),
)
..add(
Factory<ScaleGestureRecognizer>(
() => ScaleGestureRecognizer()),
),
initialCameraPosition: CameraPosition(
target: LatLng(14.551620, 121.053329), zoom: 14.5),
onMapCreated: (GoogleMapController controller) {
if (!_controller.isCompleted) {
_controller.complete(controller);
_lastCameraPosition = CameraPosition(
target: LatLng(14.551620, 121.053329), zoom: 14.5);
}
},
myLocationEnabled: true,
myLocationButtonEnabled: true,
onCameraIdle: () {
print("547: onCameraIdle");
_fetchOffers();
},
onCameraMove: (value) {
print("552: onCameraMove");
_lastCameraPosition = value;
},
onTap: (value) {
// Load items for current view if deselecting a marker
print('556: Tapped outside');
},
),
),
Positioned(
top: 50,
right: 20,
child: Container(
height: 30,
decoration: BoxDecoration(
border: Border.all(
color: _userBalance > 0
? globals.themeColor4
: globals.themeColor2,
width: 2),
boxShadow: [
BoxShadow(
blurRadius: 10.0,
color: Colors.black.withOpacity(.5),
offset: Offset(3.0, 4.0),
),
],
color: Colors.white,
borderRadius: BorderRadius.all(Radius.circular(10.0))),
child: Center(
child: Padding(
padding: EdgeInsets.fromLTRB(10, 5, 10, 5),
child: Text(
"Balance: \u{20B1} ${_userBalance.toStringAsFixed(0)}",
style: TextStyle(
color: Colors.black,
fontSize: 14,
),
),
),
),
),
),
],
),
AnimatedContainer(
color: Colors.white,
// Use the properties stored in the State class.
width: double.infinity,
height: _loaderHeight,
// Define how long the animation should take.
duration: Duration(seconds: 1),
// Provide an optional curve to make the animation feel smoother.
curve: Curves.fastOutSlowIn,
child: Center(
child: Text(
"Loading, please wait",
style: TextStyle(color: Colors.grey),
),
),
),
Container(
color: Colors.white,
child: _offers == null
? Container(
child: Padding(
padding: EdgeInsets.all(30),
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Icon(MdiIcons.foodAppleOutline,
size: 60, color: globals.themeColor4),
Padding(padding: EdgeInsets.only(right: 20)),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text("Fetching offers",
style: TextStyle(
color: globals.themeColor4,
fontWeight: FontWeight.bold)),
Padding(padding: EdgeInsets.only(top: 5)),
Text(
"We are fetching offers for you, please hold on...",
style: TextStyle(color: Colors.grey)),
],
),
),
],
),
),
)
: Column(children: _offers),
),
],
),
),

有没有人以前遇到过这个问题并有解决方案?

最佳答案

我复制了你的示例的一个简单版本,但我无法重现 onCameraIdle 的问题不开火。

现在,根据我的示例,有些行为您可能会误解为不起作用,但实际上是 ScrollView 的行为接管了(因为这些都在 ScrollView 中):

  • 有时 map 上的向下手势会拉动 ScrollView 而不是 map 。
  • 向上的手势会滚动 ScrollView 而不是与 map 交互。

  • 但没有任何进一步的细节到你的代码的其余部分,或者 mcve可以轻松重现您的问题,但很难说到底发生了什么。

    但是,如 Patrick Kelly mentioned ,也有可能是缺少 KeepAlive可能最终导致临时处理您的 map 小部件。这就是为什么建议使用 ListView 的原因,因为它内置了此功能。

    另一方面,您也可以实现 AutomaticKeepAliveClientMixin类似的效果,如在 https://stackoverflow.com/a/51738269/6668797 处看到的(但要注意小部件处置的警告)。

    无论如何,这是我的样本,我必须对您的 _fetchOffers() 做出有根据的猜测。是:
    class _MyHomePageState extends State<MyHomePage> {

    // testing
    int fetchCount = 0;
    List<Widget> _offers;
    _fetchOffers() {
    fetchCount++;
    // simulate varying data
    var rng = new Random();
    int start = rng.nextInt(10);
    int end = start + 3 + rng.nextInt(30);
    // build sample list
    List<Widget> list = new List();
    for (int i = start; i < end; i++) {
    list.add(Text('offer$i', style: new TextStyle(fontSize: 30.0)));
    }

    // assuming you are using setState()
    setState(() {
    _offers = list;
    });
    }

    // from google maps sample
    Completer<GoogleMapController> _controller = Completer();
    static final CameraPosition _kGooglePlex = CameraPosition(
    target: LatLng(37.42796133580664, -122.085749655962),
    zoom: 14.4746,
    );

    @override
    Widget build(BuildContext context) {
    return Scaffold(
    appBar: AppBar(title: Text(widget.title)),
    body: SingleChildScrollView(
    child: Column(
    children: <Widget>[
    Stack(
    children: <Widget>[
    Container(
    height: MediaQuery.of(context).size.height / 2,
    child: GoogleMap(
    mapType: MapType.normal,
    initialCameraPosition: _kGooglePlex,
    gestureRecognizers: Set()
    ..add(Factory<PanGestureRecognizer>(() => PanGestureRecognizer()))
    ..add(Factory<VerticalDragGestureRecognizer>(() => VerticalDragGestureRecognizer()))
    ..add(Factory<HorizontalDragGestureRecognizer>(() => HorizontalDragGestureRecognizer()))
    ..add(Factory<ScaleGestureRecognizer>(() => ScaleGestureRecognizer())),
    onMapCreated: (GoogleMapController controller) {
    _controller.complete(controller);
    },
    onCameraIdle: () {
    _fetchOffers();
    },
    ),
    ),
    ]
    ),
    Container(
    child: _offers == null
    ? Container(child: Text("Fetching offers"))
    : Column(children: _offers)
    ),
    ],
    ),
    ),
    floatingActionButton: FloatingActionButton(
    // for logging _fetchOffers activity
    child: Text(fetchCount.toString())
    ),
    );
    }
    }
    onCameraIdle每次都为我触发,我可以通过不断变化的报价数据和 fetchCount 日志直观地确认它。

    关于google-maps - GoogleMap 的 onCameraIdle 在 SingleChildScrollView 中停止触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57027010/

    69 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com