Flutter Quick Actions更改所选的底部导航栏项目

时间:2019-10-12 18:03:52

标签: flutter quickaction android-appshortcut

我正在尝试在Flutter应用中实施主屏幕快速操作/应用快捷方式。我要实现的目标是,当用户通过快速操作启动我的应用程序时,该应用程序会更改底部导航栏中的选定标签。任何帮助表示赞赏。

main.dart:

runApp(
    MaterialApp(
      theme: Themes.appLightTheme,
      darkTheme: Themes.appDarkTheme,
      home: QuickActionsController(
        child: HomeFrame(currentIndex: 0),
      ),

我的QuickActionsController类:

import 'package:binfinder/screens/HomeFrame.dart';
import 'package:flutter/material.dart';
import 'package:quick_actions/quick_actions.dart';

class QuickActionsController extends StatefulWidget {
  final HomeFrame child;

  QuickActionsController({Key key, this.child}) : super(key: key);

  @override
  _QuickActionsControllerState createState() => _QuickActionsControllerState();
}

class _QuickActionsControllerState extends State<QuickActionsController> {
  final QuickActions quickActions = QuickActions();
  int _currentIndex = 0;

  @override
  void initState() {
    super.initState();
    _handleQuickActions();
    _setupQuickActions();
  }

  void _setupQuickActions() {
    quickActions.setShortcutItems(<ShortcutItem>[
      ShortcutItem(
        type: 'action_map',
        localizedTitle: 'Map',
      ),
    ]);
  }

  void _handleQuickActions() {
    quickActions.initialize((shortcutType) {
      if (shortcutType == 'action_map') {
        setState(() {
          _currentIndex = 1;
        });
      } else {
        setState(() {
          _currentIndex = 0;
        });
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    widget.child.currentIndex = _currentIndex;
    return widget.child;
  }
}

1 个答案:

答案 0 :(得分:1)

在下面的演示中,直接点击的应用将进入首页,在快速操作中,选择主视图将进入第二页

_handleQuickActions需要使用

Navigator.pushReplacement(
        context,
        MaterialPageRoute(
            builder: (context) => BottomNavigationBarController(
                  initialIndex: 1,
                )));

并使用初始索引来控制页面索引

class BottomNavigationBarController extends StatefulWidget {
  final int initialIndex;

  BottomNavigationBarController({
    this.initialIndex,
    Key key,
  }) : super(key: key);

  @override
  _BottomNavigationBarControllerState createState() =>
      _BottomNavigationBarControllerState();
}

完整代码

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

class QuickActionsManager extends StatefulWidget {
  final Widget child;
  QuickActionsManager({Key key, this.child}) : super(key: key);

  _QuickActionsManagerState createState() => _QuickActionsManagerState();
}

class _QuickActionsManagerState extends State<QuickActionsManager> {
  final QuickActions quickActions = QuickActions();

  @override
  void initState() {
    super.initState();
    _setupQuickActions();
    _handleQuickActions();
  }

  @override
  Widget build(BuildContext context) {
    return widget.child;
  }

  void _setupQuickActions() {
    quickActions.setShortcutItems(<ShortcutItem>[
      ShortcutItem(
          type: 'action_main',
          localizedTitle: 'Main view',
          icon: Platform.isAndroid ? 'quick_box' : 'QuickBox'),
      ShortcutItem(
          type: 'action_help',
          localizedTitle: 'Help',
          icon: Platform.isAndroid ? 'quick_heart' : 'QuickHeart')
    ]);
  }

  void _handleQuickActions() {
    quickActions.initialize((shortcutType) {
      if (shortcutType == 'action_main') {
        Navigator.pushReplacement(
            context,
            MaterialPageRoute(
                builder: (context) => BottomNavigationBarController(
                      initialIndex: 1,
                    )));
      } else if (shortcutType == 'action_help') {
        print('Show the help dialog!');
      }
    });
  }
}

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

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'QuickActions Demo',
        home: QuickActionsManager(child: BottomNavigationBarController(initialIndex: 0,)));
  }
}

class Home extends StatelessWidget {
  const Home({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(body: Center(child: Text('Home')));
  }
}

class Login extends StatelessWidget {
  const Login({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(body: Center(child: Text('Login')));
  }
}

class BottomNavigationBarController extends StatefulWidget {
  final int initialIndex;

  BottomNavigationBarController({
    this.initialIndex,
    Key key,
  }) : super(key: key);

  @override
  _BottomNavigationBarControllerState createState() =>
      _BottomNavigationBarControllerState();
}

class _BottomNavigationBarControllerState
    extends State<BottomNavigationBarController> {
  final List<Widget> pages = [
    FirstPage(
      key: PageStorageKey('Page1'),
    ),
    SecondPage(
      key: PageStorageKey('Page2'),
    ),
  ];

  final PageStorageBucket bucket = PageStorageBucket();

  int _selectedIndex = 0;

  Widget _bottomNavigationBar(int selectedIndex) => BottomNavigationBar(
        onTap: (int index) => setState(() => _selectedIndex = index),
        currentIndex: selectedIndex,
        items: const <BottomNavigationBarItem>[
          BottomNavigationBarItem(
              icon: Icon(Icons.add), title: Text('First Page')),
          BottomNavigationBarItem(
              icon: Icon(Icons.list), title: Text('Second Page')),
        ],
      );

  @override
  void initState() {
    _selectedIndex = widget.initialIndex;
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      bottomNavigationBar: _bottomNavigationBar(_selectedIndex),
      body: PageStorage(
        child: pages[_selectedIndex],
        bucket: bucket,
      ),
    );
  }
}

class FirstPage extends StatelessWidget {
  const FirstPage({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("First Screen"),
      ),
      body: ListView.builder(itemBuilder: (context, index) {
        return ListTile(
          title: Text('Lorem Ipsum'),
          subtitle: Text('$index'),
        );
      }),
    );
  }
}

class SecondPage extends StatelessWidget {
  const SecondPage({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Second Screen"),
      ),
      body: ListView.builder(itemBuilder: (context, index) {
        return ListTile(
          title: Text('Lorem Ipsum'),
          subtitle: Text('$index'),
        );
      }),
    );
  }
}

演示,进入“第二页”时模拟器有点慢

enter image description here