我有這個:
class PageService {
static int CurrentPage = 0;
}
我有這個:
import 'package:flutter/material.dart';
import '../services/page_service.dart';
class NavigationMenu extends StatefulWidget {
const NavigationMenu({super.key});
@override
State<NavigationMenu> createState() => _NavigationMenuState();
}
class _NavigationMenuState extends State<NavigationMenu> {
@override
Widget build(BuildContext context) {
return NavigationBar(
destinations: const [
NavigationDestination(
icon: Icon(
Icons.home,
color: Colors.white,
),
label: "Home"),
NavigationDestination(icon: Icon(Icons.forum), label: "Forums"),
],
backgroundColor: const Color.fromARGB(255, 154, 15, 5),
onDestinationSelected: (int index) {
setState(() {
PageService.CurrentPage = index;
int test = PageService.CurrentPage;
print("SETTING $test");
});
},
selectedIndex: PageService.CurrentPage,
);
}
}
然后在我的主要,我有這個:
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'BeastBurst',
theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.red,
),
home: const BeastBurst(title: 'BeastBurst'),
);
}
}
class BeastBurst extends StatefulWidget {
const BeastBurst({super.key, required this.title});
// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.
// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".
final String title;
@override
State<BeastBurst> createState() => _BeastBurstState();
}
class _BeastBurstState extends State<BeastBurst> {
int _counter = 0;
bool _loggedIn = false;
List<Widget> pages = const [HomePage(), Forums()];
@override
Widget build(BuildContext context) {
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.
//
// The Flutter framework has been optimized to make rerunning build methods
// fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
return Scaffold(
body: pages[PageService.CurrentPage],
// This trailing comma makes auto-formatting nicer for build methods.
);
}
}
當我瀏覽導航選單時,我看到列印輸出如下:
SETTING 1
SETTING 0
SETTING 1
SETTING 1
CRTL S但是,除非我按下VSCode 編輯器,否則不會發生頁面切換。看起來它只讀取PageService.CurrentPage編輯器保存的值。
一旦我PageService.CurrentPage靜態化,就會發生這個問題。知道為什么會這樣嗎?我該如何解決?
uj5u.com熱心網友回復:
實際上在這里,在你的NavigationMenu類中你正在更新類的狀態而不是它的父類,你在其中使用NavigationMenu,這就是為什么父類的狀態沒有改變并且構建方法沒有被召回并且更新后的值PageService.CurrentIndex不再被參考。
在您使用過的課程中NavigationMenu:
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
bottomNavigationBar: NavigationMenu(
onDestinationSelected: (index) {
setState(() {
PageService.CurrentPage = index;
int test = PageService.CurrentPage;
print("SETTING $test");
});
},
),
body: pages[PageService.CurrentPage],
);
}
更新NavigationMenu類:
class NavigationMenu extends StatefulWidget {
final Function(int) onDestinationSelected;
const NavigationMenu({super.key, required this.onDestinationSelected});
@override
State<NavigationMenu> createState() => _NavigationMenuState();
}
class _NavigationMenuState extends State<NavigationMenu> {
@override
Widget build(BuildContext context) {
return NavigationBar(
destinations: const [
NavigationDestination(
icon: Icon(
Icons.home,
color: Colors.white,
),
label: "Home"),
NavigationDestination(icon: Icon(Icons.forum), label: "Forums"),
],
backgroundColor: const Color.fromARGB(255, 154, 15, 5),
onDestinationSelected: widget.onDestinationSelected,
selectedIndex: PageService.CurrentPage,
);
}
}
替代解決方案:
在你的NavigationMenu課堂上,改變:
onDestinationSelected: (int index) {
setState(() {
PageService.CurrentPage = index;
int test = PageService.CurrentPage;
print("SETTING $test");
});
}
至:
onDestinationSelected: (int index) {
final parentState = context.findAncestorStateOfType<_MyHomePageState>();
parentState?.setState(() {
PageService.CurrentPage = index;
int test = PageService.CurrentPage;
print("SETTING $test");
});
}
注意:更改_MyHomePageState為您正在使用NavigationMenu.
uj5u.com熱心網友回復:
您需要制作主小部件,因為StatefulWidget同時也會在此處更新 UI。我正在使用回呼方法而不是全域變數。
class NavigationMenu extends StatefulWidget {
const NavigationMenu({super.key, required this.selected});
final Function(int) selected;
//....
onDestinationSelected: (int index) {
widget.selected(index);//update parent callback
setState(() {});
},
selectedIndex: PageService.CurrentPage,
);
}
和用例
class _ExampleScreenState extends State<ExampleScreen> {
int activeIndex = 0;
@override
Widget build(BuildContext context) {
return Scaffold(
bottomNavigationBar: NavigationMenu(
selected: (p0) {
//PageService.CurrentPage=0;
activeIndex = p0;
setState(() {});
},
),
body: [Text("a"), Text("B")][activeIndex] //PageService.CurrentPage
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/535048.html
標籤:扑镖
