我正在学习 Flutter for web,并且遇到了一些小部件无法正确适应 Chrome 浏览器窗口的问题。这会导致“底部溢出”错误,因为不会出现滚动条来容纳额外的内容。下面是我的应用程序的布局结构,其中我主要使用 Column 和 AspectRatio 小部件。如何修改 Flutter 布局以确保所有内容都适合可视区域并在必要时显示滚动条?
lib/basic_screen.dart
import 'package:flutter/material.dart';
import 'package:flutter_layout/immutable_widget.dart'; //ALT+Enter on `Widget`
import 'package:flutter_layout/text_layout.dart';
class BasicScreen extends StatelessWidget {
const BasicScreen({super.key});
@override
Widget build(BuildContext context) {
//Scaffolds are also aware of your device’s specifics.
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.indigo,
title: const Text('Welcome to Flutter'),
actions: const [
Padding(
padding: EdgeInsets.all(10.0),
child: Icon(Icons.edit),
),
],
),
body: Column(
children: const [
AspectRatio(
aspectRatio: 1.0,
child: ImmutableWidget(),
),
TextLayout(),
],
),
drawer: Drawer(
child: Container(
color: Colors.lightBlue,
child: const Center(
child: Text("I'm a Drawer!"),
),
),
),
);
}
}
lib/immutable_widget.dart
import 'package:flutter/material.dart';
import 'dart:math' as Math;
class ImmutableWidget extends StatelessWidget {
const ImmutableWidget({super.key});
@override
Widget build(BuildContext context) {
return Container(
decoration: const BoxDecoration(color: Colors.green),
foregroundDecoration: const BoxDecoration(
backgroundBlendMode: BlendMode.colorBurn,
gradient: LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [
Color(0xAA0d6123),
Color(0x00000000),
Color(0xAA0d6123),
],
),
),
child: Padding(
padding: const EdgeInsets.all(40),
child: Transform.rotate(
angle: 180 / Math.pi, // Rotations are supplied in radians
child: Container(
width: 250,
height: 250,
decoration: BoxDecoration(
color: Colors.purple,
boxShadow: [
BoxShadow(
color: Colors.deepPurple.withAlpha(120),
spreadRadius: 4,
blurRadius: 15,
offset: Offset.fromDirection(1.0, 30)),
],
borderRadius: const BorderRadius.all(Radius.circular(20)),
),
child: Padding(
padding: const EdgeInsets.all(50.0),
child: _buildShinyCircle(),
),
),
),
),
);
}
Widget _buildShinyCircle() {
return Container(
decoration: const BoxDecoration(
shape: BoxShape.circle,
gradient: RadialGradient(
colors: [
Colors.lightBlueAccent,
Colors.blueAccent,
],
center: Alignment(-0.3, -0.5),
),
boxShadow: [
BoxShadow(blurRadius: 20),
],
),
);
}
}
库/main.dart
import 'package:flutter/material.dart';
import 'package:flutter_layout/basic_screen.dart';
import 'package:flutter_layout/immutable_widget.dart';
void main() => runApp(const StaticApp());
class StaticApp extends StatelessWidget {
const StaticApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
//home: const ImmutableWidget(), //Because or using Scaffolds.
home: const BasicScreen(),
);
}
}
lib/text_layout.dart
import 'package:flutter/material.dart';
class TextLayout extends StatelessWidget {
const TextLayout({super.key});
@override
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text(
'Hello, World!',
style: TextStyle(fontSize: 16),
),
Text(
'Text can wrap without issue',
style: Theme.of(context).textTheme.headline6,
),
//make sure the Text below is all in one line:
const Text(
'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam at mauris massa. Suspendisse potenti. Aenean aliquet eu nisl vitae tempus.'),
],
);
}
}
将您的色谱柱包裹在里面
SingleChildScrollView
结果: