作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如何不通过按钮将参数传递给其他屏幕,类?我想更新导航栏,以便可以将其隐藏在滚动条上
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],
);
}
}
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();
}
}
keys.dart
文件中
import 'package:flutter2/widgets/navbar.dart';
final GlobalKey<NavBarState> navBarKey = GlobalKey();
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],
);
}
}
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);
}
...
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/
我是一名优秀的程序员,十分优秀!