简体   繁体   English

如何在颤动中将路径剪切为弯曲导航栏的圆形路径

[英]How to clip the path to circular path of curved Navigation bar in flutter

I'm using Curved Navigation Bar in my flutter project.我在我的颤振项目中使用弯曲导航栏 but is not exactly the same as I want.但与我想要的不完全一样。 Can anyone clip it to exact circular like the picture attached?任何人都可以像附图一样将其剪成精确的圆形吗? Here is the clip path code.这是剪辑路径代码。

class NavCustomPainter extends CustomPainter {
double loc;
double s;
Color color;
TextDirection textDirection;

NavCustomPainter(
  double startingLoc, int itemsLength, this.color, this.textDirection) {
final span = 1.0 / itemsLength;
s = 0.2;
double l = startingLoc + (span - s) / 2;
loc = textDirection == TextDirection.rtl ? 0.8 - l : l;
}

 @override
 void paint(Canvas canvas, Size size) {
final paint = Paint()
  ..color = color
  ..style = PaintingStyle.fill;

final path = Path()
  ..moveTo(0, 0)
 ..lineTo((loc + 0.0) * size.width, 0)
  ..lineTo((loc + 0.015) * size.width, 0)
  ..cubicTo(
    (loc + s * 0.05) * size.width,
    size.height * 0.07,
    loc * size.width,
    size.height * 0.65,
    (loc + s * 0.50) * size.width,
    size.height * 0.65,
  )
  ..cubicTo(
    (loc + s + 0.01) * size.width,
    size.height * 0.65,
    (loc + s - s * 0.1) * size.width,
    size.height * 0.07,
    (loc + s - 0.01) * size.width,
    0,
  )
  ..lineTo(size.width, 0)
  ..lineTo(size.width, size.height)
  ..lineTo(0, size.height)
  ..close();
canvas.drawPath(path, paint);

} }

what i have我拥有的

What I want我想要的是

Instead of doing your own painting, I suggest you use the shapes already available out of the box from the Flutter libraries.我建议您使用 Flutter 库中现成可用的形状,而不是自己绘制。

Take a look at this example that takes advantage of the CircularNotchedRectangle shape:看看这个利用CircularNotchedRectangle形状的例子:

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
Widget build(BuildContext context) {
  return Scaffold(
    appBar: AppBar(
      title: Text('Sample Code'),
    ),
    body: Center(
      child: Text('You have pressed the button $_counter times.'),
    ),
    bottomNavigationBar: BottomAppBar(
      shape: const CircularNotchedRectangle(),
      child: Container(height: 50.0,),
    ),
    floatingActionButton: FloatingActionButton(
      onPressed: _incrementCounter,
      tooltip: 'Increment Counter',
      child: Icon(Icons.add),
    ),
    floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
  );
}
}

Which produces the following output:产生以下输出:

例子

You can customise the shape to achieve the output you want exactly.您可以自定义形状以准确实现您想要的输出。 You can even use CircularNotchedRectangle.getOuterPath to get the Path that describes a rectangle with a smooth circular notch, and modify it however you want.您甚至可以使用CircularNotchedRectangle.getOuterPath来获取描述具有平滑圆形凹口的矩形的Path ,并根据需要对其进行修改。

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

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