gpt4 book ai didi

flutter - flutter 如何将参数传递到其他屏幕

转载 作者:行者123 更新时间:2023-12-03 04:50:47 28 4
gpt4 key购买 nike

如何不通过按钮将参数传递给其他屏幕,类?我想更新导航栏,以便可以将其隐藏在滚动条上

Dart

import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter2/screen/login.dart';
import 'package:flutter2/network_utils/api.dart';
import 'package:flutter2/widgets/navbar.dart';
import 'package:flutter2/screen/page2.dart';
import 'package:flutter2/screen/search.dart';
import 'package:flutter2/widgets/loading.dart';
import 'package:flutter2/widgets/skeleton.dart';
import 'package:flutter2/screen/add.dart';
import 'package:http/http.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'dart:developer' as developer;
import 'package:geolocator/geolocator.dart';
//import 'package:location/location.dart';
import 'package:flutter2/screen/register.dart';
import 'package:carousel_pro/carousel_pro.dart';
import 'package:maps_launcher/maps_launcher.dart';
import 'package:flutter_web_browser/flutter_web_browser.dart';
import 'dart:developer';
import 'package:flutter/foundation.dart';
import 'dart:async';
import 'package:flutter/rendering.dart';
import '../keys.dart' as globals;

class Home extends StatefulWidget {
final String hometitle;
Home(this.hometitle);

@override
_HomeState createState() => _HomeState();


}

class _HomeState extends State<Home>{

List<dynamic> posts = [];
// List<dynamic> searchPosts = [];
List<dynamic> tmpPosts = [];
List imagesPosts = [];
List tmpImagesPosts = [];
String name;
String _urlImages;
var page = 1;
var perPage = 0;
var totalData = 0;
var totalPage = 0;
var lat;
var longt;
bool isLoading = false;
bool loading = false;
bool isSearching = false;
ScrollController scrollController = ScrollController();
bool isScrollingDown = false;

Color _iconColor = Colors.black;
//Timer timer;
@override
void initState(){
//logout();
_loadImageUrl();
_loadUserData();
_loadSkeleton();
scrollController.addListener(() {
if (scrollController.position.pixels ==
scrollController.position.maxScrollExtent) {
print('Slide to the bottom ${scrollController.position.pixels}');
setState(() {
isLoading = true;
});
_loadmore();
}
if (scrollController.position.userScrollDirection ==
ScrollDirection.reverse) {
if (!isScrollingDown) {
isScrollingDown = true;
print(isScrollingDown);
setState(() {
navBarKey.currentState.visible = isScrollingDown;

});
//_showAppbar = false;
// hideBottomBar();
}
}
if (scrollController.position.userScrollDirection ==
ScrollDirection.forward) {
if (isScrollingDown) {
isScrollingDown = false;
print(isScrollingDown);

setState(() {
navBarKey.currentState.visible = isScrollingDown;

});
//_showAppbar = true;
//showBottomBar();
}
}
});

super.initState();
//timer = Timer.periodic(Duration(seconds: 2), (Timer t) => _handleRefresh());
print(widget.hometitle);
}
...

导航栏
import 'package:flutter/material.dart';
import 'package:flutter2/screen/add.dart';
import 'package:flutter2/screen/login.dart';
import 'package:flutter2/screen/page2.dart';
import 'package:flutter2/screen/register.dart';
import 'package:flutter2/screen/home.dart';
import 'package:curved_navigation_bar/curved_navigation_bar.dart';
import 'package:flutter2/screen/search.dart';
import 'package:flutter/rendering.dart';
import 'dart:async';
import 'package:flutter/widgets.dart';
import '../keys.dart' as globals;

class NavBar extends StatefulWidget {

//const NavBar({ Key key, @required this.homettitle }): super(key: key);
const NavBar({Key navBarKey}) : super(key: navBarKey);
@override
_NavBarState createState() => _NavBarState();


}

final GlobalKey<_NavBarState> navBarKey = new GlobalKey();


class _NavBarState extends State<NavBar>{
@override
void initState() {

super.initState();
//print(navBarKey);

}
bool visible;
Timer timer;
int _selectedTabIndex = 0;
int _pagec=0;
List _pages = [
Home("Home"),
Search(),
AddPosts(),
//Text("About")
];


_changeIndex(int index) {
setState(() {
_selectedTabIndex = index;
});
}
Widget build(BuildContext context) {
return Scaffold(
key: navBarKey,
bottomNavigationBar: CurvedNavigationBar(
backgroundColor: Colors.white,
color: Colors.teal,
buttonBackgroundColor: Colors.teal,
height: 60,
animationDuration: Duration(
milliseconds: 200,
),
index: 0,
animationCurve: Curves.bounceInOut,
items: <Widget>[
Icon(Icons.home, size: 30,
color: Colors.white),
Icon(Icons.search, size: 30,
color: Colors.white),
Icon(Icons.add, size: 30,
color: Colors.white),
Icon(Icons.settings, size: 30,
color: Colors.white),
Icon(Icons.more_horiz, size: 30,
color: Colors.white),
],
onTap: (index) {
setState(() {
_pagec = index;
});
},
),
body: _pages[_pagec],
);
}
}

使用dart文件的完整代码进行编辑,问题是将bool变量作为globalkey或作为变量传递给NavBar.dart到Home.dart,我还创建了以下文件key.dart(我不知道是否创建正确)

keys.dart
library flutter2.globals;
import 'package:flutter2/widgets/navbar.dart';
import 'package:flutter/material.dart';

final GlobalKey navBarKey = new GlobalKey();```

最佳答案

您可以使用GlobalKey解决您的问题,它将帮助您识别和访问树中唯一控件的状态:

// 1. Create a final variable storing your GlobalKey
final GlobalKey<NavBarState> navBarKey = GlobalKey();

// 2. Then when adding the NavBar widget in the tree, add to it the created key
NavBar(
key: navBarKey,
...
)

// 3. Finally use the key to update the nav bar state on scroll
if (scrollController.position.userScrollDirection == ScrollDirection.forward) {
if (isScrollingDown) {
isScrollingDown = false;
print(isScrollingDown);
bool show = true;
setState(() {
// Here you update the nav bar state
// Make sure that in your NavBarState you have a boolean getter "visible", allowing you to change the visibility state of the nav bar
navBarKey.currentState.visible = show;
});
//_showAppbar = true;
//showBottomBar();
}
}

编辑

此编辑仅向您显示如何使用发布的代码执行先前的步骤:

1.在 keys.dart文件中

import 'package:flutter2/widgets/navbar.dart';

final GlobalKey<NavBarState> navBarKey = GlobalKey();

2.在 NavBar.dart文件中

// ..imports
// No need to import the keys.dart file here
// import '../keys.dart' as globals;

class NavBar extends StatefulWidget {
// const NavBar({ Key key, @required this.homettitle }): super(key: key);
// Never use a GlobalKey here !
const NavBar({Key key}) : super(key: key);

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

// This GlobalKey is declared in the keys.dart file, not here !
// final GlobalKey<_NavBarState> navBarKey = new GlobalKey();

// The NavBarState class MUST be public when using it with GlobalKey
// Suppress the _ at the start
class NavBarState extends State<NavBar>{
@override
void initState() {
super.initState();
// print(navBarKey);
}

// This is the visibility switch
bool _navBarVisibility;

// This must be a get/set changing the visibility of CustomNavbar
bool get visible => _navBarVisibility;
set visible(bool eneble) => setState(() => _navBarVisibility = enable);

Timer timer;
int _selectedTabIndex = 0;
int _pagec=0;
List _pages = [
Home("Home"),
Search(),
AddPosts(),
//Text("About")
];


_changeIndex(int index) {
setState(() {
_selectedTabIndex = index;
});
}

Widget build(BuildContext context) {
return Scaffold(
// The key is not used here!
// key: navBarKey,
// Switch the visibility of your nav bar here
// Maybe a better approach is to use the Visibility widget
// Or animate the disappearance of the nav bar
bottomNavigationBar: !_navBarVisibility ? null : CurvedNavigationBar(
backgroundColor: Colors.white,
color: Colors.teal,
buttonBackgroundColor: Colors.teal,
height: 60,
animationDuration: Duration(
milliseconds: 200,
),
index: 0,
animationCurve: Curves.bounceInOut,
items: <Widget>[
Icon(Icons.home, size: 30,
color: Colors.white),
Icon(Icons.search, size: 30,
color: Colors.white),
Icon(Icons.add, size: 30,
color: Colors.white),
Icon(Icons.settings, size: 30,
color: Colors.white),
Icon(Icons.more_horiz, size: 30,
color: Colors.white),
],
onTap: (index) {
setState(() {
_pagec = index;
});
},
),
body: _pages[_pagec],
);
}
}

3.在 HomePage.dart文件中(可能还有其他文件中)

// ...imports
// Import keys.dart file as follows
import 'package:flutter2/keys.dart' as globals;

class Home extends StatefulWidget {
final String hometitle;
Home(this.hometitle);

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

class _HomeState extends State<Home> {
List<dynamic> posts = [];
// List<dynamic> searchPosts = [];
List<dynamic> tmpPosts = [];
List imagesPosts = [];
List tmpImagesPosts = [];
String name;
String _urlImages;
var page = 1;
var perPage = 0;
var totalData = 0;
var totalPage = 0;
var lat;
var longt;
bool isLoading = false;
bool loading = false;
bool isSearching = false;
ScrollController scrollController = ScrollController();
bool isScrollingDown = false;

Color _iconColor = Colors.black;
// Timer timer;

@override
void initState() {
// logout();
_loadImageUrl();
_loadUserData();
_loadSkeleton();
scrollController.addListener(() {
if (scrollController.position.pixels == scrollController.position.maxScrollExtent) {
print('Slide to the bottom ${scrollController.position.pixels}');
setState(() {
isLoading = true;
});
_loadmore();
}
if (scrollController.position.userScrollDirection == ScrollDirection.reverse) {
if (!isScrollingDown) {
isScrollingDown = true;
print(isScrollingDown);
setState(() {
// Update the visible state of the nav bar here
globals.navBarKey.currentState.visible = isScrollingDown;
});
// _showAppbar = false;
// hideBottomBar();
}
}
if (scrollController.position.userScrollDirection == ScrollDirection.forward) {
if (isScrollingDown) {
isScrollingDown = false;
print(isScrollingDown);

setState(() {
// Update the visible state of the nav bar here
globals.navBarKey.currentState.visible = isScrollingDown;
});
// _showAppbar = true;
// showBottomBar();
}
}
});

super.initState();
// timer = Timer.periodic(Duration(seconds: 2), (Timer t) => _handleRefresh());
print(widget.hometitle);
}
...

4.在所有创建 NavBar小部件的页面中

// You MUST specify the key in ANY widgets using the NavBar widget
import 'package:flutter2/keys.dart' as globals;

NavBar(
key: navBarKey,
...
)

恕我直言,我认为您用于在页面之间切换到导航栏的方式效率不高,一种更好,更清洁的方法可能是使用 Navigator 小部件。

希望这会有所帮助!

关于flutter - flutter 如何将参数传递到其他屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61388220/

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