【问题标题】:Flutter: How to return data without use button?Flutter:如何在不使用按钮的情况下返回数据?
【发布时间】:2020-10-06 01:34:26
【问题描述】:

不点击按钮如何返回数据?

我只想使用takesnapshotGoogleMapController 的图像,而不显示GoogleMap

但在takesnapshot 的示例中,他们使用按钮。

这是一个例子。

const CameraPosition _kInitialPosition =
    CameraPosition(target: LatLng(-33.852, 151.211), zoom: 11.0);

class SnapshotPage extends GoogleMapExampleAppPage {
  SnapshotPage()
      : super(const Icon(Icons.camera_alt), 'Take a snapshot of the map');

  @override
  Widget build(BuildContext context) {
    return _SnapshotBody();
  }
}

class _SnapshotBody extends StatefulWidget {
  @override
  _SnapshotBodyState createState() => _SnapshotBodyState();
}

class _SnapshotBodyState extends State<_SnapshotBody> {
  GoogleMapController _mapController;
  Uint8List _imageBytes;

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.all(16),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: [
          SizedBox(
            height: 180,
            child: GoogleMap(
              onMapCreated: onMapCreated,
              initialCameraPosition: _kInitialPosition,
            ),
          ),
          FlatButton(
            child: Text('Take a snapshot'),
            onPressed: () async {
              final imageBytes = await _mapController?.takeSnapshot();
              setState(() {
                _imageBytes = imageBytes;
              });
            },
          ),
          Container(
            decoration: BoxDecoration(color: Colors.blueGrey[50]),
            height: 180,
            child: _imageBytes != null ? Image.memory(_imageBytes) : null,
          ),
        ],
      ),
    );
  }

  void onMapCreated(GoogleMapController controller) {
    _mapController = controller;
  }
}

这里有什么想法吗?

【问题讨论】:

  • 我想在屏幕上显示图像。
  • 那么如何处理GoogleMap?我不想显示它只想显示takeSnapshot()

标签: google-maps flutter


【解决方案1】:

我认为,您需要在 initState(){} 中尝试此操作,只需创建一个方法,然后调用 initState。例如;

Future<CorrectDataType> _yourMethod() async {
              final imageBytes = await _mapController?.takeSnapshot();
              setState(() {
                _imageBytes = imageBytes;
       });
}
@override
initState(){
  _yourMethod();
  super.initState();
}

想想我的回答,希望你能找到自己的路!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-26
    • 1970-01-01
    • 1970-01-01
    • 2018-12-12
    相关资源
    最近更新 更多