首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在行中对齐Alignment.bottomCenter无帮助

在行中对齐Alignment.bottomCenter无帮助
EN

Stack Overflow用户
提问于 2020-04-23 00:03:29
回答 2查看 125关注 0票数 1

Android Studio 2.6 Flutter插件。

我需要4个点把底部和中心的图像。

我尝试这样使用Align小部件:

代码语言:javascript
复制
Align(alignment: Alignment.bottomCenter,

但这无济于事。小圆点位于顶部。

代码如下:

代码语言:javascript
复制
import 'package:carousel_slider/carousel_slider.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:intl/intl.dart';
import 'package:logger/logger.dart';
import '../constants.dart' as Constants;

  Widget _createCarouselContainer() {
    final List<String> imagesFullPathList = [
      'assets/images/campaign1.png'
    ];
    int _current = 0;
    return Container(
        decoration: new BoxDecoration(boxShadow: [_createBoxShadow()]),
        child: new ClipRRect(
            borderRadius: BorderRadius.all(
                Radius.circular(Constants.ROUNDED_CORNER_RADIUS)),
            child: new Stack(children: [
              CarouselSlider(
                  options: CarouselOptions(
                      onPageChanged: (index, reason) {
                        _current = index;
                        _logger.d(
                            "_createCarouselContainer: _current = $_current");
                      },
                      autoPlay: true,
                      viewportFraction: 1.0, // page fills 100% of the carousel
                      height: Constants.CARD_VIEW_HEIGHT),
                  items: imagesFullPathList
                      .map((imageName) => Container(
                            decoration: BoxDecoration(
                              image: DecorationImage(
                                fit: BoxFit.cover,
                                image: AssetImage(imageName),
                              ),
                            ),
                          ))
                      .toList()),
              Align(
                  alignment: Alignment.bottomCenter,
                  child: Row(
                    children: imagesFullPathList.map((url) {
                      int index = imagesFullPathList.indexOf(url);
                      _logger.d("_createCarouselContainer: index =  $index");
                      return Container(
                        width: 10.0,
                        height: 10.0,
                        margin: EdgeInsets.symmetric(
                            vertical: 10.0, horizontal: 2.0),
                        decoration: BoxDecoration(
                          shape: BoxShape.circle,
                          color: _current == index
                              ? Color.fromRGBO(255, 255, 255, 1)
                              : Color.fromRGBO(0, 0, 0, 1),
                        ),
                      );
                    }).toList(),
                  ))
            ])));
  }

这里的结果是:

为什么Align不能提供帮助?

EN

回答 2

Stack Overflow用户

发布于 2020-04-23 00:05:56

您应该在Stack Widget中使用Positioned Widget,而不是Align

要使用Positioned在底部居中对齐Widget,请使用

代码语言:javascript
复制
bottom: 0,
left: 0,
right: 0,

示例代码

代码语言:javascript
复制
Widget _createCarouselContainer() {
    final List<String> imagesFullPathList = [
      'assets/images/campaign1.png'
    ];
    int _current = 0;
    return Container(
        decoration: new BoxDecoration(boxShadow: [_createBoxShadow()]),
        child: new ClipRRect(
            borderRadius: BorderRadius.all(
                Radius.circular(Constants.ROUNDED_CORNER_RADIUS)),
            child: new Stack(children: [
              CarouselSlider(
                  options: CarouselOptions(
                      onPageChanged: (index, reason) {
                        _current = index;
                        _logger.d(
                            "_createCarouselContainer: _current = $_current");
                      },
                      autoPlay: true,
                      viewportFraction: 1.0, // page fills 100% of the carousel
                      height: Constants.CARD_VIEW_HEIGHT),
                  items: imagesFullPathList
                      .map((imageName) => Container(
                            decoration: BoxDecoration(
                              image: DecorationImage(
                                fit: BoxFit.cover,
                                image: AssetImage(imageName),
                              ),
                            ),
                          ))
                      .toList()),
              Positioned(
                  bottom: 0,
                  left: 0,
                  right: 0,
                  child: Row(
                    children: imagesFullPathList.map((url) {
                      int index = imagesFullPathList.indexOf(url);
                      _logger.d("_createCarouselContainer: index =  $index");
                      return Container(
                        width: 10.0,
                        height: 10.0,
                        margin: EdgeInsets.symmetric(
                            vertical: 10.0, horizontal: 2.0),
                        decoration: BoxDecoration(
                          shape: BoxShape.circle,
                          color: _current == index
                              ? Color.fromRGBO(255, 255, 255, 1)
                              : Color.fromRGBO(0, 0, 0, 1),
                        ),
                      );
                    }).toList(),
                  ))
            ])));
  }
票数 2
EN

Stack Overflow用户

发布于 2020-04-23 01:18:34

这是我的解决方案(由MediaQueryPositioned提供)。

代码语言:javascript
复制
Widget _createCarouselContainer(BuildContext context) {
    final List<String> imagesFullPathList = [
      'assets/images/campaign1.png',
    ];
    double dotContainerWidth = 12.0;
    double dotContainerHeight = 12.0;
    double dotHorizontalInsets = 2.0;
    int _current = 0;
    double allDotsContainerWidth =
        dotContainerWidth * imagesFullPathList.length +
            ((imagesFullPathList.length - 1) * dotHorizontalInsets);
    final double screenWidth = MediaQuery.of(context).size.width;
    return Container(
        decoration: new BoxDecoration(boxShadow: [_createBoxShadow()]),
        child: new ClipRRect(
            borderRadius: BorderRadius.all(
                Radius.circular(Constants.ROUNDED_CORNER_RADIUS)),
            child: new Stack(children: [
              CarouselSlider(
                  options: CarouselOptions(
                      onPageChanged: (index, reason) {
                        _current = index;
                        _logger.d(
                            "_createCarouselContainer: _current = $_current");
                      },
                      autoPlay: true,
                      viewportFraction: 1.0, // page fills 100% of the carousel
                      height: Constants.CARD_VIEW_HEIGHT),
                  items: imagesFullPathList
                      .map((imageName) => Container(
                            decoration: BoxDecoration(
                              image: DecorationImage(
                                fit: BoxFit.cover,
                                image: AssetImage(imageName),
                              ),
                            ),
                          ))
                      .toList()),
              Positioned(
                  // in Stack to align widget must use "Positioned"
                  bottom: Constants.DEFAULT_MARGIN,
                  left: screenWidth / 2 - allDotsContainerWidth / 2,
                  child: Row(
                    children: imagesFullPathList.map((url) {
                      int index = imagesFullPathList.indexOf(url);
                      return Container(
                        width: dotContainerWidth,
                        height: dotContainerHeight,
                        margin: EdgeInsets.symmetric(
                            vertical: 10.0, horizontal: dotHorizontalInsets),
                        decoration: BoxDecoration(
                          shape: BoxShape.circle,
                          color: _current == index
                              ? Color.fromRGBO(255, 255, 255, 1)
                              : Color.fromRGBO(0, 0, 0, 1),
                        ),
                      );
                    }).toList(),
                  ))
            ])));
  }

现在圆点在底部和中心。下面是结果:

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61369676

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档