I have a settings page that sections different groups of setting inputs by grouping them into a different page in page view.
so for example there could be a 'basic settings' page where the basic settings go. Then the user can swipe to the side to access 'time settings' and swipe again to access 'advanced settings'
I want the users to be able to navigate around the pages without what they inputted getting removed and the page getting reset.
I would then like it to be reset when this happens: Navigator.pop(context);
to the pageview
CodePudding user response:
The reason your input settings is reset is when you navigate to next page, the previous page is disposed. You should store your settings above your PageView.
Here is demo code:
import 'package:flutter/material.dart';
void main() {
runApp(const MaterialApp(
title: 'Demo',
home: FirstRoute(),
));
}
class FirstRoute extends StatefulWidget {
const FirstRoute({Key? key}) : super(key: key);
@override
State<FirstRoute> createState() => _FirstRouteState();
}
class _FirstRouteState extends State<FirstRoute> {
String setting1 = "setting1";
String setting2 = "setting2";
String setting3 = "setting3";
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('First Route'),
),
body: Center(
child: Container(
color: Colors.red,
width: 200,
height: 200,
child: PageView.builder(
itemBuilder: (_, index) {
if (index == 0) {
return GestureDetector(
child: DemoWidget(settingText: setting1),
onTap: () {
setState(() {
setting1 = setting1 ' ';
});
},
);
} else if (index == 1) {
return GestureDetector(
child: DemoWidget(settingText: setting2),
onTap: () {
setState(() {
setting2 = setting2 ' ';
});
},
);
} else {
return GestureDetector(
child: DemoWidget(settingText: setting3),
onTap: () {
setState(() {
setting3 = setting3 ' ';
});
},
);
}
},
itemCount: 3,
),
),
),
);
}
}
class DemoWidget extends StatelessWidget {
const DemoWidget({
Key? key,
required this.settingText,
}) : super(key: key);
final String settingText;
@override
Widget build(BuildContext context) {
return Container(
alignment: Alignment.center,
child: Text(settingText),
);
}
}
Hope you get the idea :)