gpt4 book ai didi

Flutter 快速操作更改选定的底部导航栏项目

转载 作者:行者123 更新时间:2023-12-05 03:57:46 25 4
gpt4 key购买 nike

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

主飞镖:

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;
}
}

最佳答案

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

_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'),
);
}),
);
}
}

demo,模拟器进入第二页有点慢

enter image description here

关于Flutter 快速操作更改选定的底部导航栏项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58357002/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com