flutter 不知道如何定位和修改容器的大小

kkih6yb8  于 2023-01-21  发布在  Flutter
关注(0)|答案(2)|浏览(144)

我现在正在flutter中制作一个番茄应用程序,我几乎完成了,除了我不能定位和修改显示番茄文本和番茄数量的容器的大小。
'

import 'dart:async';

import 'package:flutter/material.dart';

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

  @override
  State<HomeScreen> createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  static const twentyFiveMinutes = 1500;
  int totalSeconds = twentyFiveMinutes;
  bool isRunning = false;
  int totalPomodoros = 0;
  late Timer timer;
  // late is a modifier that means that you don't have to initialize the property immediately but you have a promise (a contract) that you will initialize the property before you use it
  // it basically means you'll initializer later

  void onTick(Timer timer) {
    if (totalSeconds == 0) {
      setState(() {
        totalPomodoros = totalPomodoros + 1;
        isRunning = false;
        totalSeconds = twentyFiveMinutes;
      });
      timer.cancel();
    } else {
      setState(() {
        totalSeconds = totalSeconds - 1;
      });
    }
  }

  void onStartPressed() {
    timer = Timer.periodic(
      const Duration(seconds: 1),
      onTick,
    );
    setState(() {
      isRunning = true;
    }); //onTick() will gonna call the function immidiately
  }

  void onPausePressed() {
    timer.cancel();
    setState(() {
      isRunning = false;
    });
  }

  void onResetPressed() {
    timer.cancel();
    setState(() {
      isRunning = false;
      totalSeconds = twentyFiveMinutes; //totalSeconds;
    });
  }

  String format(int seconds) {
    var duration = Duration(seconds: seconds);
    print(duration.toString());
    print(duration.toString().split("."));
    print(duration.toString().split(".").first);
    print(duration.toString().split(".").first.substring(2, 7));
    return duration.toString().split(".").first.substring(
          2,
          7,
        ); //Splits the string at matches of [pattern] and returns a list of substrings.
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Theme.of(context).backgroundColor,
      body: Column(
        children: [
          Flexible(
            flex: 1,
            child: Container(
              child: Text(
                format(totalSeconds),
                style: TextStyle(
                  color: Theme.of(context).cardColor,
                  fontSize: 89,
                  fontWeight: FontWeight.w600,
                ),
              ),
            ),
          ),

          Flexible(
            flex: 3,
            child: Center(
              child: IconButton(
                iconSize: 120,
                color: Theme.of(context).cardColor,
                onPressed: isRunning ? onPausePressed : onStartPressed,
                icon: Icon(
                  isRunning
                      ? Icons.pause_circle_outline_outlined
                      : Icons.play_circle_outlined,
                ),
              ),
            ),
          ),

          //child ==> Creates a widget that controls how a child of a [Row], [Column], or [Flex] flexes. Thus, must use child, not children
          //Column ==> Creates a vertical array of children. Thus, must use children, not child
          Flexible(
            flex: 3,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                IconButton(
                  iconSize: 100,
                  color: Theme.of(context).cardColor,
                  onPressed: isRunning ? onResetPressed : onResetPressed,
                  icon: const Icon(
                    Icons.restore_outlined,
                  ),
                ),
              ],
            ),
          ),

          Flexible(
            flex: 1,
            child: Row(
              children: [
                Expanded(
                  child: Container(
                    //alignment: AlignmentDirectional.bottomEnd,
                    decoration: BoxDecoration(
                        color: Theme.of(context).cardColor,
                        borderRadius: BorderRadius.circular(50)),
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Text(
                          'Pomodoros',
                          style: TextStyle(
                            fontSize: 20,
                            color: Theme.of(context).textTheme.headline1!.color,
                            fontWeight: FontWeight.w600,
                          ),
                        ),
                        Text(
                          '$totalPomodoros',
                          style: TextStyle(
                            fontSize: 58,
                            color: Theme.of(context).textTheme.headline1!.color,
                            fontWeight: FontWeight.w600,
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
              ],
            ),
          ),
        ], // Flexible alows you to create UIs that don't really have hard coded values like 200 pixels tall or 100 pixels wide
        // alows you to create UIs that are more flexible, more elastic, more based on proportions
        // can specify the ratio so that there is the least difference between the IOS and the Android platform
      ),
    );
  }
}

Current App UI
我尝试添加对齐:alignmentdirectional.bottomend syntex,但结果它的工作方式与我预期的不同。
Error version App UI
子级:容器(对齐方式:对齐方向、底端、装饰: Package 盒装饰(颜色:主题.属于(上下文).cardColor,borderRadius:圆形边界半径(50)),
我希望将容器放置在UI的底部,并将其大小调整得稍大一些

7xzttuei

7xzttuei1#

首先,你需要从你的flexible widget中删除所有的flex值。然后,在所有flexible widget之间添加Spacer。在最后一个widget(Pomodoros Widget)中添加flex来增加它的高度。你也可以用SafeArea来 Package 你的列,以避开mobiles顶部状态栏和底部状态栏(可选)。如果它回答了你的问题,请检查以下代码:

@override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Theme.of(context).backgroundColor,
      body: SafeArea(
        child: Column(
          mainAxisSize: MainAxisSize.max,
          children: [
            Flexible(
              child: Container(
                child: Text(
                  format(totalSeconds),
                  style: TextStyle(
                    color: Theme.of(context).cardColor,
                    fontSize: 89,
                    fontWeight: FontWeight.w600,
                  ),
                ),
              ),
            ),
            Spacer(),
            Flexible(
              child: Center(
                child: IconButton(
                  iconSize: 120,
                  color: Theme.of(context).cardColor,
                  onPressed: isRunning ? onPausePressed : onStartPressed,
                  icon: Icon(
                    isRunning
                        ? Icons.pause_circle_outline_outlined
                        : Icons.play_circle_outlined,
                  ),
                ),
              ),
            ),
            Spacer(),
            //child ==> Creates a widget that controls how a child of a [Row], [Column], or [Flex] flexes. Thus, must use child, not children
            //Column ==> Creates a vertical array of children. Thus, must use children, not child
            Flexible(
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  IconButton(
                    iconSize: 100,
                    color: Theme.of(context).cardColor,
                    onPressed: isRunning ? onResetPressed : onResetPressed,
                    icon: const Icon(
                      Icons.restore_outlined,
                    ),
                  ),
                ],
              ),
            ),
            Spacer(),
            Flexible(
              flex: 2,
              child: Row(
                children: [
                  Expanded(
                    child: Container(
                      //alignment: AlignmentDirectional.bottomEnd,
                      decoration: BoxDecoration(
                          color: Theme.of(context).cardColor,
                          borderRadius: BorderRadius.circular(50)),
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Text(
                            'Pomodoros',
                            style: TextStyle(
                              fontSize: 20,
                              color: Theme.of(context).textTheme.headline1!.color,
                              fontWeight: FontWeight.w600,
                            ),
                          ),
                          Text(
                            '$totalPomodoros',
                            style: TextStyle(
                              fontSize: 58,
                              color: Theme.of(context).textTheme.headline1!.color,
                              fontWeight: FontWeight.w600,
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ], // Flexible alows you to create UIs that don't really have hard coded values like 200 pixels tall or 100 pixels wide
          // alows you to create UIs that are more flexible, more elastic, more based on proportions
          // can specify the ratio so that there is the least difference between the IOS and the Android platform
        ),
      ),
    );
  }

py49o6xq

py49o6xq2#

我是这样解决你的问题的:还添加了一些注解,用于设置容器宽度和设置文本位置,您也可以更改下面的代码用于您的用户界面

body: Column(
    children: [
      Flexible(
        child: Container(
          child: Text(
            format(totalSeconds),
            style: TextStyle(
              color: Theme.of(context).cardColor,
              fontSize: 89,
              fontWeight: FontWeight.w600,
            ),
          ),
        ),
      ),
      Spacer(),

      Flexible(
        child: Center(
          child: IconButton(
            iconSize: 120,
            color: Theme.of(context).cardColor,
            onPressed: isRunning ? onPausePressed : onStartPressed,
            icon: Icon(
              isRunning
                  ? Icons.pause_circle_outline_outlined
                  : Icons.play_circle_outlined,
            ),
          ),
        ),
      ),
      Spacer(),
      //child ==> Creates a widget that controls how a child of a [Row], [Column], or [Flex] flexes. Thus, must use child, not children
      //Column ==> Creates a vertical array of children. Thus, must use children, not child
      Flexible(
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            IconButton(
              iconSize: 100,
              color: Theme.of(context).cardColor,
              onPressed: isRunning ? onResetPressed : onResetPressed,
              icon: const Icon(
                Icons.restore_outlined,
              ),
            ),
          ],
        ),
      ),
      Spacer(),
      Flexible(
        flex: 2,
        child: Row(
          children: [
            Expanded(
              child: Container(
                decoration: BoxDecoration(
                    color: Theme.of(context).cardColor,
                    borderRadius: BorderRadius.circular(50)),
                child: Column(
                  //You can change position from here
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Text(
                      'Pomodoros',
                      style: TextStyle(
                        fontSize: 20,
                        color: Theme.of(context).textTheme.headline1!.color,
                        fontWeight: FontWeight.w600,
                      ),
                    ),
                    Text(
                      '$totalPomodoros',
                      style: TextStyle(
                        fontSize: 58,
                        color: Theme.of(context).textTheme.headline1!.color,
                        fontWeight: FontWeight.w600,
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    ], // Flexible alows you to create UIs that don't really have hard coded values like 200 pixels tall or 100 pixels wide
    // alows you to create UIs that are more flexible, more elastic, more based on proportions
    // can specify the ratio so that there is the least difference between the IOS and the Android platform
  ),

我希望它的帮助你..快乐编码:}

相关问题