Flutter bottomSheet更改主应用程序状态

时间:2020-02-18 13:50:08

标签: android-studio flutter dart flutter-layout bottom-sheet

我的main.dart已经很冗长,因此为了可维护性,我将其拆分为其他.dart个文件。

我的主应用程序使用Google Map对象,并在其上放置了各种红色的位置标记。现在,我在底部有各种FloatingActionButton()-每个人都使用showBottomSheet()showModalBottomSheet()打开底页。

我目前可以想到的将主应用程序拆分为多个文件(保持整洁)的唯一方法是将这些不同底层文件的内容放在不同的.dart文件中,然后从{{1 }}-可能是错误的方式。

enter image description here

Main.dart

main.dart

Settings.dart

...
import 'package:flutter_app/db_manager.dart' as db_manager;

import 'package:flutter_app/section_about.dart';
import 'package:flutter_app/section_settings.dart';

void main() => runApp(MyApp());

SectionAbout sectionAbout = SectionAbout();
SectionSettings sectionSettings = SectionSettings();

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

class _MyAppState extends State<MyApp> {
  GoogleMapController mapController;

  static const LatLng _center = const LatLng(xxxxxxx, xxxxxxx); 

  void _onMapCreated(GoogleMapController controller) {
    mapController = controller;
  }

  void _onCameraMove(CameraPosition position) {
    _lastMapPosition = position.target;
  }

  final Set<Marker> _markers = {};
  MapType _currentMapType = MapType.normal;
  LatLng _lastMapPosition = _center;


  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('My Map'),
          backgroundColor: Colors.green[700],
        ),

        //Put in a stack widget so can layer other widgets on top of map widget
        body: Stack(
          children: <Widget>[
            GoogleMap(
              mapType: _currentMapType,
              markers: _markers,
              onMapCreated: _onMapCreated,
              onCameraMove: _onCameraMove,
              initialCameraPosition: CameraPosition(
                target: _center,
                zoom: 11.0,
              ),
            ),
            Padding(
              padding: const EdgeInsets.all(16.0),
              child: Align(
                  alignment: Alignment.bottomCenter,
                  child: Row(mainAxisSize: MainAxisSize.min, children: <Widget>[

                    SizedBox(width: 16.0),

                    Builder(
                      builder: (context) => FloatingActionButton(
                          child: Icon(Icons.settings, size: 36.0),
                          backgroundColor: Colors.green,
                          onPressed: () {

                            sectionSettings.onSheetShowContents(context);       <------

                          }),
                    ),

                    SizedBox(width: 16.0),
                    FloatingActionButton(
                      onPressed: _onDownloadTestPressed,
                      materialTapTargetSize: MaterialTapTargetSize.padded,
                      backgroundColor: Colors.green,
                      child: const Icon(Icons.autorenew, size: 36.0),
                    ),

                    SizedBox(width: 16.0),

                    Builder(
                      builder: (context) => FloatingActionButton(
                          child: Icon(Icons.help, size: 36.0),
                          backgroundColor: Colors.green,
                          onPressed: () {

                            sectionAbout.onSheetShowContents(context);       <------

                          }),
                    ),

                    SizedBox(width: 16.0),

                    FloatingActionButton(
                      onPressed: _onDBActions,
                      materialTapTargetSize: MaterialTapTargetSize.padded,
                      backgroundColor: Colors.green,
                      child: const Icon(Icons.change_history, size: 36.0),
                    ),
                  ])),
            ),
          ],
        ),
      ),
    );
  }
}




请注意我如何使用import 'package:flutter/material.dart'; import 'package:flutter_app/db_manager.dart' as db_manager; class SectionSettings { int mapTypeView = 0; void onSheetShowContents(Context context) { showModalBottomSheet( //showBottomSheet( context: context, builder: (context) { return ListView( padding: EdgeInsets.all(15.0), children: <Widget>[ ListTile( title: Text("Map Settings"), selected: true, ), Divider(), Row( mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [ Column( children: <Widget>[ Text("Map View"), ], ), Column( children: <Widget>[ Row( children: <Widget>[ ChoiceChip( label: Text("Normal Map"), selected: mapTypeView == 0, onSelected: (value) { setState(() { mapTypeView = 0; _currentMapType = MapType.normal; }); }, ), SizedBox(width: 8), ChoiceChip( label: Text("Satelite Map"), selected: mapTypeView == 1, onSelected: (value) { setState(() { mapTypeView = 1; _currentMapType = MapType.satellite; }); }, ), ], ), ], ) ], ), ], // ), ); }); } } ,并且该代码已移至<OtherDartFileName>.onSheetShowContents();,而不是在主dart文件中占据一大部分。

这带来了一个问题,我无法在此底表中修改Google Map的<OtherDartFileName>,因为它没有主应用程序状态的引用(而且我似乎也无法通过)

  • 我想要一个底表,其中包含一个按钮,可在主地图上切换法线视图和卫星视图(以及其他选项)

  • 我是否完全不正确地构造了该项目,还是可以以某种方式引用Map状态?

对于一个SQFlite实例,我还有单独的State文件,并管理所有数据库操作。用于Android的编码,最终将推向iOS。

非常感谢

0 个答案:

没有答案