简体   繁体   English

在 Flutter 的 Stack() 中只滚动一个 Widget

[英]Scroll only one Widget inside Stack() in Flutter

I'm new to Flutter and I'm trying to build my first app.我是 Flutter 的新手,我正在尝试构建我的第一个应用程序。 I want my HomePage to have a small image on the top and its content on the rest of the page.我希望我的主页在顶部有一个小图像,其内容在页面的 rest 上。 When scrolling, I want the image to behave like a Parallax effect, staying fixed and the grey ClipPath() widget scrolling over it.滚动时,我希望图像表现得像视差效果一样,保持固定,灰色的 ClipPath() 小部件在其上滚动。

I've already tried a few approaches and I'm not sure if I'm using the correct elements on this page but this was the only way I managed to position everything the way I wanted so far.我已经尝试了几种方法,但我不确定我是否在该页面上使用了正确的元素,但这是我到目前为止按照我想要的方式成功实现 position 的唯一方法。

However, even using SingleChildScrollView() as this the Container/ClipPath() father, I still can't scroll the page.但是,即使使用 SingleChildScrollView() 作为 Container/ClipPath() 的父亲,我仍然无法滚动页面。

Could you please help me with that?你能帮我吗? Thank you all.谢谢你们。

在此处输入图像描述

// hope.page.dart:

import 'package:flutter/material.dart';
import 'package:remind_md/ui/shared/clippers/content.clipper.dart';

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        toolbarHeight: 40,
        actions: <Widget>[
            // action button
            IconButton(
              icon: Icon(Icons.notifications, color: Colors.white),
              onPressed: () {
                
              },
            ),
            // action button
            IconButton(
              icon: Icon(Icons.settings, color: Colors.white),
              onPressed: () {
                
              },
            ),
          ],
      ),
      body: Stack(
        children: <Widget>[
          Container(
            decoration: BoxDecoration(
              gradient: LinearGradient(
                colors: [Color(0xff82d9e8), Color(0xff27acc1)],
                begin: Alignment.bottomLeft,
                end: Alignment.topRight
              )
            ),
            child: OverflowBox(
              alignment: Alignment(-0.75,-1.05),
              maxHeight: MediaQuery.of(context).size.height * 2,
              child: Image.asset(
                'images/home_doctors.png', 
                scale: 1.05,
              ),
            )
          ),  
          Positioned(
            top: MediaQuery.of(context).size.height*0.2 ,
            child: SingleChildScrollView(
              scrollDirection: Axis.vertical,
              child: Container(
                child: ClipPath(
                  clipper: ContentClipper(),
                  child: Container(
                    padding: EdgeInsets.only(top: 40),
                    width: MediaQuery.of(context).size.width,
                    color: Color(0xfff4f4f4),
                    child: Column(
                      children: [
                        Row(
                          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                          crossAxisAlignment: CrossAxisAlignment.start,
                          mainAxisSize: MainAxisSize.min,
                          children: <Widget>[
                            Container(
                              width: 200,
                              height: 100,
                              child: Card(
                                elevation: 2,
                                shape: RoundedRectangleBorder(borderRadius:  BorderRadius.circular(7)),
                                child: Padding(
                                  padding: const EdgeInsets.all(5.0),
                                  child: Stack(
                                    children: <Widget>[
                                      Positioned(
                                         right: 3,
                                          top: 0,
                                          child: Opacity(
                                          opacity: 0.2,
                                          child: Image.asset('images/card_calendar.png', width: 100, height: 75)
                                        ),
                                      ),
                                      Positioned(
                                        top: 0,
                                        left: 5,
                                        child: Column(
                                          crossAxisAlignment: CrossAxisAlignment.start,
                                          children: [
                                            Text(
                                              '12 dias',
                                              textAlign: TextAlign.left, 
                                              style: TextStyle(fontFamily: 'BrunoAce', fontSize: 26, color: Color(0xff27acc1))
                                            ),
                                            Text(
                                              'até próxima',
                                              textAlign: TextAlign.left, 
                                              style: TextStyle(fontFamily: 'BrunoAce', fontSize: 18, color: Color(0xff27acc1))
                                            ),
                                            Text(
                                              'consulta.',
                                              textAlign: TextAlign.justify,
                                              style: TextStyle(fontFamily: 'BrunoAce', fontSize: 18, color: Color(0xff27acc1))
                                            ),
                                          ],
                                        ),
                                      )
                                    ]
                                  ),
                                ),
                              ),
                            ),
                            Container(
                              width: 200,
                              height: 100,
                              child: Card(
                                elevation: 2,
                                shape: RoundedRectangleBorder(borderRadius:  BorderRadius.circular(7)),
                                child: Padding(
                                  padding: const EdgeInsets.all(5.0),
                                  child: Stack(
                                    children: <Widget>[
                                      Positioned(
                                         right: 3,
                                          top: 0,
                                          child: Opacity(
                                          opacity: 0.2,
                                          child: Image.asset('images/pill_case.png', width: 100, height: 75)
                                        ),
                                      ),
                                      Positioned(
                                        top: 0,
                                        left: 5,
                                        child: Column(
                                          crossAxisAlignment: CrossAxisAlignment.start,
                                          children: [
                                            Text(
                                              '31 dias',
                                              textAlign: TextAlign.left, 
                                              style: TextStyle(fontFamily: 'BrunoAce', fontSize: 26, color: Color(0xff27acc1))
                                            ),
                                            Text(
                                              'até comprar',
                                              textAlign: TextAlign.left, 
                                              style: TextStyle(fontFamily: 'BrunoAce', fontSize: 18, color: Color(0xff27acc1))
                                            ),
                                            Text(
                                              'medicamento.',
                                              textAlign: TextAlign.justify,
                                              style: TextStyle(fontFamily: 'BrunoAce', fontSize: 18, color: Color(0xff27acc1))
                                            ),
                                          ],
                                        ),
                                      )
                                    ]
                                  ),
                                ),
                              ),
                            )
                          ],
                        ),
                        Card(
                          elevation: 2,
                          shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(7)),
                          child: Row(
                            children: <Widget>[
                              Container(
                                child: Image.asset('images/card_pills.png'),
                                height: 80,
                                width: 80,
                                decoration: BoxDecoration(
                                  color: Color(0xfff47e71),
                                  borderRadius: BorderRadius.all(Radius.circular(7))
                                ),
                              )
                            ],
                          ),
                        ),
                        Card(
                          elevation: 2,
                          shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(7)),
                          child: Row(
                            children: <Widget>[
                              Container(
                                child: Image.asset('images/card_stet_heart.png'),
                                height: 80,
                                width: 80,
                                decoration: BoxDecoration(
                                  color: Color(0xff3865b9),
                                  borderRadius: BorderRadius.all(Radius.circular(7))
                                ),
                              )
                            ],
                          ),
                        ),
                        Card(
                          elevation: 2,
                          shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(7)),
                          child: Padding(
                            padding: const EdgeInsets.all(8.0),
                            child: Column(
                              children: <Widget>[
                                Text('Histórico', style: TextStyle(fontFamily: 'BrunoAce', fontSize: 24, color: Color(0xff27acc1),  )  ),
                                Divider(),
                                Row(
                                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                                  children: <Widget>[
                                    Image.asset('images/list_credit_card.png'),
                                    Text('1 - <MEDICAMENTO> - 19/08/2020', style: TextStyle(fontFamily: 'BrunoAce', fontSize: 16, color: Color(0xff27acc1)  )  ),
                                  ],
                                ),
                                Divider(),
                                Row(
                                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                                  children: <Widget>[
                                    Image.asset('images/list_credit_card.png'),
                                    Text('1 - <MEDICAMENTO> - 19/08/2020', style: TextStyle(fontFamily: 'BrunoAce', fontSize: 16, color: Color(0xff27acc1)  )  ),
                                  ],
                                ),
                                Divider(),
                                Row(
                                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                                  children: <Widget>[
                                    Image.asset('images/list_pill.png'),
                                    Text('30 - <MEDICAMENTO> - 19/08/2020', style: TextStyle(fontFamily: 'BrunoAce', fontSize: 16, color: Color(0xff27acc1)  )  ),
                                  ],
                                ),
                                Divider(),
                                Row(
                                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                                  children: <Widget>[
                                    Image.asset('images/list_pill.png'),
                                    Text('1 - <MEDICAMENTO> - 19/08/2020', style: TextStyle(fontFamily: 'BrunoAce', fontSize: 16, color: Color(0xff27acc1)  )  ),
                                  ],
                                ),
                                Divider()
                              ],
                            ),
                          ),
                        )
                      ],
                    ) 
                  )
                ),
              ),
            ),
          ),
        ]
      ),
    );
  }
}

Wrap the SingleChildScrollView widget with an expanded widget.使用expanded的小部件包装 SingleChildScrollView 小部件。
It should do the work.它应该做的工作。 Because for the singleChildScrollView to work, it does not some height and width to scroll.因为要使 singleChildScrollView 正常工作,它不会滚动某些高度和宽度。
Hope it helps.希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM