flutter feature---->quick action

reference: https://www.filledstacks.com/snippet/managing-quick-actions-in-flutter/

code

import 'dart:io';

import 'package:flutter/material.dart';
import 'package:quick_actions/quick_actions.dart';

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

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

class QuickActionScreen extends StatefulWidget {
  @override
  _QuickActionScreenState createState() => _QuickActionScreenState();
}

class _QuickActionScreenState extends State<QuickActionScreen> {
  final QuickActions quickActions = QuickActions();

  @override
  void initState() {
    super.initState();
    _initQuickActions();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Text("quick action"),
      ),
    );
  }

  void _initQuickActions() {
    quickActions.setShortcutItems(<ShortcutItem>[
      ShortcutItem(localizedTitle: 'add', type: 'add', icon: Platform.isAndroid ? 'add' : 'Add'),
      ShortcutItem(localizedTitle: 'help', type: 'help', icon: Platform.isAndroid ? 'help' : 'Love'),
    ]);
    quickActions.initialize((type) {
      if (type == 'add') {
        Navigator.push(context, MaterialPageRoute(builder: (context) => AddScreen()));
      } else {
        Navigator.push(context, MaterialPageRoute(builder: (context) => HelpScreen()));
      }
    });
  }
}

class AddScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Text("Add"),
      ),
    );
  }
}

class HelpScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Text("help"),
      ),
    );
  }
}

Android asset

Android recommends a 24x24 image that you can customise. Export that to all the size buckets and add them in the res/drawable-[dpi] folders. Here are the assets that I used. Copy then into the drawable-mdpi, drawable-hdpi, drawable-xhdpi, etc folders to use them.

iOS

iOS recommends using the icons under home screen quick actions. I did not create custom icons for iOS for the tutorials.


猜你喜欢

转载自www.cnblogs.com/huhx/p/13406634.html