【问题标题】:Flutter column widget: childs with different heights颤振列小部件:不同高度的孩子
【发布时间】:2018-10-27 10:56:24
【问题描述】:

我正在尝试创建一个小型测试应用程序,该应用程序具有一个较大的中心区域,我将在其上呈现图像,而一个较小的底部区域将包含一个水平滚动的小部件列表。代码如下:

import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:async';
import 'dart:isolate';    
import 'package:fluttertoast/fluttertoast.dart';    
import 'package:photo_app_ui_test/fxmanager/fx_manager.dart';

////    
void main() {
  runApp(SampleApp());
}

class SampleApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Sample App',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: SampleAppPage(),
    );
  }
}

class SampleAppPage extends StatefulWidget {
  SampleAppPage({Key key}) : super(key: key);

  @override
  _SampleAppPageState createState() => _SampleAppPageState();
}

class _SampleAppPageState extends State<SampleAppPage> {
  FxManager fxManager;
  bool      showLoadingDialog = true;

  @override
  void initState() {
    super.initState();
    //loadData();
    fxManager = FxManager();
    fxManager.init().then( (dynamic) => initInterface() );
  }


  void initInterface(){
    setState(() {
      showLoadingDialog = false;
    });
  }

  getBody() {
    if (showLoadingDialog) {
      return getProgressDialog();
    } else {
      return getEffectsWidget();//getListView();
    }
  }

  getProgressDialog() {
    return Center(child: CircularProgressIndicator());
  }

  getEffectsWidget() {
    return

      Column(
          children: <Widget>[

            Expanded(child: Container(color: Color.fromARGB(255, 255, 0, 0),
                child: Center(child: Text("Image")))),
            Flexible( child: Container(
                color: Color.fromARGB(255, 0, 255, 0),
                child: ListView(
                    scrollDirection: Axis.horizontal,
                    children: _getListData()
                )))
          ]);
  }

  _getListData() {
    List<Widget> widgets = [];
    for (int i = 0; i < 100; i++) {
      widgets.add(Padding(
          padding: EdgeInsets.all(10.0),
          child: Column(
            children: <Widget>[
              /*
              Expanded(
                child: Container(),
              ),*/
              FlatButton(
                  onPressed: () => {},
                  color: Colors.orange,
                  padding: EdgeInsets.all(10.0),
                  child: Column(
                    // Replace with a Row for horizontal icon + text
                    children: <Widget>[Icon(Icons.add), Text("Add")],
                  )),
            ],
          )));
    }
    return widgets;
  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text("Sample App"),
        ),
        body: getBody());
  }


}

产生这个结果:

我想让绿色盒子的高度尽可能小:

如果ListView 小部件是Column 小部件的直接子级,则会抛出通常的Horizontal viewport was given unbounded height 异常。所以我必须将它嵌入到 FlexibleExpanded 中。

【问题讨论】:

  • 问题是什么?
  • “我想让绿色盒子的高度尽可能小”。我可以改写为:“如何使绿色框的高度尽可能小,就像第二张图片中显示的那样?”。
  • 你不是已经在第二张图片中做到了这一点吗?
  • 啊,不。我用 Gimp 编辑了它^^
  • 您可以通过计算高度比率来获得所需的输出,例如,如果您为图像设置了 flex 因子 4,它应该给您想要的,否则您将不得不给图像一个固定的高度,但我知道这不是你想做的事

标签: flutter flutter-layout


【解决方案1】:

期望的输出:

getEffectsWidget() {
    return Column(children: <Widget>[
      Expanded(
          child: Container(
              color: Color.fromARGB(255, 255, 0, 0),
              child: Center(child: Text("Image")))),

       Container(
            color: Color.fromARGB(255, 0, 255, 0),
            child: SingleChildScrollView(
              scrollDirection: Axis.horizontal,
              child: Row(
                children: _getListData(),
              ),
            ),
      )
    ]);
  }

static _getListData() {
    List<Widget> widgets = [];
    for (int i = 0; i < 100; i++) {
      widgets.add(Padding(
          padding: EdgeInsets.only(right: 10.0), 
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: <Widget>[
              /*
              Expanded(
                child: Container(),
              ),*/
              FlatButton(
                  onPressed: () => {},
                  color: Colors.orange,
                  padding: EdgeInsets.all(10.0),
                  child: Column(
                    mainAxisSize: MainAxisSize.min,
                    // Replace with a Row for horizontal icon + text
                    children: <Widget>[Icon(Icons.add), Text("Add")],
                  )),
            ],
          )));
    }
    return widgets;
  }

【讨论】:

  • 此解决方案虽然实现了与我想要的类似的效果,但并没有使绿框高度尽可能小。根据第一个 Expanded 小部件的 flex 值,您甚至可能会在设备上出现“A RenderFlex 在底部溢出 5.1 像素”这样的异常,而在另一台设备上也不会出现异常。
  • 移除了弹性系数 - 现在自动调整。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-23
  • 2021-07-07
  • 2020-11-25
  • 1970-01-01
  • 2020-02-16
  • 2021-05-24
相关资源
最近更新 更多