flutter 将ColumnWidget指定为AlignWidget的子属性时,对齐的[Y]不起作用

clj7thdc  于 2023-10-22  发布在  Flutter
关注(0)|答案(2)|浏览(104)

将ColumnWidget指定为AlignWidget的子属性时,对齐的[Y]不起作用。当指定ColumnWidget的mainAxisAlignment时,您可以将[Y]的对齐设置为中心、顶部或底部,但不能使用数字(例如,对齐(0.0,0.3))来指定它,这限制了进行细粒度调整的能力。
Dart Pad
示例代码

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(home: MyHomePage());
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Align(
          alignment: const Alignment(0.0, 0.0), // [Y] Not Working
          child: Column(
            children: [
              Container(
                height: 50,
                width: 50,
                color: Colors.amber,
              ),
            ],
          )),
    );
  }
}

如何在ColumnWidget中指定细粒度的对齐方式,如对齐(0.0,0.3)?或者这是Flutter的局限性?

acruukt9

acruukt91#

列上的默认mainAxisSize:MainAxisSize.max。这意味着它将采取尽可能多的高度(约束)。可以设置mainAxisSize:MainAxisSize.min
尺寸增大

return Scaffold(
      body: Align(
          alignment: const Alignment(0.0, 0.0), 
          child: Column(
            mainAxisSize:MainAxisSize.min, //this one
            children: [
              Container(
                height: 50,
                width: 50,
                color: Colors.amber,
              ),
            ],
          )),
    );

更多关于ui/layout/constraints

5ktev3wc

5ktev3wc2#

试试这个代码:

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 const MaterialApp(
      debugShowCheckedModeBanner: false,
      home: 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(
      body: SizedBox(
        height: double.infinity,
        width: double.infinity,
        child: Align(
          alignment: const Alignment(0.0, -0.5),
          child: Stack(
            alignment: Alignment.topCenter,
            children: [
              Container(
                height: 50,
                width: 50,
                color: Colors.amber,
              ),
            ],
          ),
        ),
      ),
    );
  }
}

相关问题