gpt-3.5-turbo-0301· OpenAI
WildChat-4.8M (AI2)
flutter how to get logical pixel width without using mediaquery or builder?
- User
flutter how to get logical pixel width without using mediaquery or builder?
- Assistant
You can use the
dart:uilibrary to get the logical pixel width without usingMediaQueryorBuilder.Here's an example code snippet:
import 'package:flutter/material.dart'; import 'dart:ui' as ui; class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { final width = ui.window.physicalSize.width / ui.window.devicePixelRatio; final height = ui.window.physicalSize.height / ui.window.devicePixelRatio; // Calculate the logical pixel size based on the screen density final logicalWidth = width / ui.window.textScaleFactor; final logicalHeight = height / ui.window.textScaleFactor; return MaterialApp( home: Scaffold( appBar: AppBar( title: Text('Logical Pixel Width'), ), body: Container( child: Center( child: Text( 'Logical Pixel Width: $logicalWidth', style: TextStyle(fontSize: 24), ), ), ), ), ); } }Here, we are using
ui.window.physicalSize.widthto get the actual pixel width of the screen, and then dividing it by to get the logical pixel width. We are also taking into consideration the current text scale factor by dividing the logical pixel size by .