没有找到任何材质小部件

Posted

技术标签:

【中文标题】没有找到任何材质小部件【英文标题】:no material widget found flutter 【发布时间】:2021-06-18 09:49:52 【问题描述】:

我正在尝试访问此控制器,但我不知道它为什么会崩溃。作为一个错误,他告诉我:

未找到材质小部件 iconButtonwidget 需要一个材质小部件

是一个简单的视图,显示带有各种保存点的地图

我的视图初始化很糟糕吗?

谁能告诉我我哪里错了?

这是我使用的代码

class MapTab extends StatefulWidget 
  @override
  _MapTabState createState() => _MapTabState();


class _MapTabState extends State<MapTab> 
  Size _size;
  MapboxMapController _mapController;

  StreamController<Restaurant> _bottomCardStreamController;

  _onMapCreated(MapboxMapController controller) 
    _mapController = controller;

    contentManager.getMerchants().then(
          (restaurants) => restaurants.forEach(
            (restaurant) => controller.addSymbol(
              SymbolOptions(
                geometry: LatLng(double.parse(restaurant.coordinates.lat),
                    double.parse(restaurant.coordinates.long)),
                iconImage: "assets/images/map_pin.png",
              ),
              restaurant.toJson(),
            ),
          ),
        );

    controller.onSymbolTapped.add((symbol) async 
      Restaurant restaurant = Restaurant.fromJson(symbol.data);
      _toggleBottomCard(restaurant);
    );

    _bottomCardStreamController.stream.listen((restaurant) 
      if (restaurant == null) return;
      List<CameraUpdate> updates = [
        CameraUpdate.zoomTo(15),
        CameraUpdate.newLatLng(LatLng(
          double.parse(restaurant.coordinates.lat),
          double.parse(restaurant.coordinates.long),
        )),
      ];

      updates
          .forEach((element) async => await controller.animateCamera(element));
    );
  

  _toggleBottomCard(Restaurant data) 
    _bottomCardStreamController.sink.add(data);
  

  @override
  void initState() 
    super.initState();
    _bottomCardStreamController = StreamController<Restaurant>.broadcast();
  

  @override
  void dispose() 
    _bottomCardStreamController.close();
    super.dispose();
  

  @override
  Widget build(BuildContext context) 
    _size = MediaQuery.of(context).size;
    return Stack(
      alignment: AlignmentDirectional.center,
      children: [
        FutureBuilder<Position>(
            future: PositionService.getCurrentPosition(),
            builder: (context, AsyncSnapshot<Position> positionSnap) 
              if (positionSnap.hasError)
                return Center(
                  child: Text(localization.err_localization),
                );
              if (positionSnap.connectionState != ConnectionState.done)
                return Container(
                  child: Center(
                    child: CircularProgressIndicator(valueColor: new AlwaysStoppedAnimation<Color>(appColors.yellow)),
                  ),
                );

              var data = positionSnap.data;
              var latLng = LatLng(data.latitude, data.longitude);

              return FutureBuilder<String>(
                  future: contentManager.getMapBoxKey(),
                  builder: (context, tokenSnap) 
                    if (tokenSnap.hasError)
                      return Container(
                        child: Center(
                          child: Text(localization.err_mapbox_key),
                        ),
                      );
                    if (tokenSnap.connectionState != ConnectionState.done)
                      return Container(
                        child: Center(
                          child: CircularProgressIndicator(valueColor: new AlwaysStoppedAnimation<Color>(appColors.yellow)),
                        ),
                      );
                    return Container(
                      height: _size.height,
                      width: _size.width,
                      child: MapboxMap(
                        rotateGesturesEnabled: false,
                        myLocationEnabled: true,
                        onStyleLoadedCallback: () ,
                        zoomGesturesEnabled: true,
                        onMapClick: (_, __) 
                          // fa scomparire la bottomCard
                          _bottomCardStreamController.sink.add(null);
                        ,
                        accessToken: tokenSnap.data,
                        onMapCreated: _onMapCreated,
                        compassEnabled: false,
                        initialCameraPosition: CameraPosition(
                          target: latLng,
                          zoom: 15,
                          bearing: 0,
                          tilt: 0,
                        ),
                      ),
                    );
                  );
            ),
        Positioned(
          bottom: 10,
          right: 10,
          height: 50,
          width: 50,
          child: GestureDetector(
            child: Container(
              decoration: BoxDecoration(
                color: Color.fromARGB(255, 255, 255, 255),
                shape: BoxShape.circle,
              ),
              child: Icon(Icons.my_location),
            ),
            onTap: () async 
              var position = await PositionService.getCurrentPosition();
              var latLng = LatLng(position.latitude, position.longitude);
              _mapController.animateCamera(CameraUpdate.newLatLng(latLng));
            ,
          ),
        ),
        Positioned(
          top: 10,
          left: 0,
          width: _size.width,
          child: Row(
            mainAxisSize: MainAxisSize.min,
            children: [
              IconButton(
                icon: Icon(
                  Icons.list,
                ),
                onPressed: () ,
              ),
              Flexible(
                child: GestureDetector(
                  onTap: () async 
                    await showSearch<Restaurant>(
                      context: context,
                      delegate: CustomSearchDelegate(),
                      query: "",
                    ).then(
                      (value) => _bottomCardStreamController.sink.add(value),
                    );
                  ,
                  child: Container(
                    decoration: BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.circular(20),
                    ),
                    child: Padding(
                      padding: EdgeInsets.symmetric(
                        horizontal: 3,
                        vertical: 2,
                      ),
                      child: TextField(
                        enabled: false,
                        decoration: InputDecoration(
                          icon: Icon(
                            Icons.search,
                            color: Colors.grey,
                          ),
                          hintText: localization.find,
                          border: InputBorder.none,
                        ),
                      ),
                    ),
                  ),
                ),
              ),
              IconButton(
                icon: Icon(
                  Icons.filter_alt,
                ),
                onPressed: () ,
              )
            ],
          ),
        ),
        Positioned(
          bottom: 25,
          width: _size.width,
          child: StreamBuilder<Restaurant>(
            stream: _bottomCardStreamController.stream,
            builder: (_, snap) 
              if (snap.data == null)
                return Container();
              else
                return RestaurantMapCard(snap.data,
                    mapController: _mapController);
            ,
          ),
        ),
      ],
    );
  

【问题讨论】:

【参考方案1】:

MaterialApp 小部件包裹你的根(第一个小部件)。

Widget build(BuidContext context)
  return  MaterialApp(
    home: Stack() // your stack and other widgets are here.
  );

【讨论】:

而不是堆栈?

以上是关于没有找到任何材质小部件的主要内容,如果未能解决你的问题,请参考以下文章

未找到材质小部件 文本字段小部件需要材质小部件祖先

如何从一侧移除高程阴影而不从卡片或材质小部件中移除高程本身?

如何遍历窗口中的所有小部件?

QWebview 未显示在小部件框上

文本小部件始终保持在顶部并且在我滚动时不会消失

是否可以从仪表板小部件中创建新的小部件实例?