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的精彩世界!

相关推荐
耳東陈2 小时前
【重磅发布】flutter_chen_updater - 版本升级更新
flutter
wordbaby6 小时前
Flutter列表渲染的"诡异"问题:为什么我的数据总是第一个?
前端·flutter
恋猫de小郭6 小时前
谷歌开启 Android 开发者身份验证,明年可能开始禁止“未经验证”应用的侧载,要求所有开发者向谷歌表明身份
android·前端·flutter
苏元1 天前
Flutter + GetX:Dio 多接口 401 拦截后跳登录,避免重复跳转和 Controller 找不到问题
flutter
Mhua_Z1 天前
使用 flutter_tts 的配置项
flutter
你听得到111 天前
弹窗库1.1.0版本发布!不止于统一,更是全面的体验升级!
android·前端·flutter
RaidenLiu1 天前
Riverpod 3 :掌握异步任务处理与 AsyncNotifier
前端·flutter
无知的前端1 天前
Flutter 模型转JSON跳过零值/null
flutter·json
jiushiapwojdap2 天前
Flutter上手记:为什么我的按钮能同时在iOS和Android上跳舞?[特殊字符][特殊字符]
android·其他·flutter·ios
木子雨廷2 天前
Flutter 局部刷新小组件汇总
前端·flutter