抖动-底部溢出像素数

问题描述 投票:0回答:2

我正在使用Expanded小部件及其子项Container,并且我为double.infinity提供了宽度。我正在尝试将ListView添加为Container的子级,我希望ListView的高度从屏幕顶部到屏幕底部。我的屏幕顶部已经有一个名为BuildHeaderPage的小部件。因此,我希望Container小部件的高度从BuildHeaderPage小部件的底部开始到屏幕底部。

下面是我的代码:

return Scaffold(
  appBar: AppBar(
    elevation: 0,
    title: Text("Dry Cough"),
    backgroundColor: bgroundHeaderColor,
    actions: <Widget>[
      IconButton(
        icon: Icon(Icons.more_vert),
        onPressed: () {},
      )
    ],
  ),
  resizeToAvoidBottomInset: false,
  body: Column(
    crossAxisAlignment: CrossAxisAlignment.start,
    children: <Widget>[
      BuildHeaderPage(bgroundHeaderColor: bgroundHeaderColor),
      Padding(
        padding: new EdgeInsets.symmetric(vertical: 8, horizontal: 8),
        child: Expanded(
          child: Container(
            width: double.infinity,
            decoration: BoxDecoration(
              color: kPrimaryColor.withOpacity(0.3),
            ),
            child: ListView(
              children: <Widget>[
                ListTile(
                  onTap: () {},
                  title: Text("Data"),
                ),
                ListTile(
                  onTap: () {},
                  title: Text("Data"),
                ),
                ListTile(
                  onTap: () {},
                  title: Text("Data"),
                ),
                ListTile(
                  onTap: () {},
                  title: Text("Data"),
                ),
                ListTile(
                  onTap: () {},
                  title: Text("Data"),
                ),
                ListTile(
                  onTap: () {},
                  title: Text("Data"),
                ),
                ListTile(
                  onTap: () {},
                  title: Text("Data"),
                ),
                ListTile(
                  onTap: () {},
                  title: Text("Data"),
                ),
              ],
            ),
          ),
        ),
      )
    ],
  ),
);

这里是BuildHeaderPage小部件:

return Container(
  decoration: BoxDecoration(
    color: bgroundHeaderColor,
    borderRadius: BorderRadius.only(
      bottomLeft: Radius.circular(32),
      bottomRight: Radius.circular(32),
    ),
  ),
  padding: new EdgeInsets.all(8),
  width: double.infinity,
  height: MediaQuery.of(context).size.height / 4,
  child: Stack(
    children: <Widget>[
      // buildAppBar(context),
      Padding(
        padding: const EdgeInsets.only(left: 16),
        child: Align(
          alignment: Alignment.topCenter,
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: <Widget>[
              RichText(
                text: TextSpan(
                  children: [
                    TextSpan(
                      text: "Are you feeling sick?\n",
                      style: TextStyle(
                          height: 1.5,
                          color: Colors.white,
                          fontWeight: FontWeight.bold,
                          fontSize: 22),
                    ),
                    TextSpan(text: "Please contact your local hospitalize.")
                  ],
                ),
              ),
              Image.asset(
                "assets/images/cough.png",
                height: 80,
                width: 80,
                fit: BoxFit.cover,
              ),
            ],
          ),
        ),
      ),
      Stack(
        alignment: Alignment.center,
        children: <Widget>[
          Positioned(
            bottom: 0,
            child: FlatButton(
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(12),
              ),
              padding: EdgeInsets.symmetric(
                  vertical: 12,
                  horizontal: MediaQuery.of(context).size.width / 4),
              textTheme: ButtonTextTheme.primary,
              color: Colors.redAccent,
              onPressed: () {},
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Text(
                    "Call Now",
                    style: TextStyle(
                      fontSize: 18,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                  SizedBox(
                    width: 8,
                  ),
                  Icon(Icons.call),
                ],
              ),
            ),
          ),
        ],
      ),
    ],
  ),
);

您可以看到我的代码。因此,我希望高度Expanded小部件及其子Container小部件应从BuildHeaderPage开始到屏幕底部。

This is App Screenshot

android ios flutter widget flutter-layout
2个回答
0
投票

[很遗憾,我现在无法在电话上进行测试,因为我正在电话上打字,但这应该可以工作,您需要将Expanded设为Column的直接子代,并且可以在ListView内进行填充。

替换:

Column(
  crossAxisAlignment: CrossAxisAlignment.start,
  children: <Widget>[
    BuildHeaderPage(bgroundHeaderColor: bgroundHeaderColor),
    Padding( // wrong
      padding: new EdgeInsets.symmetric(vertical: 8, horizontal: 8),
      child: Expanded(...),
    ),
  ],
)

Column(
  crossAxisAlignment: CrossAxisAlignment.start,
  children: <Widget>[
    BuildHeaderPage(bgroundHeaderColor: bgroundHeaderColor),
    Expanded(child: ListView(...)) // right
  ],
)

0
投票

在代码中进行以下更改

首先将Padding小部件包装在Expanded小部件内>

Expanded小部件中删除Padding小部件,因为Expanded小部件需要columnrow作为父小部件

样本代码

import 'package:flutter/material.dart';
import 'package:flutter/painting.dart';

final Color darkBlue = Color.fromARGB(255, 18, 32, 47);

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue),
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: Center(
          child: MyWidget(),
        ),
      ),
    );
  }
}

class MyWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        elevation: 0,
        title: Text("Dry Cough"),
        backgroundColor: Colors.red,
        actions: <Widget>[
          IconButton(
            icon: Icon(Icons.more_vert),
            onPressed: () {},
          )
        ],
      ),
      resizeToAvoidBottomInset: false,
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Container(
            decoration: BoxDecoration(
              color: Colors.green,
              borderRadius: BorderRadius.only(
                bottomLeft: Radius.circular(32),
                bottomRight: Radius.circular(32),
              ),
            ),
            padding: new EdgeInsets.all(8),
            width: double.infinity,
            height: MediaQuery.of(context).size.height / 4,
            child: Stack(
              children: <Widget>[
                // buildAppBar(context),
                Padding(
                  padding: const EdgeInsets.only(left: 16),
                  child: Align(
                    alignment: Alignment.topCenter,
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceAround,
                      children: <Widget>[
                        RichText(
                          text: TextSpan(
                            children: [
                              TextSpan(
                                text: "Are you feeling sick?\n",
                                style: TextStyle(
                                    height: 1.5,
                                    color: Colors.white,
                                    fontWeight: FontWeight.bold,
                                    fontSize: 22),
                              ),
                              TextSpan(text: "Please contact your local hospitalize.")
                            ],
                          ),
                        ),
                        Image.asset(
                          "assets/images/cough.png",
                          height: 80,
                          width: 80,
                          fit: BoxFit.cover,
                        ),
                      ],
                    ),
                  ),
                ),
                Stack(
                  alignment: Alignment.center,
                  children: <Widget>[
                    Positioned(
                      bottom: 0,
                      child: FlatButton(
                        shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(12),
                        ),
                        padding: EdgeInsets.symmetric(
                            vertical: 12,
                            horizontal: MediaQuery.of(context).size.width / 4),
                        textTheme: ButtonTextTheme.primary,
                        color: Colors.redAccent,
                        onPressed: () {},
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: <Widget>[
                            Text(
                              "Call Now",
                              style: TextStyle(
                                fontSize: 18,
                                fontWeight: FontWeight.bold,
                              ),
                            ),
                            SizedBox(
                              width: 8,
                            ),
                            Icon(Icons.call),
                          ],
                        ),
                      ),
                    ),
                  ],
                ),
              ],
            ),
          ),
          Expanded(
            child: Padding(
              padding:  EdgeInsets.symmetric(vertical: 8, horizontal: 8),
              child: Container(
                width: double.infinity,
                decoration: BoxDecoration(
                  color: Colors.blueGrey.withOpacity(0.3),
                ),
                child: ListView(
                  shrinkWrap: true,
                  children: <Widget>[
                    ListTile(
                      onTap: () {},
                      title: Text("Data"),
                    ),
                    ListTile(
                      onTap: () {},
                      title: Text("Data"),
                    ),
                    ListTile(
                      onTap: () {},
                      title: Text("Data"),
                    ),
                    ListTile(
                      onTap: () {},
                      title: Text("Data"),
                    ),
                    ListTile(
                      onTap: () {},
                      title: Text("Data"),
                    ),
                    ListTile(
                      onTap: () {},
                      title: Text("Data"),
                    ),
                    ListTile(
                      onTap: () {},
                      title: Text("Data"),
                    ),
                    ListTile(
                      onTap: () {},
                      title: Text("Data"),
                    ),
                  ],
                ),
              ),
            ),
          )
        ],
      ),
    );
  }
}

输出

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.