【问题标题】:How to use clip path with sliver app bar in flutter如何在颤动中使用带有银色应用栏的剪辑路径
【发布时间】:2021-06-26 06:58:03
【问题描述】:

谁能解释一下如何在flutter中更改sliver应用栏的剪辑路径

【问题讨论】:

  • 用你写的ClipPath发布代码
  • 我知道我应该使用 ClipPath,但我不明白具体如何。请给我看看好吗?
  • class MyClipper extends CustomClipper<Path> { @override Path getClip(Size size) { final r = Offset.zero & size; final r1 = Rect.fromCircle(center: r.bottomRight - Offset(32, 64), radius: 32); final r2 = Rect.fromCircle(center: r.bottomLeft + Offset(32, 0), radius: 32); return Path() ..moveTo(r.topLeft.dx, r.topLeft.dy) ..lineTo(r.topRight.dx, r.topRight.dy) ..arcTo(r1, 0, pi / 2, false) ..arcTo(r2, -pi / 2, -pi / 2, false); } @override bool shouldReclip(covariant CustomClipper<Path> oldClipper) => true; }
  • @pskink 谢谢,我会试试的!
  • @pskink 不,我需要另一个解决方案

标签: flutter dart hybrid-mobile-app


【解决方案1】:
class MyClipper extends CustomClipper<Path> {
  @override Path getClip(Size size) {
    final radius = 32.0;
    final r = Offset.zero & size; 
    final r1 = Rect.fromCircle(center: r.bottomRight - Offset(radius, radius * 2), radius: radius); 
    final r2 = Rect.fromCircle(center: r.bottomLeft + Offset(radius, 0), radius: radius);
    return Path() 
      ..moveTo(r.topLeft.dx, r.topLeft.dy)
      ..lineTo(r.topRight.dx, r.topRight.dy) 
      ..arcTo(r1, 0, pi / 2, false) 
      ..arcTo(r2, -pi / 2, -pi / 2, false);
  }

  @override bool shouldReclip(covariant CustomClipper<Path> oldClipper) => true;
}

答主:pskink

【讨论】:

    【解决方案2】:

    您可以使用它,我的设计支持自动调整剪辑路径并完美运行

    import 'package:flutter/material.dart';
    
    import 'package:unfpa/base/base_stateful_state.dart';
    
    import 'package:unfpa/utils/constants.dart';
    import 'package:unfpa/utils/custom_clip_path.dart';
    import 'package:unfpa/utils/custom_clip_path_two.dart';
    
    class TestWidget extends StatefulWidget {
      TestWidget({Key key}) : super(key: key);
    
      @override
      _TestWidgetState createState() => _TestWidgetState();
    }
    
    class _TestWidgetState extends BaseStatefulState<TestWidget> {
      final ScrollController _sliverScrollController = ScrollController();
      ValueNotifier<bool> isPinned = ValueNotifier(false);
    
      Size size;
    
      @override
      void initState() {
        super.initState();
    
        _sliverScrollController.addListener(() {
          if (!isPinned.value &&
              _sliverScrollController.hasClients &&
              _sliverScrollController.offset > kToolbarHeight) {
            isPinned.value = true;
          } else if (isPinned.value &&
              _sliverScrollController.hasClients &&
              _sliverScrollController.offset < kToolbarHeight) {
            isPinned.value = false;
          }
        });
      }
    
      @override
      Widget build(BuildContext context) {
        size = MediaQuery.of(context).size;
    
        return Scaffold(
            backgroundColor: Colors.white,
            body: CustomScrollView(
              controller: _sliverScrollController,
              slivers: <Widget>[
                ValueListenableBuilder(
                    valueListenable: isPinned,
                    builder: (context, value, child) {
                      return SliverAppBar(
                        elevation: 0,
                        backgroundColor: Colors.red,
                        leading: IconButton(
                            iconSize: 48,
                            color: Colors.white,
                            icon: Icon(Icons.close),
                            onPressed: () => Navigator.of(context).pop()),
                        primary: true,
                        pinned: true,
                        stretch: true,
                        title: Visibility(
                          visible: value,
                          child: Text(
                            "Sliver AppBar Title",
                            style: TextStyle(
                              fontWeight: FontWeight.w700,
                              fontSize: 22.0,
                              color: Colors.white,
                            ),
                          ),
                        ),
                        titleSpacing: 0,
                      );
                    }),
                SliverList(
                    delegate: SliverChildListDelegate([
                  Column(
                    crossAxisAlignment: CrossAxisAlignment.stretch,
                    children: [
                      Container(
                        transform: Matrix4.translationValues(0.0, -2.0, 0.0),
                        alignment: Alignment.center,
                        child: ClipPath(
                          clipper: ClipPathClass(),
                          child: Container(
                            padding: EdgeInsets.only(bottom:20),
                            width: size.width,
                            color: Colors.yellow,
                            child: Container(
                              margin: EdgeInsets.symmetric(
                                  horizontal: 25),
                              child: Column(
                                mainAxisSize: MainAxisSize.min,
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: <Widget>[
                                  SizedBox(height: kToolbarTopMargin),
                                  Text(
                                    "Expanded Sliver title",
                                    style: TextStyle(
                                      fontSize: 30.0,
                                      color: Colors.white,
                                    ),
                                  ),
                                  SizedBox(
                                    height: 5,
                                  ),
                                  Text(
                                    "Expanded sliver description",
                                    style: TextStyle(
                                      fontSize: 17,
                                      color: Colors.white,
                                      height: 1.3,
                                      fontWeight: FontWeight.w700,
                                    ),
                                  )
                                ],
                              ),
                            ),
                          ),
                        ),
                      ),
                    ],
                  )
                ])),
              ],
            ));
      }
    }
    CustomClipPathClass
    import 'package:flutter/material.dart';
    
    class ClipPathClass extends CustomClipper<Path> {
      @override
      Path getClip(Size size) {
        var path = Path();
        path.lineTo(0.0, size.height - 30);
    
        var firstControlPoint = Offset(size.width / 4, size.height);
        var firstPoint = Offset(size.width / 2, size.height);
        path.quadraticBezierTo(firstControlPoint.dx, firstControlPoint.dy,
            firstPoint.dx, firstPoint.dy);
    
        var secondControlPoint = Offset(size.width - (size.width / 4), size.height);
        var secondPoint = Offset(size.width, size.height - 30);
        path.quadraticBezierTo(secondControlPoint.dx, secondControlPoint.dy,
            secondPoint.dx, secondPoint.dy);
    
        path.lineTo(size.width, 0.0);
        path.close();
    
        return path;
      }
    
      @override
      bool shouldReclip(CustomClipper<Path> oldClipper) => false;
    }

    【讨论】:

      猜你喜欢
      • 2020-11-06
      • 2015-02-22
      • 2020-01-16
      • 2022-06-15
      • 2014-09-09
      • 1970-01-01
      • 2019-11-29
      • 1970-01-01
      相关资源
      最近更新 更多