我在我的应用程序中使用Flutter SearchDelegate,下面是代码:
class NameSearch extends SearchDelegate<String> {
@override
List<Widget> buildActions(BuildContext context) {
return [
IconButton(
icon: Icon(Icons.clear),
onPressed: () {
query = "";
},
)
];
}
@override
Widget buildLeading(BuildContext context) {
return IconButton(
icon: AnimatedIcon(
icon: AnimatedIcons.menu_arrow, progress: transitionAnimation),
onPressed: () {
close(context, null);
},
);
}
@override
Widget buildResults(BuildContext context) {
return null;
}
@override
Widget buildSuggestions(BuildContext context) {
suggestionList = query.isEmpty ? [] : List.generate(nameList.length,
(i) => nameList[i]).where((p) => p.name.startsWith(query)).toList();
return ListView.builder(
itemBuilder: (context, index) => ListTile( …Run Code Online (Sandbox Code Playgroud) 启动Flutter应用程序时出现以下错误:
??? EXCEPTION CAUGHT BY FLUTTER FRAMEWORK ??????????????????????????????????????????????????????????
The following message was thrown:
Could not navigate to initial route.
The requested route name was: "/animals/cats/lolcats"
The following routes were therefore attempted:
* /
* /animals
* /animals/cats
* /animals/cats/lolcats
This resulted in the following objects:
* MaterialPageRoute<dynamic>("/", animation: null)
* MaterialPageRoute<dynamic>("/animals", animation: null)
* null
* MaterialPageRoute<dynamic>("/animals/cats/lolcats", animation: null)
One or more of those objects was null, and therefore the initial route specified will be ignored and
"/" will be …Run Code Online (Sandbox Code Playgroud) 我正在尝试在 Flutter 中设置一个多页面应用程序,其中底部选项卡包含四个最常用的页面,包括一个“更多”按钮。更多按钮显示包含其他页面链接的页面。当点击某个页面时,我希望这个页面取代“更多”页面,因此创建了一个新的导航堆栈。但是,当切换到另一个选项卡然后返回“更多”选项卡时,导航状态会被记住。这意味着我没有看到“更多”页面,而是看到切换选项卡时离开的页面。我想每次用户单击“更多”选项卡时显示“更多”页面。
我尝试过使用导航器的pushReplacement方法,因此当用户单击更多页面时,他无法导航回更多页面列表。但是,当切换选项卡时,更多页面现在永远不会显示,因为它已被替换。
我还尝试调整选项卡回调方法,以弹出所有视图并将导航器返回到更多屏幕。然而,这给我在控制台中留下了一个错误:setState() or markNeedsBuild() called during build.。
选项卡屏幕:
class TabScreen extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return TabScreenState();
}
}
class TabScreenState extends State<TabScreen> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: WillPopScope(
// Prevent swipe popping of this page. Use explicit exit buttons only.
onWillPop: () => Future<bool>.value(true),
child: CupertinoTabScaffold(
tabBar: CupertinoTabBar(
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.home),
title: Text('Artikelen'),
),
BottomNavigationBarItem(
icon: Icon(Icons.message),
title: Text('Activiteiten'),
),
BottomNavigationBarItem(
icon: Icon(Icons.speaker_group),
title: Text('Training'), …Run Code Online (Sandbox Code Playgroud) 我Navigator.pushReplacementNamed在 flutter 应用程序中使用从登录页面导航到主页,效果很好,但在我的主页中,appBar 上显示后退箭头按钮,按下它时它会返回到登录页面。我应该怎么办?我尝试 put leading: Text(''),但是当按下物理后退按钮时,它仍然返回到登录页面。
我有一个注销按钮,我希望用户仅通过此按钮注销,而不是通过后退按钮注销
*这是我的第一个问题,抱歉我的英语不好或有任何错误
我目前正在尝试使用新的 Flutter web beta 构建一个 web 应用程序。问题是能够拥有历史记录,处理浏览器中的前进和后退按钮,并能够处理用户输入到需要新 Navigator 2.0 API 的 URL(至少从我的理解)。
目前只有少数可用资源,我正在尝试基于这些资源构建我的导航器。我使用的资源:
我设法让后退和前进按钮,以及历史工作。但是,我很难处理页面切换(在 Navigator() 中)。在 John 的示例中,他管理 Navigator 小部件的 'page:' 数组中的不同站点(在 routeDelegater 中)。这对我来说似乎很奇怪,但我像这样尝试过,但它并没有真正起作用(代码进一步向下)。
在“页面:[]”中,我首先尝试让布尔值触发(如 show404),但这不是很干净,所以我的下一次尝试是按如下方式获取当前页面(在数组中):
if(_currentPage.name == 'pages[0]') pageBuilds[0]
Run Code Online (Sandbox Code Playgroud)
这有点奏效,因为我可以输入段 /matchit/0 并加载正确的页面,但是由于某种原因,“/”路由不再起作用,我收到了错误
Navigator.onGenerateRoute was null, but the route named "/" was referenced
Run Code Online (Sandbox Code Playgroud)
然后我尝试使用 'ongenerateRoute' 但这只是抛出了一堆错误。我有点新,所以也许我在那里做错了什么。但在我看来这不是正确的方法。这就是我目前陷入困境的地方。我不知道接下来该尝试什么,希望你们中的一些人可以帮助我。
我的 Route-Delegater 如下所示(我将在代码中包含我的注释,也许它可以帮助你们中的一些人,他们也希望了解 Navigator 2.0,了解正在发生的事情):
/**
* The RouteDelegate defines application specific behavious of how the router
* …Run Code Online (Sandbox Code Playgroud) navigator flutter flutter-navigation flutter-web flutter-routes
颤动,导航器
我想从新屏幕返回数据
使用旧的 Navigator 非常简单,在基本屏幕上使用 Navigator.push('new screen').then('process result') ,并在新屏幕上使用 Navigator.pop(result) 。 https://flutter.dev/docs/cookbook/navigation/returning-data
如何使用新的 Navigator 2.0 做到这一点?
This error keeps coming in my development
Debug Console
[ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: 'package:get_it/get_it_impl.dart': Failed assertion: line 312 pos 7: 'instanceFactory != null': Object/factory with type NavigationService is not registered inside GetIt.
E/flutter (27190): (Did you accidentally do GetIt sl=GetIt.instance(); instead of GetIt sl=GetIt.instance;
E/flutter (27190): Did you forget to register it?)
Run Code Online (Sandbox Code Playgroud)
This is the navigator service file in which the navigation service as well as getit is initialised
navigation_service.dart
GetIt locator = GetIt.instance;
setupLocator() {
locator.registerLazySingleton(() => NavigationService());
}
class …Run Code Online (Sandbox Code Playgroud) 我正在使用 Navigation 2 并setUrlStrategy(PathUrlStrategy());
添加了它WillPopScope,但onWillPop单击浏览器的后退按钮时不会调用 。
Widget build(context) {
return WillPopScope(
onWillPop: () async {
print('here');
if (currentStep == 0) {
return false;
}
return true;
},
child: Scaffold(...
Run Code Online (Sandbox Code Playgroud) 我正在使用带有页面绑定的 getx 导航 - 例如:
GetPage(
name: Routes.pageone,
page: () => PageOne(),
binding: PageOneBinding(),
),
GetPage(
name: Routes.pagetwo,
page: () => PageTwo(),
binding: PageTwoBinding(),
),
GetPage(
name: Routes.pagethree,
page: () => PageThree(),
binding: PageThreeBinding(),
),
Run Code Online (Sandbox Code Playgroud)
每个页面都有自己的控制器和绑定。
通常,为了导航到其他页面,我会
Get.toNamed(Routes.pageone)像这样使用路由。
但是,当我使用选项卡栏/底部导航栏时,我想保留脚手架的应用程序栏和底部导航栏,而只需使用 getx 切换内容。我能够做的唯一方法是为所有页面提供脚手架,并在每次单击每个选项卡时重新加载所有内容,这是低效的,并且每次切换到页面时都会加载不必要的小部件(应用程序栏、标题、底部导航栏等) .)。
加载选项卡内容的常用方法如下
List<Widget> _widgetOptions = <Widget>[
PageOne(),
PageTwo(),
PageThree(),
];
Run Code Online (Sandbox Code Playgroud)
不使用 getx GetPage() 进行绑定,并且会加载所有页面的所有控制器。我找不到使用 GetPage() 和底部导航栏的正确参考。
例如,加载应用程序时我的导航堆栈将/main/pageone在每次切换选项卡时将每个页面堆叠到我的堆栈中。(/main/pageone单击 tab3-> /main/pageone/pagethree)当我按下每个选项卡时,该选项卡的控制器将被加载,并且先前的控制器将被删除。
我不确定应该将什么放入body脚手架中,该脚手架会接收小部件,但使用带有绑定的 GetPage 让我感到困惑。
这是我的应用程序的页面文件代码
class MyPage extends GetView<MyPageController> {
@override …Run Code Online (Sandbox Code Playgroud) flutter flutter-navigation flutter-bottomnavigation flutter-getx
我有一个屏幕(A),它使用“AccountFetched”状态从数据库加载数据列表:
return BlocProvider<AccountBloc>(
create: (context) {
return _accountBloc..add(FetchAccountEvent());
},
child: BlocBuilder<AccountBloc, AccountState>(
builder: (context, state) {
if (state is AccountFetched) {
accounts = state.accounts;
}
Run Code Online (Sandbox Code Playgroud)
在我的第二个屏幕 (B) 中,我调用 AddAccountEvent 将数据添加到数据库并导航回父屏幕屏幕 (A)。
onPressed: () {
BlocProvider.of<AccountBloc>(context)
..add(AddAccountEvent(account: account));
Navigator.of(context).pop();
}
Run Code Online (Sandbox Code Playgroud)
但是当我导航回屏幕 A 时,数据列表并未更新。我应该手动刷新屏幕(A)还是如何更新块的状态?
这是我的集体课程:
class AccountBloc extends Bloc<AccountEvent, AccountState> {
AccountBloc() : super(AccountInitial());
@override
Stream<AccountState> mapEventToState(AccountEvent event) async* {
if (event is FetchAccountEvent) yield* _fetchAccountEvent(event, state);
if (event is AddAccountEvent) yield* _addAccountEvent(event, state);
}
Stream<AccountState> _fetchAccountEvent(
FetchAccountEvent event, AccountState state) async* …Run Code Online (Sandbox Code Playgroud) flutter ×9
dart ×3
flutter-web ×2
back-button ×1
bloc ×1
browser ×1
flutter-bloc ×1
flutter-getx ×1
navigator ×1