将应用屏幕分成两半 and/or 个分数时出现问题
Having issues dividing the app screen into halves and/or fractions
下面的代码是我的应用程序的众多页面之一。我正在尝试使用 LayoutBuilder 设置此页面,以便它像测验页面一样工作,底部 half/section 将容纳 3 到 4 个 TextButtons,顶部 half/section 将容纳问题并且可能显示图片。
我做了一些研究,发现您可以使用 LayoutBuilder 将容器设置为屏幕的一半。但是,当我尝试使用此代码在此处执行此操作时,抛出了很多异常。所以我想知道是否有更好的方法将屏幕分成两半(或者可能是 1/4、1/4 和 1/2)以获得类似测验的布局。
感谢您的帮助!
quiz.dart
class Quiz extends StatelessWidget {
const Quiz({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Quiz'),
),
body: Column(
children: [
Align(alignment: Alignment.topCenter,
child: LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraints) {
return Container(
height: constraints.maxHeight / 2,
// widthdouble.infinity,
);
},
)),
Align(alignment: Alignment.topCenter,
child: LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraints) {
return Container(
height: constraints.maxHeight / 2,
// widthdouble.infinity,
color: const Color.fromRGBO(155, 205, 255, 0.8),
child: Column(children: [
/* TextButton(onPressed: onPressed, child: child),
TextButton(onPressed: onPressed, child: child),
TextButton(onPressed: onPressed, child: child), */
Text('Option A'),
Text('Option B'),
Text('Option C'),
],),
);
},
)),
],
));
}
}
您可以尝试将 LayoutBuilder
向上移动,以便您可以使用 Column
:
的子项的约束
class Quiz extends StatelessWidget {
const Quiz({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Quiz')),
body: LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraints) {
return Column(
children: [
Container(
height: constraints.maxHeight / 2,
color: Colors.green.shade300,
),
Container(
height: constraints.maxHeight / 2,
color: Colors.blue.shade300,
),
],
);
},
),
);
}
}
借助扩展的 widget.Create2 扩展小部件,您可以轻松完成此操作,您可以根据需要划分屏幕。
enter code here
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 Column(
children: [
Expanded(
flex: 1,
child: Container(
color: Colors.amberAccent,
),
),
Expanded(
flex: 1,
child: Container(
color: Colors.lightBlue,
),
),
],
);
}
}
下面的代码是我的应用程序的众多页面之一。我正在尝试使用 LayoutBuilder 设置此页面,以便它像测验页面一样工作,底部 half/section 将容纳 3 到 4 个 TextButtons,顶部 half/section 将容纳问题并且可能显示图片。 我做了一些研究,发现您可以使用 LayoutBuilder 将容器设置为屏幕的一半。但是,当我尝试使用此代码在此处执行此操作时,抛出了很多异常。所以我想知道是否有更好的方法将屏幕分成两半(或者可能是 1/4、1/4 和 1/2)以获得类似测验的布局。 感谢您的帮助!
quiz.dart
class Quiz extends StatelessWidget {
const Quiz({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Quiz'),
),
body: Column(
children: [
Align(alignment: Alignment.topCenter,
child: LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraints) {
return Container(
height: constraints.maxHeight / 2,
// widthdouble.infinity,
);
},
)),
Align(alignment: Alignment.topCenter,
child: LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraints) {
return Container(
height: constraints.maxHeight / 2,
// widthdouble.infinity,
color: const Color.fromRGBO(155, 205, 255, 0.8),
child: Column(children: [
/* TextButton(onPressed: onPressed, child: child),
TextButton(onPressed: onPressed, child: child),
TextButton(onPressed: onPressed, child: child), */
Text('Option A'),
Text('Option B'),
Text('Option C'),
],),
);
},
)),
],
));
}
}
您可以尝试将 LayoutBuilder
向上移动,以便您可以使用 Column
:
class Quiz extends StatelessWidget {
const Quiz({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Quiz')),
body: LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraints) {
return Column(
children: [
Container(
height: constraints.maxHeight / 2,
color: Colors.green.shade300,
),
Container(
height: constraints.maxHeight / 2,
color: Colors.blue.shade300,
),
],
);
},
),
);
}
}
借助扩展的 widget.Create2 扩展小部件,您可以轻松完成此操作,您可以根据需要划分屏幕。
enter code here
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 Column(
children: [
Expanded(
flex: 1,
child: Container(
color: Colors.amberAccent,
),
),
Expanded(
flex: 1,
child: Container(
color: Colors.lightBlue,
),
),
],
);
}
}