CupertinoSegmentedControl 没有起始值字段。我该如何初始化? (颤动;ios)
CupertinoSegmentedControl doesn't have a starting value field. How can I initialize? (Flutter; ios)
所以我在我的 Flutter 应用程序中使用了 CupertinoSegmentedControl 小部件,以便让用户 select 一个选项。除了在一种情况下,一切都完美无缺。如果用户 select 有一个选项,然后离开该字段,然后返回,虽然我确实将他们的 selection 保存在代码中,但它并没有以视觉方式呈现给用户。
如何做到这一点?
我的问题是这个小部件内部似乎没有初始值选项,所以我查看了其他选项,我唯一能想到的就是尝试以编程方式 select 客户选择的按钮,因为程序正在重新select该选项并调出小部件。
甚至 plausible/workable?如果可以,我该怎么办?
(现挂件如下)
return SizedBox(
width: double.infinity,
child: Padding(
padding: EdgeInsets.only(top: 8),
child: CupertinoSegmentedControl<int>(
children: choice_types,
onValueChanged: (int newValue) {
choiceType = newValue;
choice_selected = true;
}
)
)
);
有什么建议吗?
如果您希望 CupertinoSegmentedControl
小部件使用起始值进行初始化,那么您可以按照以下示例使用 CupertinoSegmentedControl
小部件的 groupValue
参数:
import 'package:flutter/cupertino.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return CupertinoApp(
home: HomePage(),
);
}
}
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
int _choiceType;
final _choiceTypes = <int, Widget>{
1: Text('1'),
2: Text('2'),
3: Text('3'),
};
@override
Widget build(BuildContext context) {
return CupertinoPageScaffold(
child: SafeArea(
child: SizedBox(
width: double.infinity,
child: Padding(
padding: EdgeInsets.only(top: 8),
child: CupertinoSegmentedControl<int>(
children: _choiceTypes,
onValueChanged: (value) {
setState(() {
_choiceType = value;
});
},
groupValue: _choiceType,
),
),
),
),
);
}
}
文档 here and here 提供了有关如何分别使用 groupValue
参数和 onValueChanged
参数的更多信息。
如果您希望选定的段在应用程序的整个生命周期内都以状态存储,而不仅仅是针对此页面,那么您可以使用 provider 包来跟踪选定的段。
测试此示例代码并查看您的响应:
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
main() {
runApp(MaterialApp(
home: TestSegmentedControl(),
));
}
class TestSegmentedControl extends StatefulWidget {
@override
_TestSegmentedControlState createState() => _TestSegmentedControlState();
}
class _TestSegmentedControlState extends State<TestSegmentedControl> {
static const _choice_types = {
0: Text('Test 0'),
1: Text('Test 1'),
2: Text('Test 2'),
3: Text('Test 3'),
};
int _currentChoiceId = 0;
bool _choiceSelected = false;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
SizedBox(
width: double.infinity,
child: Padding(
padding: EdgeInsets.only(top: 8),
child: CupertinoSegmentedControl<int>(
children: _choice_types,
onValueChanged: (int newValue) {
_choiceSelected = true;
setState(() {
_currentChoiceId = newValue;
});
},
groupValue: _currentChoiceId,
))),
RaisedButton(
onPressed: () {
Navigator.push(
context, MaterialPageRoute(builder: (ctx) => TestPage()));
},
child: Text("Next Page"),
),
],
),
),
);
}
}
class TestPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Test Page"),
),
body: Center(
child: RaisedButton(
onPressed: () {
Navigator.pop(context);
},
child: Text("Go Back"),
),
),
);
}
}
所以我在我的 Flutter 应用程序中使用了 CupertinoSegmentedControl 小部件,以便让用户 select 一个选项。除了在一种情况下,一切都完美无缺。如果用户 select 有一个选项,然后离开该字段,然后返回,虽然我确实将他们的 selection 保存在代码中,但它并没有以视觉方式呈现给用户。
如何做到这一点?
我的问题是这个小部件内部似乎没有初始值选项,所以我查看了其他选项,我唯一能想到的就是尝试以编程方式 select 客户选择的按钮,因为程序正在重新select该选项并调出小部件。
甚至 plausible/workable?如果可以,我该怎么办?
(现挂件如下)
return SizedBox(
width: double.infinity,
child: Padding(
padding: EdgeInsets.only(top: 8),
child: CupertinoSegmentedControl<int>(
children: choice_types,
onValueChanged: (int newValue) {
choiceType = newValue;
choice_selected = true;
}
)
)
);
有什么建议吗?
如果您希望 CupertinoSegmentedControl
小部件使用起始值进行初始化,那么您可以按照以下示例使用 CupertinoSegmentedControl
小部件的 groupValue
参数:
import 'package:flutter/cupertino.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return CupertinoApp(
home: HomePage(),
);
}
}
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
int _choiceType;
final _choiceTypes = <int, Widget>{
1: Text('1'),
2: Text('2'),
3: Text('3'),
};
@override
Widget build(BuildContext context) {
return CupertinoPageScaffold(
child: SafeArea(
child: SizedBox(
width: double.infinity,
child: Padding(
padding: EdgeInsets.only(top: 8),
child: CupertinoSegmentedControl<int>(
children: _choiceTypes,
onValueChanged: (value) {
setState(() {
_choiceType = value;
});
},
groupValue: _choiceType,
),
),
),
),
);
}
}
文档 here and here 提供了有关如何分别使用 groupValue
参数和 onValueChanged
参数的更多信息。
如果您希望选定的段在应用程序的整个生命周期内都以状态存储,而不仅仅是针对此页面,那么您可以使用 provider 包来跟踪选定的段。
测试此示例代码并查看您的响应:
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
main() {
runApp(MaterialApp(
home: TestSegmentedControl(),
));
}
class TestSegmentedControl extends StatefulWidget {
@override
_TestSegmentedControlState createState() => _TestSegmentedControlState();
}
class _TestSegmentedControlState extends State<TestSegmentedControl> {
static const _choice_types = {
0: Text('Test 0'),
1: Text('Test 1'),
2: Text('Test 2'),
3: Text('Test 3'),
};
int _currentChoiceId = 0;
bool _choiceSelected = false;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
SizedBox(
width: double.infinity,
child: Padding(
padding: EdgeInsets.only(top: 8),
child: CupertinoSegmentedControl<int>(
children: _choice_types,
onValueChanged: (int newValue) {
_choiceSelected = true;
setState(() {
_currentChoiceId = newValue;
});
},
groupValue: _currentChoiceId,
))),
RaisedButton(
onPressed: () {
Navigator.push(
context, MaterialPageRoute(builder: (ctx) => TestPage()));
},
child: Text("Next Page"),
),
],
),
),
);
}
}
class TestPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Test Page"),
),
body: Center(
child: RaisedButton(
onPressed: () {
Navigator.pop(context);
},
child: Text("Go Back"),
),
),
);
}
}