Я хочу показать результат радио на следующей странице, но я не знаю, как передать данные радио или слова в текстовом поле на следующую страницу.
То, что я хочу, это что если я выберу Гостиную, то на следующей странице будет показано «Расположение - Гостиная». Если я выберу другие, а также введу «игровая комната» в последнем варианте, на следующей странице отобразится «Место - игровая комната».
Как это сделать с помощью навигатора и радио?
import 'package:flutter/material.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: 'Select A Location',
theme: ThemeData(
primarySwatch: Colors.blue,
),
initialRoute: '/',
routes: <String, WidgetBuilder>{
'/': (context) => SelectALocation(),
'/nextpage': (context) => NextPage(),
},
);
}
}
class SelectALocation extends StatefulWidget {
@override
_SelectALocationState createState() => _SelectALocationState();
}
class _SelectALocationState extends State<SelectALocation> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: SingleChildScrollView(
child: Column(
children: <Widget>[
Text('Select a location'),
SelectLocationWidget(),
RaisedButton(
child: Text('Next'),
onPressed: () {
Navigator.pushNamed(context, '/nextpage', arguments: 'Location');
},
)
],
)),
);
}
}
enum Location {livingRoom, bedroom, kitchen, others }
class SelectLocationWidget extends StatefulWidget {
SelectLocationWidget({Key key}) : super(key: key);
@override
_SelectLocationWidgetState createState() => _SelectLocationWidgetState();
}
class _SelectLocationWidgetState extends State<SelectLocationWidget> {
Location _location = Location.livingRoom;
final TextEditingController _locationOthersController =
new TextEditingController();
@override
Widget build(BuildContext context) {
return Column(
children: <Widget>[
RadioListTile<Location>(
title: const Text('Living Room'),
value: Location.livingRoom,
groupValue: _location,
onChanged: (Location value) {
setState(() {
_location = value;
});
},
),
RadioListTile<Location>(
title: const Text('Bedroom'),
value: Location.bedroom,
groupValue: _location,
onChanged: (Location value) {
setState(() {
_location = value;
});
},
),
RadioListTile<Location>(
title: const Text('Kitchen'),
value: Location.kitchen,
groupValue: _location,
onChanged: (Location value) {
setState(() {
_location = value;
});
},
),
RadioListTile<Location>(
title: TextField(
controller: _locationOthersController,
decoration: InputDecoration(
hintText: 'Others',
),
),
value: Location.others,
groupValue: _location,
onChanged: (Location value) {
setState(() {
_location = value;
});
},
),
],
);
}
}
class NextPage extends StatefulWidget {
@override
_NextPageState createState() => _NextPageState();
}
class _NextPageState extends State<NextPage> {
@override
Widget build(BuildContext context) {
return Container(child: Text('Location is $Location'));
}
}