【问题标题】:Flutter Expanded widget error even if theres a Flex parent即使有 Flex 父级,Flutter Expanded 小部件也会出错
【发布时间】:2020-01-03 00:32:15
【问题描述】:

大家好,你们能帮我解决这个问题吗,我知道 Expanded 小部件需要 Flex 父级。但由于某种原因,我的给出了一个错误:

我的代码是:

import 'package:flutter/material.dart';

class InputPage extends StatefulWidget {
  @override
  _InputPageState createState() => _InputPageState();
}

class _InputPageState extends State<InputPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Center(child: Text('BMI CALCULATOR')),
      ),
      body: Column(
        children: <Widget>[
          Expanded(
                      child: Container(
              child: ReusableCard(colour: Color(0xFF141A3C)),
            ),
          ),
        ],
      ),
    );
  }
}

class ReusableCard extends StatelessWidget {
  //CUSTOM CONSTRUCTOR
  //the color from the Stateful Widget from above is passed in to the INPUT of ReusableCard({PASSED IN HERE}),

  ReusableCard({this.colour});
  Color colour;

  @override
  Widget build(BuildContext context) {
    return Expanded(
      child: Container(
        margin: EdgeInsets.all(15.0),
        // height: 200.0,
        // width: 170.0,
        decoration: BoxDecoration(
          color: colour,
          borderRadius: BorderRadius.circular(10.0),
        ),
      ),
    );
  }
}

更新

我尝试将另一个扩展小部件添加为 Column() 的孩子,但错误再次出现

class _InputPageState extends State<InputPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Center(child: Text('BMI CALCULATOR')),
      ),
      body: Column(
        children: <Widget>[
          Expanded(
            child: Row(
              // <------ change this Container to a Row or Column
              children: <Widget>[
                ReusableCard(colour: Color(0xFF141A3C)),
                ReusableCard(colour: Color(0xFF141A3C)),
              ],
            ),
          ),
          Expanded(
            child: ReusableCard(
              colour: Color(0xFF141A3C),
            ),
          )
        ],
      ),
    );
  }
}

【问题讨论】:

    标签: android flutter flutter-layout


    【解决方案1】:

    您的ReusableCard(返回Expanded 小部件)被包装在Container 类中。这就是给你错误的原因。要解决此问题,您只需将 Container 类更改为 RowColumn

    class _InputPageState extends State<InputPage> {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Center(child: Text('BMI CALCULATOR')),
          ),
          body: Column(
            children: <Widget>[
              Expanded(
                  child: Container( // <------ change this Container to a Row or Column
                       child: ReusableCard(colour: Color(0xFF141A3C)
                  ),
                ),
              ),
            ],
          ),
        );
      }
    }
    

    【讨论】:

    • 您好,这个解决了我的问题,但是当我将另一个展开的小部件作为身体的孩子:Column() 时,错误再次出现。我会更新我的代码
    猜你喜欢
    • 1970-01-01
    • 2023-01-28
    • 2022-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-20
    • 1970-01-01
    相关资源
    最近更新 更多