将Card的child部分改为Expanded包裹的ListView,然后在ListView的外层再包裹一个Expanded即可让Card最大化。

代码如下:

class CalllogAddPage extends StatefulWidget {
  const CalllogAddPage({Key? key}) : super(key: key);

  @override
  State<CalllogAddPage> createState() => _CalllogAddPageState();
}

class _CalllogAddPageState extends State<CalllogAddPage> {
  final GlobalKey _formKey = GlobalKey<FormState>();

  Map<String, dynamic> _param = {};

  String? path;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('通话信息上传'),
      ),
      body: Container(
        alignment: Alignment.topCenter,
        child: Card(
          margin: EdgeInsets.only(left: 30, top: 20, right: 30, bottom: 50),
          child: Form(
            key: _formKey,
            child: Expanded(
              child: ListView(
                children: [
                  Container(
                    margin: EdgeInsets.all(10),
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        TextButton(
                          onPressed: () async {
                            FilePickerResult? result = await FilePicker.platform.pickFiles(
                              dialogTitle: '录音选择',
                              type: FileType.audio,
                            );
                            if (result != null) {
                              String? file = result.files.single.path;
                              path = file;
                              setState(() {});
                            } else {
                              // User canceled the picker
                            }
                          },
                          child: Text('文件选择'),
                        ),
                        SizedBox(height: 15),
                        Container(
                          child: Text('$path'),
                        ),
                        SizedBox(height: 15),
                        ElevatedButton(
                          onPressed: () {
                            FormState _form = _formKey.currentState as FormState;
                            _form.save();
                            if (_form.validate()) {
                              if (_param.length < 3) {
                                EasyLoading.showError('请填写完内容');
                                return;
                              }
                            }
                          },
                          child: Text('上传'),
                        )
                      ],
                    ),
                  ),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }
}
Flutter Card 最大化:使用 Expanded 和 ListView

原文地址: https://www.cveoy.top/t/topic/mRea 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录