微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

IllegalFloatingActionButtonSizeException浮动操作按钮必须为圆形

如何解决IllegalFloatingActionButtonSizeException浮动操作按钮必须为圆形

我遇到以下错误

IllegalFloatingActionButtonSizeException:浮动操作按钮必须 是一个圆形文件“ circular_notch_and_corner_clipper.dart”,第25行, 在CircularNotchedAndCorneredRectangleClipper.getClip文件中 _RenderCustomClip._updateClip文件中的“ proxy_Box.dart”行1314 RenderPhysicalShape.paint文件中的“ proxy_Box.dart”行1935年 RenderObject._paintWithContext文件中的“ object.dart”行2311 PaintingContext.paintChild文件中的“ object.dart”行189 RenderBoxContainerDefaultsMixin.defaultPaint中的“ Box.dart”行2826 在第407行中输入文件“ custom_layout.dart” RenderCustomMultiChildLayoutBox.paint文件“ object.dart”,行2311, 在RenderObject._paintWithContext文件“ object.dart”的第189行中 PaintingContext.paintChild文件“ proxy_Box.dart”,第131行,在 RenderProxyBoxMixin.paint文件“ material.dart”,第555行,在 _RenderInkFeatures.paint文件“ object.dart”,行2311,在RenderObject._paintWithContext文件“ object.dart”,行189,在 PaintingContext.paintChild文件“ proxy_Box.dart”,第131行,在 RenderProxyBoxMixin.paint文件“ object.dart”,行396,在 PaintingContext.pushLayer文件“ proxy_Box.dart”,行1862,在 RenderPhysicalModel.paint文件“ object.dart”,行2311,在 RenderObject._paintWithContext文件“ object.dart”,第189行,在 PaintingContext.paintChild文件“ proxy_Box.dart”,第131行,在 RenderProxyBoxMixin.paint文件“ object.dart”,行2311,在 RenderObject._paintWithContext文件“ object.dart”,第189行,在 PaintingContext.paintChild文件“ proxy_Box.dart”,第131行,在 RenderProxyBoxMixin.paint文件“ object.dart”,行2311,在 RenderObject._paintWithContext文件“ object.dart”,第189行,在 PaintingContext.paintChild文件“ proxy_Box.dart”,第131行,在 RenderProxyBoxMixin.paint文件“ object.dart”,行2311,在 RenderObject._paintWithContext文件“ object.dart”,第140行,在 PaintingContext._repaintCompositedChild文件“ object.dart”,行 100,在PaintingContext.repaintCompositedChild文件“ object.dart”中, PipelineOwner.flushPaint文件“ binding.dart”的第978行,第438行, 在RendererBinding.drawFrame文件“ binding.dart”的第914行中 WidgetsBinding.drawFrame文件“ binding.dart”,第302行,在 RendererBinding._handlePersistentFrameCallback文件“ binding.dart”, SchedulerBinding._invokeFrameCallback文件中的第1117行 SchedulerBinding.handleDrawFrame文件中的“ binding.dart”行1055 SchedulerBinding._handleDrawFrame文件中的“ binding.dart”行971 _rootRun文件“ zone.dart”,第1093行,在“ zone.dart”,行1190,在 _CustomZone.run中的_CustomZone.run文件“ zone.dart”第997行,_drawFrame中的_invoke文件“ hooks.dart”的第251行_drawFrame

我想可能与设备类型有关,但我不确定。这是我看到错误的设备之一:

品牌:“三星”,设备:“ crownlte”,型号:“ SM-N960F”

相关代码

import 'package:animated_bottom_navigation_bar/animated_bottom_navigation_bar.dart';

...

floatingActionButtonLocation:
              FloatingActionButtonLocation.centerDocked,bottomNavigationBar: AnimatedBottomNavigationBar(
            icons: allDestinations()
                .map((NavbarOption destination) => destination.icon)
                .toList(),activeColor: Colors.white,splashColor: Theme.of(context).primaryColor,activeIndex: _currentIndex,gapLocation: GapLocation.center,inactiveColor: Colors.white60,notchAndCornersAnimation: animation,backgroundColor: Theme.of(context).primaryColor,notchSmoothness: NotchSmoothness.softEdge,onTap: (index) => setState(() => _currentIndex = index),),

解决方法

可能是以下原因导致错误,因为您提到它不会每次都发生

也许,从 0 (begin:0) 开始的缩放转换导致了错误,因为当它缩放到 0 时它不再是一个圆。因此,将开始更改为大约的值。 0 可能会解决错误,即 begin:0.00001

 WidgetsBinding.instance.addPostFrameCallback(
      (_) {
        _handleInternetConnection();

        Future.delayed(
          Duration(seconds: 1),() {
            if (mounted) _animationController.forward();
          },);
      },);

解决方案:

  1. 设置开始begin:0.00001

  2. 此外,如果您希望小部件在构建后立即进行动画处理,最好使用隐式动画。我修改了代码以包含隐式动画而不是显式动画(其中必须显式调用 forward() 才能设置动画)。

试试这个例子(使用空安全)

import 'package:flutter/material.dart';

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

/// This is the main application widget.
class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  static const String _title = 'Flutter Code Sample';

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      title: _title,home: MyStatefulWidget(),);
  }
}

/// This is the stateful widget that the main application instantiates.
class MyStatefulWidget extends StatefulWidget {
  const MyStatefulWidget({Key? key}) : super(key: key);

  @override
  State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
}

/// This is the private State class that goes with MyStatefulWidget.
class _MyStatefulWidgetState extends State<MyStatefulWidget> {
  int _selectedIndex = 0;
  static const TextStyle optionStyle =
      TextStyle(fontSize: 30,fontWeight: FontWeight.bold);
  static const List<Widget> _widgetOptions = <Widget>[
    Text(
      'Index 0: Home',style: optionStyle,),Text(
      'Index 1: Business',Text(
      'Index 2: School',];

  void _onItemTapped(int index) {
    setState(() {
      _selectedIndex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('BottomNavigationBar Sample'),body: Center(
        child: _widgetOptions.elementAt(_selectedIndex),floatingActionButton: _selectedIndex == 0
          ? TweenAnimationBuilder<double>(
              tween: Tween<double>(
                begin: 0.00001,end: 1,curve: Interval(
                0.5,1.0,curve: Curves.fastOutSlowIn,duration: Duration(milliseconds: 700),builder: (_,double scaleValue,__) {
                return Transform.scale(
                    scale: scaleValue,child: FloatingActionButton(
                      elevation: 8,foregroundColor: Colors.white,child: Icon(
                        Icons.face_retouching_natural,size: 36,onPressed: () {
                        print("hi");
                      },));
              })
          : null,bottomNavigationBar: BottomNavigationBar(
        items: const <BottomNavigationBarItem>[
          BottomNavigationBarItem(
            icon: Icon(Icons.home),label: 'Home',BottomNavigationBarItem(
            icon: Icon(Icons.business),label: 'Business',BottomNavigationBarItem(
            icon: Icon(Icons.school),label: 'School',],currentIndex: _selectedIndex,selectedItemColor: Colors.amber[800],onTap: _onItemTapped,);
  }
}
,

首先,尝试使用最新版本的库和 flutter 运行它。如果它仍然存在,请告诉我。我无法重现它,所以如果你能这样做,请提供一个最小的可重现样本。

其次,关于“我猜可能与设备类型有关,但我不确定。”,可能与设备无关,因为Flutter是跨平台的,而您的代码似乎不相关到本机代码。

第三,尝试使用“二进制搜索调试”技术。例如,尝试从 AnimatedBottomNavigationBar() 中删除所有参数并查看它是否有效。如果是这样,请添加一些参数并重试。如果再次起作用,请添加更多参数;否则删除一些。通过这样做,您将看到哪个参数导致了问题。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。