點燈坊

失くすものさえない今が強くなるチャンスよ

Center 使 Widget 水平垂直置中

Sam Xiao's Avatar 2024-09-05

Center 為 Layout Widget,可將內層 Widget 水平垂直置中

Version

Flutter 3.24

Flutter

cupertino01

  • Hello World 在頁面 水平垂直置中

Home

home.dart

import 'package:flutter/material.dart';

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

  
  Widget build(BuildContext context) {
    return const Scaffold(
        body:
            Center(child: Text(style: TextStyle(fontSize: 30), 'Hello World')));
  }
}
  • 定義 Home page

Line 6


Widget build(BuildContext context) {
  return const Scaffold(
      body:
          Center(child: Text(style: TextStyle(fontSize: 30), 'Hello World')));
 }
  • Center:將內層 widget 垂直水平置中
    • child:設定內層 widget

Conclusion

  • Center 可簡單將內層 widget 垂直水平置中,且不受長寬限制