如何将onPressed添加到FloatingActionButton flutter中的每个弹出按钮

时间:2020-10-22 07:28:32

标签: flutter

我是一名新的flutter开发人员,我仍在学习。我试图使用多个FloatingActionButton为用户显示不同的选项,以便他可以移动到不同的页面。我们依靠选择FloatingActionButton按钮中可用的选项之一。 现在,我有一个现成的示例,它显示FloatingActionButton主按钮,如果用户按下它,它将为用户提供三个不同的选项。

我需要在三个弹出按钮上添加onPressed。因此,我可以将Navigator.push添加到使用它的用户。如果有人知道可以使用弹出按钮之一帮助我的解决方案,我该怎么做才能完成以下按钮。

import 'package:flutter/material.dart';
import 'dart:math' as math;

void main() {
  runApp(new MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home: new MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  State createState() => new MyHomePageState();
}

class MyHomePageState extends State<MyHomePage> with TickerProviderStateMixin {
  AnimationController _controller;

  static const List<IconData> icons = const [
    Icons.sms,
    Icons.mail,
    Icons.phone
  ];

  static method1() {
    print('method 1');
  }

  static method2() {
    print('method 2');
  }

  static method3() {
    print('method 3');

  }

  List<Function> methods = [method1, method2, method3];

  @override
  void initState() {
    _controller = new AnimationController(
      vsync: this,
      duration: const Duration(milliseconds: 500),
    );
  }

  Widget build(BuildContext context) {
    Color backgroundColor = Theme.of(context).cardColor;
    Color foregroundColor = Theme.of(context).accentColor;
    return new Scaffold(

      appBar: new AppBar(title: new Text('Speed Dial Example')),

      floatingActionButton: new Column(
        mainAxisSize: MainAxisSize.min,
        children: new List.generate(icons.length, (int index) {
          Widget child = new Container(
            height: 70.0,
            width: 56.0,
            alignment: FractionalOffset.topCenter,
            child: new ScaleTransition(
              scale: new CurvedAnimation(
                parent: _controller,
                curve: new Interval(0.0, 1.0 - index / icons.length / 2.0,
                    curve: Curves.easeOut),
              ),
              child: new FloatingActionButton(
                heroTag: null,
                backgroundColor: backgroundColor,
                mini: true,
                child: new Icon(icons[index], color: foregroundColor),
                onPressed: () {
                  methods[index]();
                },
              ),
            ),
          );
          return child;
        }).toList()
          ..add(
            new FloatingActionButton(
              heroTag: null,
              child: new AnimatedBuilder(
                animation: _controller,
                builder: (BuildContext context, Widget child) {
                  return new Transform(
                    transform: new Matrix4.rotationZ(
                        _controller.value * 0.5 * math.pi),
                    alignment: FractionalOffset.center,
                    child: new Icon(
                        _controller.isDismissed ? Icons.share : Icons.close),
                  );
                },
              ),
              onPressed: () {
                if (_controller.isDismissed) {
                  _controller.forward();
                } else {
                  _controller.reverse();
                }
              },
            ),

          ),
      ),
    );
  }
}


0 个答案:

没有答案