如何获得screensize来设置小部件的大小在Flutter?

g6baxovj  于 2023-03-31  发布在  Flutter
关注(0)|答案(1)|浏览(145)

我有这样的代码,它会得到屏幕宽度来计算和设置标签自适应宽度accordingto screensize.它在调试模式下工作正常,但在释放模式下,标签总是发现可以滚动与大amoung的宽度. isScrollable属性的Tabbar被设置为true提供所需的宽度标签.

final double screenWidth = Data.screen.width;

@override
  void initState() {
    super.initState();
    _tabController = TabController(vsync: this, length: 4);
    _tabController.addListener(_handleTabSelection);
    tabWidth = screenWidth / 4.8;
  }

@override
  Widget build(BuildContext context) {
    List<Widget> myTabs = [
      const HomePageTab(
        icon: Icons.groups_rounded,
      ),
      SizedBox(
        width: tabWidth,
        child: const HomePageTab(
          title: 'Chats',
        ),
      ),
      SizedBox(
        width: tabWidth,
        child: const HomePageTab(
          title: 'Status',
        ),
      ),
      SizedBox(
        width: tabWidth,
        child: const HomePageTab(
          title: 'Calls',
        ),
      ),
    ];

.

In release mode

In Debug mode

rm5edbpk

rm5edbpk1#

使用var screenSize = MediaQuery.of(context).size

相关问题