Flutter 导航器将所选收音机或文本的值传递到颤振中的下一页

Flutter 导航器将所选收音机或文本的值传递到颤振中的下一页,flutter,radio,navigator,Flutter,Radio,Navigator,我想在下一页显示广播结果,但我不知道如何将广播数据或文本字段中的单词传递到下一页 我想要的是,如果我选择客厅,那么下一页将显示“位置是客厅”。如果我选择了其他人并在最后一个选项中键入“game room”,那么下一页将显示“Location is game room” 如何使用导航器和收音机进行此操作 import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends S

我想在下一页显示广播结果,但我不知道如何将广播数据或文本字段中的单词传递到下一页

我想要的是,如果我选择客厅,那么下一页将显示“位置是客厅”。如果我选择了其他人并在最后一个选项中键入“game room”,那么下一页将显示“Location is game room”

如何使用导航器和收音机进行此操作

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'));
  }
}
导入“包装:颤振/材料.省道”;
void main()=>runApp(MyApp());
类MyApp扩展了无状态小部件{
//此小部件是应用程序的根。
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“选择一个位置”,
主题:主题数据(
主样本:颜色。蓝色,
),
initialRoute:“/”,
路线:{
“/”:(上下文)=>SelectalLocation(),
“/nextpage”:(上下文)=>nextpage(),
},
);
}
}
类SelectalLocation扩展StatefulWidget{
@凌驾
_SelectALocationState createState()=>\u SelectALocationState();
}
类_SelectALocationState扩展状态{
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(),
正文:SingleChildScrollView(
子:列(
儿童:[
文本(“选择位置”),
选择LocationWidget(),
升起的按钮(
子项:文本('Next'),
已按下:(){
pushNamed(上下文“/nextpage”,参数:“Location”);
},
)
],
)),
);
}
}
枚举位置{客厅、卧室、厨房等}
类SelectLocationWidget扩展StatefulWidget{
SelectLocationWidget({Key}):super(Key:Key);
@凌驾
_SelectLocationWidgetState createState()=>\u SelectLocationWidgetState();
}
类_SelectLocationWidgetState扩展状态{
地点_地点=地点.客厅;
最终文本编辑控制器位置其他控制器=
新建TextEditingController();
@凌驾
小部件构建(构建上下文){
返回列(
儿童:[
放射科医师(
标题:施工文本(“客厅”),
价值:位置、客厅、,
groupValue:\u位置,
onChanged:(位置值){
设置状态(){
_位置=价值;
});
},
),
放射科医师(
标题:常量文本(“卧室”),
价值:位置。卧室,
groupValue:\u位置,
onChanged:(位置值){
设置状态(){
_位置=价值;
});
},
),
放射科医师(
标题:常量文本(“厨房”),
价值:地点、厨房、,
groupValue:\u位置,
onChanged:(位置值){
设置状态(){
_位置=价值;
});
},
),
放射科医师(
标题:文本字段(
控制器:\位置其他控制器,
装饰:输入装饰(
hintText:“其他人”,
),
),
价值:位置。其他,
groupValue:\u位置,
onChanged:(位置值){
设置状态(){
_位置=价值;
});
},
),
],
);
}
}
类NextPage扩展StatefulWidget{
@凌驾
_NextPageState createState()=>NextPageState();
}
类_下一个妊娠期扩展状态{
@凌驾
小部件构建(构建上下文){
返回容器(子:Text('Location is$Location');
}
}

您可以复制粘贴以下代码

我将您的
RaisedButton
小部件移动到
SelectLocationWidget
中列的末尾。因为您正在
SelectLocationWidget
中保留所选位置的信息。然后我为
NextPage
添加一个参数以传递位置。您可能需要实现另一个功能来打印
位置
。我使用默认的
descripbeenum
函数来实现这一点。但它会把Livig Room打印成livingRoom,所以你可能想要修复它

import 'package:flutter/foundation.dart';
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(),
      },
    );
  }
}

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(),
        ],
      )),
    );
  }
}

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;
            });
          },
        ),
        RaisedButton(
            child: Text('Next'),
            onPressed: () {
              Navigator.push(
                context, 
                MaterialPageRoute(
                  builder: (context) => NextPage(location: _location),
                ),
              );
            },
          )
      ],
    );
  }
}

class NextPage extends StatefulWidget {
  final Location location;

  NextPage({@required this.location});

  @override
  _NextPageState createState() => _NextPageState();
}

class _NextPageState extends State<NextPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        child: Text('Location is ${describeEnum(widget.location)}'),
      ),
    );
  }
}
导入“包:flift/foundation.dart”;
进口“包装:颤振/材料.省道”;
void main()=>runApp(MyApp());
类MyApp扩展了无状态小部件{
//此小部件是应用程序的根。
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“选择一个位置”,
主题:主题数据(
主样本:颜色。蓝色,
),
initialRoute:“/”,
路线:{
“/”:(上下文)=>SelectalLocation(),
},
);
}
}
类SelectalLocation扩展StatefulWidget{
@凌驾
_SelectALocationState createState()=>\u SelectALocationState();
}
类_SelectALocationState扩展状态{
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(),
正文:SingleChildScrollView(
子:列(
儿童:[
文本(“选择位置”),
选择LocationWidget(),
],
)),
);
}
}
枚举位置{客厅、卧室、厨房等}
类SelectLocationWidget扩展StatefulWidget{
SelectLocationWidget({Key}):super(Key:Key);
@凌驾
_SelectLocationWidgetState createState()=>\u SelectLocationWidgetState();
}
类_SelectLocationWidgetState扩展状态{
地点_地点=地点.客厅;
最终文本编辑控制器位置其他控制器=
新建TextEditingController();
@凌驾
小部件构建(构建上下文){
返回列(
儿童:[
放射科医师(
标题:施工文本(“客厅”),
价值:位置、客厅、,
groupValue:\u位置,
onChanged:(位置值){
设置状态(){
_位置=价值;
});
},
),
放射科医师(
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(),
      },
    );
  }
}

enum Location {livingRoom, bedroom, kitchen, others }

class SelectALocation extends StatefulWidget {
  @override
  _SelectALocationState createState() => _SelectALocationState();
}

class _SelectALocationState extends State<SelectALocation> {
  Location location = Location.livingRoom;
  String actualLocation = 'Living Room';
  final TextEditingController locationOthersController = new TextEditingController();

  Widget selectLocationWidget() {
    return Column(
      children: <Widget>[
        RadioListTile<Location>(
          title: const Text('Living Room'),
          value: Location.livingRoom,
          groupValue: location,
          onChanged: (Location value) {
            setState(() {
              location = value;
              actualLocation = 'Living Room';
            });
          },
        ),
        RadioListTile<Location>(
          title: const Text('Bedroom'),
          value: Location.bedroom,
          groupValue: location,
          onChanged: (Location value) {
            setState(() {
              location = value;
              actualLocation = 'Bedroom';
            });
          },
        ),
        RadioListTile<Location>(
          title: const Text('Kitchen'),
          value: Location.kitchen,
          groupValue: location,
          onChanged: (Location value) {
            setState(() {
              location = value;
              actualLocation = 'Kitchen';
            });
          },
        ),
        RadioListTile<Location>(
          title: TextField(
            controller: locationOthersController,
            decoration: InputDecoration(
              hintText: 'Others',
            ),
          ),
          value: Location.others,
          groupValue: location,
          onChanged: (Location value) {
            setState(() {
              location = value;
              actualLocation = locationOthersController.text;
            });
          },
        ),
      ],
    );
  }

  @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.push(
                  context, 
                  MaterialPageRoute(
                    builder: (context) => NextPage(
                      locationText: actualLocation,
                    ),
                  )
                );
              },
            )
          ],
        ),
      ),
    );
  }
}

class NextPage extends StatefulWidget {
  final String locationText;
  NextPage({@required this.locationText});

  @override
  _NextPageState createState() => _NextPageState();
}

class _NextPageState extends State<NextPage> {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Text('Location is ${widget.locationText}')
    );
  }
}