【问题标题】:Flutter, Multiple Floataing Action Button (notched)Flutter,多个浮动操作按钮(缺口)
【发布时间】:2020-07-03 22:39:26
【问题描述】:

我可以将第二个浮动操作按钮添加为缺口吗?我制作了一张图片来表达我想要的东西。

PS:我尝试了一些方法(包装堆栈或行小部件等,但结果是;

我也需要底栏按钮,我发现浮动按钮总是覆盖它。

我当前的代码是:

import 'package:flutter/cupertino.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Welcome to Flutter',
      home: Scaffold(
        appBar: AppBar(
          title: Text('Welcome to Flutter'),
        ),
        body: Center(
          child: Text('Hello World'),
        ),

      bottomNavigationBar: Container(
        height: 50,
        child: BottomAppBar(
          color: Colors.black,
          shape: CircularNotchedRectangle(),
          clipBehavior: Clip.antiAlias,
          child: RaisedButton(onPressed: () {},
            child: new Text('Details',
              style: TextStyle(fontSize: 16.0),
            ),
          ),
        ),
      ),
      floatingActionButtonLocation: FloatingActionButtonLocation.endDocked,
      floatingActionButton: new FloatingActionButton(
        child: Icon(Icons.home,
          ),
        onPressed: () {},
      ),
      )
    );
  }
}

【问题讨论】:

    标签: flutter flutter-layout floating-action-button


    【解决方案1】:

    这是一种方法。我已经录制了CircularNotchedRectangle() 类,将其复制并粘贴到一个新类中。我称它为DoubleCircularNotchedButton()。我已经将它修改为 2 个切口和硬编码位置,因此这些切口将始终保留在一个位置。这是代码(只需复制粘贴即可)

    import 'dart:math' as math;
    import 'package:flutter/material.dart';
    
    class DoubleCircularNotchedButton extends NotchedShape {
      const DoubleCircularNotchedButton();
      @override
      Path getOuterPath(Rect host, Rect guest) {
        if (guest == null || !host.overlaps(guest)) return Path()..addRect(host);
    
        final double notchRadius = guest.height / 2.0;
    
        const double s1 = 15.0;
        const double s2 = 1.0;
    
        final double r = notchRadius;
        final double a = -1.0 * r - s2;
        final double b = host.top - 0;
    
        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);
    
        ///Cut-out 1
        final List<Offset> px = List<Offset>(6);
    
        px[0] = Offset(a - s1, b);
        px[1] = Offset(a, b);
        final double cmpx = b < 0 ? -1.0 : 1.0;
        px[2] = cmpx * p2yA > cmpx * p2yB ? Offset(p2xA, p2yA) : Offset(p2xB, p2yB);
    
        px[3] = Offset(-1.0 * px[2].dx, px[2].dy);
        px[4] = Offset(-1.0 * px[1].dx, px[1].dy);
        px[5] = Offset(-1.0 * px[0].dx, px[0].dy);
    
        for (int i = 0; i < px.length; i += 1)
          px[i] += Offset(0 + (notchRadius + 12), 0); //Cut-out 1 positions
    
        ///Cut-out 2
        final List<Offset> py = List<Offset>(6);
    
        py[0] = Offset(a - s1, b);
        py[1] = Offset(a, b);
        final double cmpy = b < 0 ? -1.0 : 1.0;
        py[2] = cmpy * p2yA > cmpy * p2yB ? Offset(p2xA, p2yA) : Offset(p2xB, p2yB);
    
        py[3] = Offset(-1.0 * py[2].dx, py[2].dy);
        py[4] = Offset(-1.0 * py[1].dx, py[1].dy);
        py[5] = Offset(-1.0 * py[0].dx, py[0].dy);
    
        for (int i = 0; i < py.length; i += 1)
          py[i] += Offset(host.width - (notchRadius + 12), 0); //Cut-out 2 positions
    
        return Path()
          ..moveTo(host.left, host.top)
          ..lineTo(px[0].dx, px[0].dy)
          ..quadraticBezierTo(px[1].dx, px[1].dy, px[2].dx, px[2].dy)
          ..arcToPoint(
            px[3],
            radius: Radius.circular(notchRadius),
            clockwise: false,
          )
          ..quadraticBezierTo(px[4].dx, px[4].dy, px[5].dx, px[5].dy)
          ..lineTo(py[0].dx, py[0].dy)
          ..quadraticBezierTo(py[1].dx, py[1].dy, py[2].dx, py[2].dy)
          ..arcToPoint(
            py[3],
            radius: Radius.circular(notchRadius),
            clockwise: false,
          )
          ..quadraticBezierTo(py[4].dx, py[4].dy, py[5].dx, py[5].dy)
          ..lineTo(host.right, host.top)
          ..lineTo(host.right, host.bottom)
          ..lineTo(host.left, host.bottom)
          ..close();
      }
    }
    

    之后,您可以像调用CircularNotchedRectangle() 一样在底部应用栏中调用它。连续添加2个FloatingActionButtons就完成了

    Scaffold(
          appBar: AppBar(
            title: Text('Welcome to Flutter'),
          ),
          body: Container(),
          bottomNavigationBar: Container(
            height: 50,
            child: BottomAppBar(
              color: Colors.black,
              shape: DoubleCircularNotchedButton(), //changed to new class
              clipBehavior: Clip.antiAlias,
              child: RaisedButton(
                onPressed: () {},
                child: Text(
                  'Details',
                  style: TextStyle(fontSize: 16.0),
                ),
              ),
            ),
          ),
          floatingActionButtonLocation: FloatingActionButtonLocation.endDocked,
    // Added row with 2 buttons
          floatingActionButton: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: <Widget>[
              Padding(
                padding: EdgeInsets.only(left: 32),
                child: FloatingActionButton(
                  child: Icon(
                    Icons.android,
                  ),
                  onPressed: () {},
                ),
              ),
              FloatingActionButton(
                child: Icon(
                  Icons.home,
                ),
                onPressed: () {},
              ),
            ],
          ),
        );
    

    输出

    【讨论】:

      猜你喜欢
      • 2020-03-25
      • 1970-01-01
      • 2019-03-06
      • 1970-01-01
      • 2020-09-23
      • 2021-09-20
      • 2021-07-15
      • 2019-08-05
      • 2019-03-06
      相关资源
      最近更新 更多