【问题标题】:flutter bottom navigation bar styling problem颤动底部导航栏样式问题
【发布时间】:2020-03-05 11:36:03
【问题描述】:

这是我正在尝试做的底部导航。

如何实现这个底部导航栏?我尝试了弯曲的底部导航,但它没有给我解决方案,我尝试使用 fab 中心停靠的颤动底部导航栏,但这也没有用。

【问题讨论】:

  • 你的意思是形状和CircularNotchedRectangle的不完全一样?
  • 当首先使用curd_navigation_bar时,我只有75的高度,我不能自定义选定的索引,我不能在选定的索引后面做任何事情,这在我尝试使用的这个设计中是不可接受的弯曲的导航我要么用错了,要么不能很好地为我服务
  • 我的意思是我有 2 种情况,未选中的图标和选中的图标我应该将橙色容器应用到我不能做的选中图标,我也想看到底部导航栏后面的屏幕我试过了,但我也做不到

标签: flutter bottomnavigationview


【解决方案1】:

Flutter 对这种 UI 有一个特定的形状,称为 CircularNotchedRectangle。您可以获取该类的代码来查看形状是如何绘制的,从中构建您的自定义形状,并对其进行调整以获得您想要的样式,因为它与CircularNotchedRectangle 非常相似。我写了一个小sn-p,您可以在DartPad 中运行,以便您了解如何在BottomNavigationBar 中实现它。它可能不是完全相同的形状,但我想我已经很接近了。如果没有该曲线的参数,我不可能得到确切的形状。使用s1s2addedRadius 参数来获得您想要的结果:

import 'dart:math' as math;

import 'package:flutter/material.dart';

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

class CustomNotchedRectangle extends NotchedShape {
  const CustomNotchedRectangle();

  @override
  Path getOuterPath(Rect host, Rect guest) {
    if (guest == null || !host.overlaps(guest)) return Path()..addRect(host);

    const double s1 = 10.0;
    const double s2 = 8.0;
    const double addedRadius = 3;

    final double notchRadius = guest.width / 2.0 + addedRadius;
    final double r = notchRadius;
    final double a = -1.0 * r - s2;
    final double b = host.top - guest.center.dy;

    final double n2 = math.sqrt(b * b * r * r * (a * a + b * b - r * r));
    final double p2xA = ((a * r * r) - n2) / (a * a + b * b);
    final double p2xB = ((a * r * r) + n2) / (a * a + b * b);
    final double p2yA = math.sqrt(r * r - p2xA * p2xA);
    final double p2yB = math.sqrt(r * r - p2xB * p2xB);

    final List<Offset> p = List<Offset>(6);

    p[0] = Offset(a - s1, b);
    p[1] = Offset(a, b);
    final double cmp = b < 0 ? -1.0 : 1.0;
    p[2] =
        cmp * p2yA > cmp * p2yB ? Offset(p2xA, p2yA) : Offset(p2xB, p2yB);

    p[3] = Offset(-1.0 * p[2].dx, p[2].dy);
    p[4] = Offset(-1.0 * p[1].dx, p[1].dy);
    p[5] = Offset(-1.0 * p[0].dx, p[0].dy);

    for (int i = 0; i < p.length; i += 1) p[i] += guest.center;

    return Path()
      ..moveTo(host.left, host.top)
      ..lineTo(p[0].dx, p[0].dy)
      ..quadraticBezierTo(p[1].dx, p[1].dy, p[2].dx, p[2].dy)
      ..arcToPoint(
        p[3],
        radius: Radius.circular(notchRadius),
        clockwise: false,
      )
      ..quadraticBezierTo(p[4].dx, p[4].dy, p[5].dx, p[5].dy)
      ..lineTo(host.right, host.top)
      ..lineTo(host.right, host.bottom)
      ..lineTo(host.left, host.bottom)
      ..close();
  }
}

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 CustomNotchedRectangle(),
        child: Container(height: 50.0, color: Theme.of(context).primaryColor),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment Counter',
        child: Icon(Icons.add),
      ),
      floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
    );
  }
}

结果如下:

【讨论】:

  • 我试过了,但看曲线看起来不像照片解决我的问题
  • 好的,我明白了。请查看我编辑的答案,我认为它可能对您有用。
  • 是的,虽然我不懂数学,但确实很棒
猜你喜欢
  • 1970-01-01
  • 2020-03-07
  • 1970-01-01
  • 1970-01-01
  • 2022-11-29
  • 2019-06-19
  • 2019-12-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多