颤振:如何使儿童的容器变大

时间:2020-04-23 21:16:52

标签: flutter dart

根据定义,一个带有孩子的容器会长得足以向他们展示。在我正在开发的此示例中,我无法使容器适合孩子的大小,我必须对“重量”和“高度”进行硬编码,否则,容器会消失(是红色背景的容器,我将完整的代码,因此您可以复制粘贴,但是只有这样我无法控制行为。

import 'package:flutter/material.dart';
import 'package:littlebusiness/logic/Category.dart';
import 'package:hive/hive.dart';

class FormCategoryPage extends StatefulWidget {
  @override
  _FormCategoryPageState createState() => _FormCategoryPageState();
}

class _FormCategoryPageState extends State<FormCategoryPage> {
  final _formKey = GlobalKey<FormState>();

  List<RadioModel> sampleData = new List<RadioModel>();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    sampleData.add(new RadioModel(true, 'A', 0xffe6194B));
    sampleData.add(new RadioModel(false, 'B', 0xfff58231));
    sampleData.add(new RadioModel(false, 'C', 0xffffe119));
    sampleData.add(new RadioModel(false, 'D', 0xffbfef45));
    sampleData.add(new RadioModel(true, 'A', 0xffe6194B));
    sampleData.add(new RadioModel(false, 'B', 0xfff58231));
  }

  String _name;

  Color _color;

  String _selectedValue;

  void addCategory(Category cat) {
    Hive.box('categories').add(cat);
  }

  void getColor(String value) {
    _selectedValue = value;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('PERFORMANCE'),
      ),
      body: Center(
        child: Form(
          key: _formKey,
          child: Padding(
            padding: EdgeInsets.all(16.0),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: <Widget>[
                TextFormField(
                  decoration: const InputDecoration(
//                  hintText: 'Enter your email',
                    labelText: 'Name',
                  ),
                  onSaved: (value) => _name = value,
                  validator: (value) {
                    if (value.isEmpty) {
                      return 'Please enter some text';
                    }
                    return null;
                  },
                ),
                SizedBox(
                  height: 20,
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    Container(
                      height: 100,
                      width: 320,
                      color: Colors.red,
                      child: ListView.builder(
                        scrollDirection: Axis.horizontal,
                        itemCount: sampleData.length,
                        itemBuilder: (BuildContext context, int index) {
                          return InkWell(
                            onTap: () {
                              setState(() {
                                sampleData.forEach(
                                    (element) => element.isSelected = false);
                                sampleData[index].isSelected = true;
                              });
                            },
                            child: RadioItem(sampleData[index]),
                          );
                        },
                      ),
                    ),
                  ],
                ),
                RaisedButton(
                  shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(18.0),
                      side: BorderSide(color: Colors.red)),
                  child: Text('Add New Contact'),
                  color: Colors.teal,
                  textColor: Colors.white,
                  onPressed: () {
                    _formKey.currentState.save();
//                  final newContact = Contact(_name, int.parse(_age));
//                  addContact(newContact);
                  },
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

class RadioItem extends StatelessWidget {
  final RadioModel _item;
  RadioItem(this._item);
  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.all(15.0),
      child: Column(
        mainAxisSize: MainAxisSize.max,
        children: <Widget>[
          Container(
            height: 35.0,
            width: 35.0,
            alignment: Alignment.center,
            child: Container(
                height: 25.0,
                width: 25.0,
                decoration: BoxDecoration(
                  color: Color(_item.colorCode),
                  borderRadius:
                      const BorderRadius.all(const Radius.circular(15)),
                )),
            decoration: BoxDecoration(
              color: Colors.transparent,
              border: Border.all(
                  width: 3.0,
                  color: _item.isSelected
                      ? Color(_item.colorCode)
                      : Colors.transparent),
              borderRadius: const BorderRadius.all(const Radius.circular(25)),
            ),
          ),
          Container(margin: EdgeInsets.only(left: 20.0))
        ],
      ),
    );
  }
}

class RadioModel {
  bool isSelected;
  final String buttonText;
  final int colorCode;
  RadioModel(this.isSelected, this.buttonText, this.colorCode);
}

这是执行器结果: enter image description here 有人知道为什么会这样吗?我迷路了,给double.infinity加上一个不起作用... 谢谢!

2 个答案:

答案 0 :(得分:0)

您必须同时指定宽度和高度,因为Listview是ColumnRow的子代

您可以将Listview中的Row中的RadioItem替换为SingleChildScrollView

Container(
  color: Colors.red,
  child: Builder(
    builder: (context) {
      var childs = <Widget>[];
      for (var item in sampleData) {
        childs.add(RadioItem(item));
      }
      return SingleChildScrollView(
        scrollDirection: Axis.horizontal,
        child: Row(
          children: childs,
        ),
      );
    },
  ),
),

答案 1 :(得分:0)

您可以使用相对值来代替设备,而不是使用智慧和高度的固定值

MediaQuery.of(context).size.width, MediaQuery.of(context).size.height

您也可以像使用它们

MediaQuery.of(context).size.width * 0.5 表示设备屏幕的50%

希望这会有所帮助