无法在Flutter中使用FAB

时间:2020-04-14 23:01:52

标签: flutter

我收到未定义名为floatingActionButton的错误。

这是我的main.dart代码:

Widget build(BuildContext context) {
  return MaterialApp(
    debugShowCheckedModeBanner: false,
    home: Scaffold(
      appBar: AppBar(
        title: Text('appname'),
      ),
      body: PageView(
        physics: NeverScrollableScrollPhysics(),
        onPageChanged: (index) {
          indexcontroller.add(index);
        },
        controller: pageController,
        children: <Widget>[
          Center(
            child: GoogleMap(
              mapType: MapType.hybrid,
              initialCameraPosition: _kGooglePlex,
              onMapCreated: (GoogleMapController controller) {
                _controller.complete(controller);
              },
              myLocationEnabled: true,
            ),
            floatingActionButton: FloatingActionButton.extended(
              onPressed: _currentLocation,
              label: Text('My Home!'),
              icon: Icon(Icons.location_on),
            ),
          ),
          Center(
            child: Text('tab2'),
          ),
          Center(
            child: Text('tab3'),
          ),
          Center(
            child: Text('tab4'),
          ),
        ],
      ),
    ),
  );
}

2 个答案:

答案 0 :(得分:1)

问题

您正在尝试将FloatingActionButton添加为PageView的参数。但是FAB的直接父级应为Scaffold

解决方案

您应该在floatingActionButton中将FAB添加为Scaffold

return MaterialApp(
  debugShowCheckedModeBanner: false,
  home: Scaffold(
    appBar: AppBar(
      
    ),

    body: PageView(
      
    ),
    
    // HERE ADD FAB
    floatingActionButton: FloatingActionButton.extended(
      onPressed: _currentLocation,
      label: Text('My Home!'),
      icon: Icon(Icons.location_on),
    ),
  ),
);

答案 1 :(得分:0)

尝试将FloatingActionButton放在body的{​​{1}}之外。现在,它直接位于Scaffold内部。