【问题标题】:coding style question: using event to store data without yielding a state编码风格问题:使用事件存储数据而不产生状态
【发布时间】:2020-12-02 13:30:41
【问题描述】:

到目前为止,我还没有找到在 iOS 设备上获取可用屏幕尺寸的好方法。现在我有了一个非常规的想法,如果即将出现任何问题或有人有更好的想法,我会很感兴趣。

这个想法是通过初始屏幕上 SafeArea 的位置和大小来确定可用的屏幕大小,然后在渲染后将其与事件一起存储到一个块中,但不会触发任何状态更改。然后,该集团会将信息提供给树中的其他小部件。

class _SplashPageState extends State<SplashPage> {
  final safeAreaKey = GlobalKey();
  @override
  void initState(){
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback(_afterLayout);
  }
  _afterLayout(_) {
    BlocProvider.of<AuthenticationBloc>(context)
        .add(AuthenticationScreenSizeDetermined(
          getPosition(safeAreaKey, context, null), 
          getSize(safeAreaKey, context)));
  }

  @override
  Widget build(BuildContext context) {
    return SafeArea(
        key: safeAreaKey,
        child: Text('Init'),
          ),
    ));
  }
}

这是因为到目前为止我还没有找到提供iOS系统栏大小信息的变量(对于Android,它们可用)。

【问题讨论】:

    标签: flutter layout bloc


    【解决方案1】:

    你也试过这个吗:

    内部构建()

    // full screen width and height
    double width = MediaQuery.of(context).size.width;
    double height = MediaQuery.of(context).size.height;
    
    // height without SafeArea
    var padding = MediaQuery.of(context).padding;
    double height1 = height - padding.top - padding.bottom;
    
    // height without status bar
    double height2 = height - padding.top;
    
    // height without status and toolbar
    double height3 = height - padding.top - kToolbarHeight;
    

    编辑

    double bottomPadding = MediaQuery.of(context).viewPadding.bottom;
    

    【讨论】:

    • 是的,不幸的是,kToolbarHeight 仅适用于 Android
    • 试试这个MediaQuery.of(context).viewPadding.top;
    • 如果我在顶部有一个 CupertinoPageScaffold,viewPadding.top 仍然提供 0.0 作为上部位置。而且,正如我所说,kToolBar 也没有为此提供正确的值。通常,在您使用 Positioned() 之前,这不是问题
    猜你喜欢
    • 2021-08-16
    • 2010-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-31
    相关资源
    最近更新 更多