我正在构建一个包含三个页面的入职功能:
- 页面0包含
PageView Widget
,并使用widgets.add()
方法加载页面1和2 - 第1页是欢迎屏幕
- 第二页包含两个下拉按钮,应在
onChanged
之后将其值传递到第0页
第0页:
class Page0 extends StatefulWidget {
@override
State<StatefulWidget> createState() => new _Page0State();
}
class _Page0State extends State<Page0> {
late UserPreferencesArguments? args;
int currentPage = 0;
static const _kDuration = const Duration(milliseconds: 500);
static const _kCurve = Curves.ease;
PageController pageController = PageController();
final globalScaffoldKey = GlobalKey<ScaffoldState>();
List<Widget> widgets = [];
@override
void initState() {
super.initState();
init();
}
init() async {
widgets.add(Page1());
widgets.add(Page2());
}
@override
void dispose() {
//pageController?.dispose();
super.dispose();
}
@override
void setState(fn) {
if (mounted) super.setState(fn);
}
@override
Widget build(BuildContext context) {
args = ModalRoute.of(context)?.settings.arguments as UserPreferencesArguments?;
return SafeArea(
child: Scaffold(
key: globalScaffoldKey,
body: Stack(
children: [
Container(
child: PageView(
scrollDirection: Axis.horizontal,
controller: pageController,
children: widgets,
onPageChanged: (i) {
currentPage = i;
setState(() {});
},
),
),
Positioned(
child: Container(
child: ElevatedButton(
style: ElevatedButton.styleFrom(
backgroundColor: Colors.black,
shadowColor: Colors.grey,
),
onPressed: () {
if (currentPage == 1) {
updateAttributes(args?.aaa, args?.bbb);
Navigator.pushReplacementNamed(context, '/homepage');
} else {
pageController.nextPage(duration: _kDuration, curve: _kCurve);
}
},
child: Text(currentPage == 1 ? 'Finish' : "Next",),
),
),
),
],
)
),
);
}
updateAttributes(aaa, bbb) async {
debugPrint('devid: ${aaa}');
debugPrint('devid: ${bbb}');
}
}
第二页:
class Page2 extends StatefulWidget {
@override
State<StatefulWidget> createState() => new _Page2State();
}
class _Page2State extends State<Page2> {
String aaaImportValue = "";
String? _valAAA;
List _aaaList = ["1", "2" ];
String bbbImportValue = "";
String? _valBBB;
List _bbbList = ["3", "4" ];
@override
void initState() {
super.initState();
init();
}
init() async {}
@override
Widget build(BuildContext context) {
return Column(
children: [
Container(
child: DropdownButton<String>(
hint: Text("Select").tr(),
value: _valAAA,
items: _aaaList.map((value) {
return DropdownMenuItem<String>(
child: Text(value),
value: value,
);
}).toList(),
onChanged: (String? value) {
setState(() {
_valAAA = value!;
});
},
),
),
Container(
child: DropdownButton<String>(
hint: Text("Select").tr(),
value: _valBBB,
items: _bbbList.map((value) {
return DropdownMenuItem<String>(
child: Text(value),
value: value,
);
}).toList(),
onChanged: (String? value) {
setState(() {
_valBBB = value!;
});
},
),
),
],
);
}
}
我试着以这种方式传递下拉列表值,但这只是导航到Page0:
onChanged: (String? value) {
setState(() {
_valBBB = value!;
if(_valBBB != null){
Navigator.pushNamed(context, "/Page0",
arguments: UserPreferencesArguments(_valAAA, _valBBB));
}
});
},
1条答案
按热度按时间czq61nw11#
您可以在Page2上使用回调方法。
现在值已更改,请调用
而且你会得到价值
测试片段