在 flutter 中使用 singlechildscrollview 文本框的视口未正确显示
Viewport for the textbox is not showing properly using singlechildscrollview in flutter
大家好,所有可以帮助我计算代码的人,
import 'package:flutter/material.dart';
import 'package:survey/widgets/widgetlist.dart';
import 'dart:ui' as ui;
class Item {
Item(this.name);
String name;
}
class createnewsurv extends StatefulWidget {
@override
_createnewsurvState createState() => _createnewsurvState();
}
class _createnewsurvState extends State<createnewsurv> {
TextEditingController _textcontroller = new TextEditingController();
int surveyquestionnum = 1;
Item selectedUser;
List<Item> users = <Item>[
Item('Sample 1'),
Item('Sample 2'),
Item('Sample 3'),
Item('Sample 4'),
];
List<Item> users2 = <Item>[
Item('Sample EMOJI 1'),
Item('Sample EMOJI 2'),
Item('Sample EMOJI 3'),
Item('Sample EMOJI 4'),
];
@override
Widget _dropdownbutton (List<Item> userlist){
return Container(
padding: EdgeInsets.all(1),
width: MediaQuery.of(context).size.width,
decoration: BoxDecoration(
border: Border.all(),
borderRadius: BorderRadius.all(
Radius.circular(15.0)
),
),
child: DropdownButton<Item>(
underline: SizedBox(),
isExpanded: true,
icon: Icon(Icons.arrow_drop_down),
hint: Text(" SELECT FROM DROPDOWN"),
value: selectedUser,
onChanged: (Item Value) {
setState(() {
selectedUser = Value;
});
},
items: userlist.map((Item user) {
return DropdownMenuItem<Item>(
value: user,
child: Row(
children: <Widget>[
SizedBox(width: 10,),
Text(
user.name,
style: TextStyle(color: Colors.black),
),
],
),
);
}).toList(),
),
);
}
Widget build(BuildContext context) {
final ui.Size logicalSize = MediaQuery.of(context).size;
final double _height = logicalSize.height;
return SafeArea(
child: Scaffold(
resizeToAvoidBottomInset: false,
backgroundColor: Colors.white,
body: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text("CREATE A NEW SURVEY ",style: txtttitsize),
SizedBox(height: 10),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(" SURVEY TITLE",style: txtttitsize),
),
_dropdownbutton(users),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(" SELECT EMOJI SET",style: txtttitsize),
),
_dropdownbutton(users2),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(" SURVEY QUESTION #$surveyquestionnum",style: txtttitsize),
),
TextFormField(
autocorrect: true,
controller: _textcontroller,
maxLines: 10,
onSaved: (value){
},
validator: (val){
if(val.isEmpty){
return "This page Cannot be Blank!";
}else{
return null;
}
},
decoration: InputDecoration(
labelText: "QUESTION",
fillColor: Colors.white,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(25)
)
),
)
]
),
),
)),
);
}
}
这是一个不完整的代码,但如果您注意到并尝试了 运行,它会显示
2 下拉小部件,由于某种原因,当我 select 它崩溃时显示“应该只有一个项目具有 [DropDownButton] 的值:错误,
下一个是 textform 字段,我希望当有人键入它时我将它包装在 singlescrollview 上,或者让视口在键入时将其屏幕聚焦到文本框,
但是它已经在第三行并且已经被键盘挡住了,
我可能是错的,但我怎样才能正确地使用 singlescrollview 呢?或者是否有其他小部件可以让用户在键入时正确查看此文本框?
更新
单击任何下拉按钮后出现此错误。
我用建议的答案更新了代码,它对我来说仍然是一样的。
您可以复制粘贴运行下面的完整代码
两个 _dropdownbutton
使用相同的 selectedUser
导致此错误
您可以使用 List<Item> selectedUser = [null, null];
来保留结果
并传递 index
以像这样运行 _dropdownbutton(users, 0)
代码片段
Widget _dropdownbutton(List<Item> userlist, int index) {
...
child: DropdownButton<Item>(
underline: SizedBox(),
isExpanded: true,
icon: Icon(Icons.arrow_drop_down),
hint: Text(" SELECT FROM DROPDOWN"),
value: selectedUser[index],
onChanged: (Item Value) {
setState(() {
selectedUser[index] = Value;
});
},
...
_dropdownbutton(users, 0),
_dropdownbutton(users2, 1),
工作演示
完整代码
import 'package:flutter/material.dart';
import 'package:flutter/material.dart';
//import 'package:survey/widgets/widgetlist.dart';
import 'dart:ui' as ui;
import 'package:flutter/services.dart';
class Item {
Item(this.name);
String name;
}
class createnewsurv extends StatefulWidget {
@override
_createnewsurvState createState() => _createnewsurvState();
}
class _createnewsurvState extends State<createnewsurv> {
TextEditingController _textcontroller = new TextEditingController();
int surveyquestionnum = 1;
List<Item> selectedUser = [null, null];
List<Item> users;
List<Item> users2;
@override
void initState() {
users = <Item>[
Item('Sample 1'),
Item('Sample 2'),
Item('Sample 3'),
Item('Sample 4'),
];
users2 = <Item>[
Item('Sample EMOJI 1'),
Item('Sample EMOJI 2'),
Item('Sample EMOJI 3'),
Item('Sample EMOJI 4'),
];
super.initState();
}
@override
Widget _dropdownbutton(List<Item> userlist, int index) {
return Container(
padding: EdgeInsets.all(1),
width: MediaQuery.of(context).size.width,
decoration: BoxDecoration(
border: Border.all(),
borderRadius: BorderRadius.all(Radius.circular(15.0)),
),
child: DropdownButton<Item>(
underline: SizedBox(),
isExpanded: true,
icon: Icon(Icons.arrow_drop_down),
hint: Text(" SELECT FROM DROPDOWN"),
value: selectedUser[index],
onChanged: (Item Value) {
setState(() {
selectedUser[index] = Value;
});
},
items: userlist.map((Item user) {
return DropdownMenuItem<Item>(
value: user,
child: Row(
children: <Widget>[
SizedBox(
width: 10,
),
Text(
user.name,
style: TextStyle(color: Colors.black),
),
],
),
);
}).toList(),
),
);
}
Widget build(BuildContext context) {
final ui.Size logicalSize = MediaQuery.of(context).size;
final double _height = logicalSize.height;
return SafeArea(
child: Scaffold(
resizeToAvoidBottomInset: false,
backgroundColor: Colors.white,
body: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
"CREATE A NEW SURVEY ",
),
SizedBox(height: 10),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
" SURVEY TITLE",
),
),
_dropdownbutton(users, 0),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
" SELECT EMOJI SET",
),
),
_dropdownbutton(users2, 1),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
" SURVEY QUESTION #$surveyquestionnum",
),
),
TextFormField(
autocorrect: true,
controller: _textcontroller,
maxLines: 10,
onSaved: (value) {},
validator: (val) {
if (val.isEmpty) {
return "This page Cannot be Blank!";
} else {
return null;
}
},
decoration: InputDecoration(
labelText: "QUESTION",
fillColor: Colors.white,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(25))),
)
]),
),
)),
);
}
}
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Expanded(child: createnewsurv()),
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}
大家好,所有可以帮助我计算代码的人,
import 'package:flutter/material.dart';
import 'package:survey/widgets/widgetlist.dart';
import 'dart:ui' as ui;
class Item {
Item(this.name);
String name;
}
class createnewsurv extends StatefulWidget {
@override
_createnewsurvState createState() => _createnewsurvState();
}
class _createnewsurvState extends State<createnewsurv> {
TextEditingController _textcontroller = new TextEditingController();
int surveyquestionnum = 1;
Item selectedUser;
List<Item> users = <Item>[
Item('Sample 1'),
Item('Sample 2'),
Item('Sample 3'),
Item('Sample 4'),
];
List<Item> users2 = <Item>[
Item('Sample EMOJI 1'),
Item('Sample EMOJI 2'),
Item('Sample EMOJI 3'),
Item('Sample EMOJI 4'),
];
@override
Widget _dropdownbutton (List<Item> userlist){
return Container(
padding: EdgeInsets.all(1),
width: MediaQuery.of(context).size.width,
decoration: BoxDecoration(
border: Border.all(),
borderRadius: BorderRadius.all(
Radius.circular(15.0)
),
),
child: DropdownButton<Item>(
underline: SizedBox(),
isExpanded: true,
icon: Icon(Icons.arrow_drop_down),
hint: Text(" SELECT FROM DROPDOWN"),
value: selectedUser,
onChanged: (Item Value) {
setState(() {
selectedUser = Value;
});
},
items: userlist.map((Item user) {
return DropdownMenuItem<Item>(
value: user,
child: Row(
children: <Widget>[
SizedBox(width: 10,),
Text(
user.name,
style: TextStyle(color: Colors.black),
),
],
),
);
}).toList(),
),
);
}
Widget build(BuildContext context) {
final ui.Size logicalSize = MediaQuery.of(context).size;
final double _height = logicalSize.height;
return SafeArea(
child: Scaffold(
resizeToAvoidBottomInset: false,
backgroundColor: Colors.white,
body: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text("CREATE A NEW SURVEY ",style: txtttitsize),
SizedBox(height: 10),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(" SURVEY TITLE",style: txtttitsize),
),
_dropdownbutton(users),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(" SELECT EMOJI SET",style: txtttitsize),
),
_dropdownbutton(users2),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(" SURVEY QUESTION #$surveyquestionnum",style: txtttitsize),
),
TextFormField(
autocorrect: true,
controller: _textcontroller,
maxLines: 10,
onSaved: (value){
},
validator: (val){
if(val.isEmpty){
return "This page Cannot be Blank!";
}else{
return null;
}
},
decoration: InputDecoration(
labelText: "QUESTION",
fillColor: Colors.white,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(25)
)
),
)
]
),
),
)),
);
}
}
这是一个不完整的代码,但如果您注意到并尝试了 运行,它会显示 2 下拉小部件,由于某种原因,当我 select 它崩溃时显示“应该只有一个项目具有 [DropDownButton] 的值:错误,
下一个是 textform 字段,我希望当有人键入它时我将它包装在 singlescrollview 上,或者让视口在键入时将其屏幕聚焦到文本框, 但是它已经在第三行并且已经被键盘挡住了,
我可能是错的,但我怎样才能正确地使用 singlescrollview 呢?或者是否有其他小部件可以让用户在键入时正确查看此文本框?
更新
单击任何下拉按钮后出现此错误。 我用建议的答案更新了代码,它对我来说仍然是一样的。
您可以复制粘贴运行下面的完整代码
两个 _dropdownbutton
使用相同的 selectedUser
导致此错误
您可以使用 List<Item> selectedUser = [null, null];
来保留结果
并传递 index
以像这样运行 _dropdownbutton(users, 0)
代码片段
Widget _dropdownbutton(List<Item> userlist, int index) {
...
child: DropdownButton<Item>(
underline: SizedBox(),
isExpanded: true,
icon: Icon(Icons.arrow_drop_down),
hint: Text(" SELECT FROM DROPDOWN"),
value: selectedUser[index],
onChanged: (Item Value) {
setState(() {
selectedUser[index] = Value;
});
},
...
_dropdownbutton(users, 0),
_dropdownbutton(users2, 1),
工作演示
完整代码
import 'package:flutter/material.dart';
import 'package:flutter/material.dart';
//import 'package:survey/widgets/widgetlist.dart';
import 'dart:ui' as ui;
import 'package:flutter/services.dart';
class Item {
Item(this.name);
String name;
}
class createnewsurv extends StatefulWidget {
@override
_createnewsurvState createState() => _createnewsurvState();
}
class _createnewsurvState extends State<createnewsurv> {
TextEditingController _textcontroller = new TextEditingController();
int surveyquestionnum = 1;
List<Item> selectedUser = [null, null];
List<Item> users;
List<Item> users2;
@override
void initState() {
users = <Item>[
Item('Sample 1'),
Item('Sample 2'),
Item('Sample 3'),
Item('Sample 4'),
];
users2 = <Item>[
Item('Sample EMOJI 1'),
Item('Sample EMOJI 2'),
Item('Sample EMOJI 3'),
Item('Sample EMOJI 4'),
];
super.initState();
}
@override
Widget _dropdownbutton(List<Item> userlist, int index) {
return Container(
padding: EdgeInsets.all(1),
width: MediaQuery.of(context).size.width,
decoration: BoxDecoration(
border: Border.all(),
borderRadius: BorderRadius.all(Radius.circular(15.0)),
),
child: DropdownButton<Item>(
underline: SizedBox(),
isExpanded: true,
icon: Icon(Icons.arrow_drop_down),
hint: Text(" SELECT FROM DROPDOWN"),
value: selectedUser[index],
onChanged: (Item Value) {
setState(() {
selectedUser[index] = Value;
});
},
items: userlist.map((Item user) {
return DropdownMenuItem<Item>(
value: user,
child: Row(
children: <Widget>[
SizedBox(
width: 10,
),
Text(
user.name,
style: TextStyle(color: Colors.black),
),
],
),
);
}).toList(),
),
);
}
Widget build(BuildContext context) {
final ui.Size logicalSize = MediaQuery.of(context).size;
final double _height = logicalSize.height;
return SafeArea(
child: Scaffold(
resizeToAvoidBottomInset: false,
backgroundColor: Colors.white,
body: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
"CREATE A NEW SURVEY ",
),
SizedBox(height: 10),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
" SURVEY TITLE",
),
),
_dropdownbutton(users, 0),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
" SELECT EMOJI SET",
),
),
_dropdownbutton(users2, 1),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
" SURVEY QUESTION #$surveyquestionnum",
),
),
TextFormField(
autocorrect: true,
controller: _textcontroller,
maxLines: 10,
onSaved: (value) {},
validator: (val) {
if (val.isEmpty) {
return "This page Cannot be Blank!";
} else {
return null;
}
},
decoration: InputDecoration(
labelText: "QUESTION",
fillColor: Colors.white,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(25))),
)
]),
),
)),
);
}
}
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Expanded(child: createnewsurv()),
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}