Flutter 当IndexedStack的子项特定状态更改时,如何重新生成IndexedStack
Flutter 当IndexedStack的子项特定状态更改时,如何重新生成IndexedStack,flutter,Flutter,IndexedStack解决了在底部导航索引更改时保留内容的问题。但是,当IndexedStack的子级状态发生变化时,我怎么也找不到重建IndexedStack的方法。只有当应用程序重新启动时,才会反映出孩子们的状态变化 更改子项时,索引堆栈不会更新 代码如下: Widget myBody() { Widget myBodyWidgetData = SizedBox.shrink(); if (_isSelectedConnectionReady &&
IndexedStack
解决了在底部导航索引更改时保留内容的问题。但是,当IndexedStack的子级状态发生变化时,我怎么也找不到重建IndexedStack的方法。只有当应用程序重新启动时,才会反映出孩子们的状态变化
更改子项时,索引堆栈
不会更新
代码如下:
Widget myBody() {
Widget myBodyWidgetData = SizedBox.shrink();
if (_isSelectedConnectionReady &&
_selectedConnectionId is int &&
_selectedConnectionId >= 0 &&
_selectedConnection.runtimeType == Connection) {
myBodyWidgetData = IndexedStack(
index: _selectedBottomNavigationIndex,
children: _children,
);
} else if (_isSelectedConnectionReady && _selectedConnectionId < 0) {
final GlobalKey<RefreshIndicatorState> _refreshIndicatorKey =
new GlobalKey<RefreshIndicatorState>();
myBodyWidgetData = RefreshIndicator(
key: _refreshIndicatorKey,
onRefresh: setSelectedConnection,
child: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Padding(
padding: const EdgeInsets.fromLTRB(10, 20, 10, 20),
child: Text(
"Kindly add a connection to manage woocommerce",
textAlign: TextAlign.center,
),
),
Container(
height: 45,
width: 200,
child: RaisedButton(
color: Theme.of(context).primaryColor,
textColor: Colors.white,
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => AddConnectionPage(
refreshConnectionsList: setSelectedConnection,
),
),
);
},
child: Text(
"Add Connection",
style: Theme.of(context).textTheme.button,
),
),
)
],
),
),
);
} else {
myBodyWidgetData = Center(
child: SpinKitPulse(
color: Theme.of(context).primaryColor,
size: 70,
),
);
}
return myBodyWidgetData;
}
Widget myBody(){
Widget myBodyWidgetData=SizedBox.shrink();
如果(_)已选择连接就绪&&
_selectedConnectionId为int&&
_selectedConnectionId>=0&&
_selectedConnection.runtimeType==连接){
myBodyWidgetData=IndexedStack(
索引:\ u选择底部导航索引,
儿童:_儿童,
);
}如果(_isSelectedConnectionReady&&u selectedConnectionId<0){
最后一次全球杯=
新GlobalKey();
myBodyWidgetData=RefreshIndicator(
键:_refreshIndicatorKey,
onRefresh:setSelectedConnection,
子:SingleChildScrollView(
子:列(
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
填充物(
填充:LTRB(10,20,10,20)中的常数边集,
子:文本(
“请添加连接以管理woocommerce”,
textAlign:textAlign.center,
),
),
容器(
身高:45,
宽度:200,
孩子:升起按钮(
颜色:主题。背景。原色,
textColor:Colors.white,
已按下:(){
导航器。推(
上下文
材料路线(
生成器:(上下文)=>AddConnectionPage(
refreshConnectionsList:setSelectedConnection,
),
),
);
},
子:文本(
“添加连接”,
样式:Theme.of(context).textTheme.button,
),
),
)
],
),
),
);
}否则{
myBodyWidgetData=中心(
孩子:SpinKitPulse(
颜色:主题。背景。原色,
尺码:70,
),
);
}
返回myBodyWidgetData;
}
完整代码:
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:woocommerceadmin/src/common/widgets/MyDrawer.dart';
import 'package:woocommerceadmin/src/connections/widgets/AddConnectionPage.dart';
import 'package:woocommerceadmin/src/customers/widgets/CustomersListPage.dart';
import 'package:woocommerceadmin/src/db/ConnectionDBProvider.dart';
import 'package:woocommerceadmin/src/db/models/Connection.dart';
import 'package:woocommerceadmin/src/orders/widgets/OrdersListPage.dart';
import 'package:woocommerceadmin/src/products/widgets/ProductsListPage.dart';
import 'package:woocommerceadmin/src/reports/widgets/ReportsPage.dart';
import 'package:woocommerceadmin/src/config.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Woocommerce Admin',
theme: ThemeData(
primarySwatch: Colors.purple,
textTheme: TextTheme(
headline: TextStyle(
fontSize: 24.0,
fontWeight: FontWeight.bold,
// backgroundColor: Colors.purple,
// color: Colors.white
),
subhead: TextStyle(
fontSize: 18.0,
fontWeight: FontWeight.bold,
),
body1: TextStyle(
fontSize: 14.0,
),
button: TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
fontSize: 16,
)),
),
home: HomePage(),
);
}
}
class HomePage extends StatefulWidget {
HomePage();
final _HomePageState _homePageState = _HomePageState();
@override
_HomePageState createState() => _homePageState;
Future<void> setSelectedConnection() async {
await _homePageState.setSelectedConnection();
}
}
class _HomePageState extends State<HomePage> {
int _selectedConnectionId = -1;
bool _isSelectedConnectionReady = false;
Connection _selectedConnection;
int _selectedBottomNavigationIndex = 0;
List<Widget> _children = [];
@override
void initState() {
super.initState();
setSelectedConnection();
}
@override
void setState(fn) {
if (mounted) {
super.setState(fn);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: myAppBar(),
drawer: MyDrawer(),
body: myBody(),
bottomNavigationBar: myBottomNavigation(),
);
}
Future<void> setSelectedConnection() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
int selectedConnectionId;
try {
selectedConnectionId = prefs.getInt("selectedConnectionId");
} catch (e) {
selectedConnectionId = -1;
_isSelectedConnectionReady = true;
}
List<Connection> connectionList =
await ConnectionDBProvider.db.getAllConnections();
if (selectedConnectionId is int &&
selectedConnectionId >= 0 &&
connectionList.isNotEmpty) {
if (connectionList[selectedConnectionId] is Connection) {
setState(() {
_selectedConnectionId = selectedConnectionId;
_selectedConnection = connectionList[_selectedConnectionId];
_isSelectedConnectionReady = true;
_children = [
ReportsPage(
baseurl: _selectedConnection.baseurl,
username: _selectedConnection.username,
password: _selectedConnection.password,
),
OrdersListPage(
baseurl: _selectedConnection.baseurl,
username: _selectedConnection.username,
password: _selectedConnection.password,
),
ProductsListPage(
baseurl: _selectedConnection.baseurl,
username: _selectedConnection.username,
password: _selectedConnection.password,
),
CustomersListPage(
baseurl: _selectedConnection.baseurl,
username: _selectedConnection.username,
password: _selectedConnection.password,
)
];
});
}
}
}
Widget myAppBar() {
Widget myAppBarWidgetData;
if (!_isSelectedConnectionReady &&
_selectedConnectionId is int &&
_selectedConnection is! Connection) {
myAppBarWidgetData = AppBar(
title: Text("Setup"),
);
}
return myAppBarWidgetData;
}
Widget myBody() {
Widget myBodyWidgetData = SizedBox.shrink();
if (_isSelectedConnectionReady &&
_selectedConnectionId is int &&
_selectedConnectionId >= 0 &&
_selectedConnection.runtimeType == Connection) {
myBodyWidgetData = IndexedStack(
index: _selectedBottomNavigationIndex,
children: _children,
);
} else if (_isSelectedConnectionReady && _selectedConnectionId < 0) {
final GlobalKey<RefreshIndicatorState> _refreshIndicatorKey =
new GlobalKey<RefreshIndicatorState>();
myBodyWidgetData = RefreshIndicator(
key: _refreshIndicatorKey,
onRefresh: setSelectedConnection,
child: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Padding(
padding: const EdgeInsets.fromLTRB(10, 20, 10, 20),
child: Text(
"Kindly add a connection to manage woocommerce",
textAlign: TextAlign.center,
),
),
Container(
height: 45,
width: 200,
child: RaisedButton(
color: Theme.of(context).primaryColor,
textColor: Colors.white,
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => AddConnectionPage(
refreshConnectionsList: setSelectedConnection,
),
),
);
},
child: Text(
"Add Connection",
style: Theme.of(context).textTheme.button,
),
),
)
],
),
),
);
} else {
myBodyWidgetData = Center(
child: SpinKitPulse(
color: Theme.of(context).primaryColor,
size: 70,
),
);
}
return myBodyWidgetData;
}
Widget myBottomNavigation() {
Widget myBottomNavigationWidgetData = SizedBox.shrink();
if (_isSelectedConnectionReady &&
_selectedBottomNavigationIndex >= 0 &&
_selectedConnection.runtimeType == Connection) {
myBottomNavigationWidgetData = BottomNavigationBar(
// backgroundColor: Colors.purple, //Not Working Don't Know why
showSelectedLabels: true,
showUnselectedLabels: true,
unselectedItemColor: Config.colors["lightTheme"]
["bottomNavInactiveColor"],
selectedItemColor: Config.colors["lightTheme"]["mainThemeColor"],
currentIndex: _selectedBottomNavigationIndex,
onTap: (int index) {
setState(() {
_selectedBottomNavigationIndex = index;
});
},
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.insert_chart),
title: Text('Reports'),
),
BottomNavigationBarItem(
icon: Icon(Icons.assignment),
title: Text('Orders'),
),
BottomNavigationBarItem(
icon: Icon(Icons.collections),
title: Text('Products'),
),
BottomNavigationBarItem(
icon: Icon(Icons.people),
title: Text('Customers'),
),
]);
}
return myBottomNavigationWidgetData;
}
}
导入“包装:颤振/材料.省道”;
导入“package:shared_preferences/shared_preferences.dart”;
导入“package:woocmerceadmin/src/common/widgets/MyDrawer.dart”;
导入“package:woocmerceadmin/src/connections/widgets/AddConnectionPage.dart”;
导入“package:woocmerceadmin/src/customers/widgets/CustomersListPage.dart”;
导入“package:woocmerceadmin/src/db/ConnectionDBProvider.dart”;
导入“package:woocmerceadmin/src/db/models/Connection.dart”;
导入“package:woocmerceadmin/src/orders/widgets/orderslitpage.dart”;
导入“package:woocmerceadmin/src/products/widgets/ProductsListPage.dart”;
导入“包:woocmerceadmin/src/reports/widgets/ReportsPage.dart”;
导入“package:woocmerceadmin/src/config.dart”;
进口“包装:颤振旋转套件/颤振旋转套件.dart”;
void main()=>runApp(MyApp());
类MyApp扩展了无状态小部件{
//此小部件是应用程序的根。
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“Woocommerce管理员”,
主题:主题数据(
原色样本:颜色。紫色,
textTheme:textTheme(
标题:文本风格(
字体大小:24.0,
fontWeight:fontWeight.bold,
//背景颜色:Colors.purple,
//颜色:颜色。白色
),
小标题:文本样式(
字体大小:18.0,
fontWeight:fontWeight.bold,
),
正文1:文本样式(
字体大小:14.0,
),
按钮:文本样式(
颜色:颜色,白色,
fontWeight:fontWeight.bold,
尺寸:16,
)),
),
主页:主页(),
);
}
}
类主页扩展了StatefulWidget{
主页();
final _HomePageState _HomePageState=_HomePageState();
@凌驾
_HomePageState createState()=>\u HomePageState;
Future setSelectedConnection()异步{
wait_homePageState.setSelectedConnection();
}
}
类_HomePageState扩展状态{
int _selectedConnectionId=-1;
bool_isSelectedConnectionReady=false;
连接\u选择连接;
int _selectedBottomNavigationIndex=0;
列表_children=[];
@凌驾
void initState(){
super.initState();
setSelectedConnection();
}
@凌驾
无效设置状态(fn){
如果(已安装){
超级设定状态(fn);
}
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:myAppBar(),
抽屉:MyDrawer(),
body:myBody(),
bottomNavigationBar:myBottomNavigation(),
);
}
Future setSelectedConnection()异步{
SharedReferences prefs=等待SharedReferences.getInstance();
int-selectedConnectionId;
试一试{
selectedConnectionId=prefs.getInt(“selectedConnectionId”);
}捕获(e){
selectedConnectionId=-1;
_isSelectedConnectionReady=true;
}
列表连接列表=
等待ConnectionDBProvider.db.getAllConnections();
如果(selectedConnectionId为int&&
selectedConnectionId>=0&&
connectionList.isNotEmpty){
如果(连接列表[selectedConnectionId]为连接){
设置状态(){
_selectedConnectionId=selectedConnectionId;
_selectedConnection=connectionList[_selectedConnectionId];
_isSelectedConnectionReady=true;
_儿童=[
报告SPAGE(
setState(() {
_children.removeAt(0);
_children.insert(0, CustomWidget(customData: customData, key: GlobalKey()));
});