Android应用-flutter使用Positioned将控件定位到底部中间

文章目录

场景描述

要将Positioned定位到屏幕底部中间的位置,你可以使用MediaQuery来获取屏幕的高度,然后设置Positioned的bottom属性和left或right属性,一般我们left和right都会设置一个值让控制置于合适的位置,那么如何使其位于底部中央?

示例

以下是一个示例代码:

dart 复制代码
import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Positioned Example'),
        ),
        body: MyPositionedWidget(),
      ),
    );
  }
}

class MyPositionedWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    double screenHeight = MediaQuery.of(context).size.height;

    return Stack(
      children: [
        // Your main content goes here
        Center(
          child: Text(
            'Main Content',
            style: TextStyle(fontSize: 20),
          ),
        ),

        // Positioned at the bottom center
        Positioned(
          bottom: 0,
          left: 0,
          right: 0,
          child: Container(
            height: 50,
            color: Colors.blue,
            child: Center(
              child: Text(
                'Positioned at the bottom center',
                style: TextStyle(color: Colors.white),
              ),
            ),
          ),
        ),
      ],
    );
  }
}

解释

在这个例子中,Positioned包含一个具有蓝色背景的Container,该Container位于屏幕的底部中央。bottom: 0确保它位于屏幕底部,而left: 0和right: 0使其水平方向上充满整个屏幕宽度。你可以根据需要调整高度、颜色和内容。


结束语 Flutter是一个由Google开发的开源UI工具包,它可以让您在不同平台上创建高质量、美观的应用程序,而无需编写大量平台特定的代码。我将学习和深入研究Flutter的方方面面。从基础知识到高级技巧,从UI设计到性能优化,欢饮关注一起讨论学习,共同进入Flutter的精彩世界!

相关推荐
Flutter OH41 分钟前
Flutter OH 外接纹理问题定位指南
flutter
未来猫咪花5 小时前
Everything is ViewModel:让状态管理回到对象世界
android·flutter·ios
Flutter OH6 小时前
Flutter OH 日志抓取与过滤指南
flutter
Flutter OH6 小时前
Flutter OH 内存与 GPU 问题定位指南
flutter
Flutter OH7 小时前
Flutter OH 多设备适配问题定位指南
flutter
律宏阔8 小时前
Flutter Hooks 与 flutter_map_animations 冲突:第二次地图动画报错的解决方案
flutter
Flutter OH12 小时前
Flutter OH 卡死冻屏问题定位指南
flutter·命令模式
2501_9160074712 小时前
Flutter与游戏App加固避坑指南:如何为混合开发与高交互应用选对安全方案?
安全·flutter·游戏·ios·小程序·uni-app·iphone
恋猫de小郭13 小时前
Flutter A2UI 深度解析,它是怎么提供动态生产力的,然后为什么 A2UI 不只是 Flutter
android·前端·flutter