如何找到小部件尺寸以避免窗口小部件在设备区域外渲染时渲染
在这里,我有一个中心偏移值,我需要将小部件定位,我在位置的小部件的帮助下将小部件放置在窗口小部件,并借助分数倾斜的小部件将小部件移至中心,并将其移至中心偏移量。
但是在这里,有些零件小部件在画布外面呈现,所以我想忽略屏幕外渲染的小部件的渲染。如果我得到小部件的大小,我就可以忽略渲染,但是我正在努力获得小部件的高度和宽度。
我已将代码段附加在下面的代码段供参考。
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key}) : super(key: key);
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Stack(children: [
Positioned(
left: 20,
top: 150,
child: FractionalTranslation(translation: const Offset(-0.5, -0.5),child: Container(
color: Colors.grey,
child: const Text('Widget size'),
)))
]),
);
}
}
谢谢。
Here I have a center Offset value that is where I need to position the widget, I have positioned the widget with the help of Positioned widget and moved the widget to the center with the help of FractionalTranslation widget, and moved it to the center offset.
But here some part widgets get rendered outside the canvas, so I want to ignore the rendering of the widget which is rendered outside the screen. If I get the size of the widget, I can be able to ignore the render, but I am struggling with getting widget height and width.
I have attached the code snippet below for reference.
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key}) : super(key: key);
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Stack(children: [
Positioned(
left: 20,
top: 150,
child: FractionalTranslation(translation: const Offset(-0.5, -0.5),child: Container(
color: Colors.grey,
child: const Text('Widget size'),
)))
]),
);
}
}
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
检查此解决方案,有点复杂,但值得一试:
Check this solution, a bit complicated but worth a try:
我已经至少有一个问题,即您将一个容器置于容器的方式。但是,我试图在小部件的尺寸选项上找到一个处理。有02种方法,用扑来。
I already have at least one problem with the way you centre a container. However, I have tried to get a handle on the size option of a widget. There are 02 approaches to this with Flutter.