【问题标题】:Flutter : Achieved Sliverappbar ShapeFlutter : 实现 Sliverappbar 形状
【发布时间】:2021-05-06 04:02:39
【问题描述】:

我在 Dribble https://dribbble.com/shots/9175650-Beauty-Salon-App/attachments/1218583?mode=media 中找到了一些 Appbar 设计,我尝试创建类似的,但我混淆了彼此的边框半径。

无背景试验 1:

试用 2,脚手架背景颜色为蓝色

Trial 3 与背景颜色深橙色

我怎样才能实现像链接这样的应用栏设计?

源代码

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

class TestingScreen extends StatelessWidget {
  static const routeName = "/testing-screen";
  @override
  Widget build(BuildContext context) {
    var mediaQuery = MediaQuery.of(context);

    return Scaffold(
      body: CustomScrollView(
        slivers: <Widget>[
          SliverAppBar(
            expandedHeight: mediaQuery.size.height / 4,
            backgroundColor: Colors.deepOrange,
            shape: RoundedRectangleBorder(
                borderRadius:
                    BorderRadius.only(bottomRight: Radius.circular(80))),
          ),
          SliverList(
            delegate: SliverChildListDelegate(
              [
                Container(
                  height: mediaQuery.size.height,
                  decoration: BoxDecoration(
                      color: Colors.blue,
                      borderRadius:
                          BorderRadius.only(topLeft: Radius.circular(80))),
                  child: Center(child: Text('data')),
                )
              ],
            ),
          )
        ],
      ),
    );
  }
}

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    你实际上可以使用CustomClipper来做到这一点,方法是PathquadraticBezierTo:

    class MyHomePage extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Container(
            child: Stack(
              children: <Widget>[
                Container(color: Colors.orange),
                ClipPath(
                  clipper: TheCustomClipper(),
                  child: Container(
                    color: Colors.greenAccent,
                  ),
                ),
              ],
            ),
          ),
        );
      }
    }
    
    class TheCustomClipper extends CustomClipper<Path> {
      @override
      getClip(Size size) {
        var path = Path();
        path.lineTo(0, size.height / 3);
        var firstControlPoint = Offset(0, size.height / 3.5); // adjust the height to move start of the first curve
        var firstEndPoint = Offset(size.width / 4.2, size.height / 3.5 + 10); // adjust the width to add the end controll point and height to move end of the first curve
    
        var secControlPoint = Offset(size.width, size.height / 2.8); // adjust the height to move end of the second curve
        var secEndPoint = Offset(size.width, size.height / 3 - 40); // adjust the width to add the right first controll point and height to move start of the second curve
    
        path.quadraticBezierTo(firstControlPoint.dx, firstControlPoint.dy,
            firstEndPoint.dx, firstEndPoint.dy);
        path.quadraticBezierTo(
            secControlPoint.dx, secControlPoint.dy, secEndPoint.dx, secEndPoint.dy);
    
        path.lineTo(size.width, size.height / 3);
        path.lineTo(size.width, 0);
        path.close();
        return path;
      }
    
      @override
      bool shouldReclip(CustomClipper oldClipper) {
        return null;
      }
    }
    

    请注意,我只在第一个小部件中添加了曲线。如果您想在Stack 的第一个Container 中添加另一个CustomClipper,请按相反的逻辑进行。

    输出:

    【讨论】:

      【解决方案2】:

      源代码:

      import 'package:flutter/material.dart';
      
      import '../../../shared/app_colors.dart';
      
      class WelcomenPage extends StatelessWidget {
        static const id = 'welcomen_page';
      
        @override
        Widget build(BuildContext context) {
          return Scaffold(
            backgroundColor: Colors.white,
            body: Stack(
              children: <Widget>[
                ClipPath(
                  child: Container(
                    height: MediaQuery.of(context).size.height,
                    color: AppColors.primary,
                  ),
                  clipper: BottomWaveClipper(),
                ),
              ],
            ),
          );
        }
      }
      
      class BottomWaveClipper extends CustomClipper<Path> {
        @override
        Path getClip(Size size) {
          var path = Path();
          path.lineTo(0, size.height * .65);
      
          var firstControlPoint = Offset(0, size.height * .75);
          var firstEndPoint = Offset(size.width / 6, size.height * .75);
      
          path.quadraticBezierTo(firstControlPoint.dx, firstControlPoint.dy,
              firstEndPoint.dx, firstEndPoint.dy);
      
          path.lineTo(size.width / 1.2, size.height * .75);
      
          var secControlPoint = Offset(size.width, size.height * .75);
          var secEndPoint = Offset(size.width, size.height * 0.85);
      
          path.quadraticBezierTo(
              secControlPoint.dx, secControlPoint.dy, secEndPoint.dx, secEndPoint.dy);
      
          path.lineTo(size.width, 0);
          path.close();
      
          return path;
        }
      
        @override
        bool shouldReclip(CustomClipper<Path> oldClipper) => false;
      }
      

      输出: image

      【讨论】:

        猜你喜欢
        • 2020-07-20
        • 2019-10-25
        • 1970-01-01
        • 2018-11-17
        • 2019-10-03
        • 1970-01-01
        • 1970-01-01
        • 2020-11-15
        • 2020-11-27
        相关资源
        最近更新 更多