skip to Main Content

I want to display add to wallet button in Flutter. I have written some native codes, channel and where to display this button. Now the button is visible. But can not control it. I can not put it to the exact place. For example I want to put a button after the texts. But the button still stays on the appbar and doesn’t move.

My AppDelegate file is like this :

import PassKit

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    GeneratedPluginRegistrant.register(with: self)
            let controller: FlutterViewController = window?.rootViewController as! FlutterViewController
            let channel = FlutterMethodChannel(name: "your_wallet_channel", binaryMessenger: controller.binaryMessenger)
            channel.setMethodCallHandler { (call: FlutterMethodCall, result: @escaping FlutterResult) in
                if call.method == "showAddToWalletButton" {
                    WalletMethods.showAddToWalletButton()
                } else {
                    result(FlutterMethodNotImplemented)
                }
            }
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }
}


import PassKit

class WalletMethods {
    static func showAddToWalletButton() {
        let passButton = PKAddPassButton(addPassButtonStyle: .black)

        passButton.frame = CGRect(x: 20, y: 20, width: 200, height: 44)

        passButton.addTarget(self, action: #selector(addToWalletButtonTapped), for: .touchUpInside)

        let controller = UIApplication.shared.keyWindow?.rootViewController
        controller?.view.addSubview(passButton)
    }

    @objc private func addToWalletButtonTapped() {
        
    }
}

Also I have created channel for connection between native code.

class WalletChannel {
  static const MethodChannel _channel = MethodChannel('your_wallet_channel');

  static Future<void> showAddToWalletButton() async {
    await _channel.invokeMethod('showAddToWalletButton');
  }
}

And finally my screen is like this

class _WalletScreenState extends State<WalletScreen> {
  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) {
      showAddToWalletButton();
    });
  }

  void showAddToWalletButton() {
    WalletChannel.showAddToWalletButton();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Wallet Screen')),
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Text('Welcome to Your Wallet'),
          SizedBox(height: 20),
          Text('This is your digital wallet.'),
          SizedBox(height: 20),
          Text('Manage your cards and passes.'),
          SizedBox(height: 40),
        ],
      ),
    );
  }
}

3

Answers


  1. I don’t know the library you are using, and to be honest is the first time I have seen someone using Flutter like this.

    There might be different reasons for what you are experiencing.
    I notice a few things out of the code.

    There is no reference in your build to where the button should be positioned, meaning the library creates the button and places it on the view in some default place, that as you say, is the header.
    When you call the button on the init, it immediately places it on the view.

    Look up the library reference, check how can you get the reference for the button, and place it on the build tree.

    Login or Signup to reply.
  2. It appears like you’re attempting to use native Flutter-integrated code to display a "Add to Wallet" button. Although it appears that you are off to a good start, it appears that the button location is not performing as intended.

    Login or Signup to reply.
  3. D’après ce que je vois tu as mis en place la communication entre flutter et Objectie-C pour afficher ton bouton mais tu n’arrives pas à positionner le bouton à l’endroit souhaiter.
    Dans ton code actuel tu ajoutes le bouton directement à la vue racine UIApplication.shared.keyWindow?.rootViewController?.view ce qui le place au dessus de tout le contenu, pour positionner le bouton après les textes dans ton écran tu devrais ajuster l’emplacement où tu ajoutes le bouton dans la hiérarchie des vues!!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search