Flutter 多类颤振作用域模型
我是一名新手,我想管理一个有一个文本字段的登录屏幕页面,当用户单击“下一步”时,我保存该值,并使用作用域模型状态管理将提示文本更改为同一文本字段上的下一步提示 首先,这是我的代码: loginBase.dart(同时具有“登录字段(已导入)”和“登录按钮”的主登录页面类):Flutter 多类颤振作用域模型,flutter,scoped-model,Flutter,Scoped Model,我是一名新手,我想管理一个有一个文本字段的登录屏幕页面,当用户单击“下一步”时,我保存该值,并使用作用域模型状态管理将提示文本更改为同一文本字段上的下一步提示 首先,这是我的代码: loginBase.dart(同时具有“登录字段(已导入)”和“登录按钮”的主登录页面类): 导入“包装:颤振/材料.省道”; 导入“package:font_awesome_flatter/font_awesome_flatter.dart”; 导入“package:scoped_model/scoped_mode
导入“包装:颤振/材料.省道”;
导入“package:font_awesome_flatter/font_awesome_flatter.dart”;
导入“package:scoped_model/scoped_model.dart”;
导入“login model.dart”;
//页数
导入“/LoginFields.dart”;
类LoginPage扩展StatefulWidget{
@凌驾
_LoginPagentate createState()=>_LoginPagentate();
}
类_loginpagentate扩展状态{
@凌驾
小部件构建(构建上下文){
返回范围模型(
模型:LoginModel(),
孩子:脚手架(
主体:材料(
子:容器(
颜色:颜色,白色,
子:ListView(
儿童:[
填充物(
填充:仅限常量边集(顶部:20.0,底部:20.0),
孩子:图像(
图片:AssetImage(“assets/images/landing_screen.jpg”),
),
LoginFields(),
填充物(
填充:仅限常量边设置(
左:10.0,右:10.0,顶部:100.0),
孩子:排(
儿童:[
扩大(
弹性:1,
孩子:填充(
衬垫:
仅限常数边集(左:15.0,右:15.0),
子项:buildButton(),
)),
],
),
)
],
),
),
),
),
);
}
var buildButton=()=>ScopedModelSecondant(
生成器:(上下文、子对象、模型)=>FlatButton(
已按下:(){
model.nextStep();
},
颜色:颜色,黑色,
形状:新的RoundedRectangleBorder(
边界半径:新边界半径。圆形(30.0)),
孩子:填充(
填充:常数边集全部(14.0),
子:图标(
Icons.arrow_向前,
颜色:颜色,白色,
尺寸:25.0,
)),
),
);
}
LoginFields.dart(包含该字段的字段):
导入“包装:颤振/材料.省道”;
导入“package:scoped_model/scoped_model.dart”;
导入“login model.dart”;
类LoginFields扩展StatefulWidget{
@凌驾
_LoginFieldsState createState()=>\u LoginFieldsState();
LoginFields();
}
类_loginfieldstate扩展状态{
@凌驾
小部件构建(构建上下文){
返回范围模型(
模型:LoginModel(),
子:容器(
子:列(
儿童:[
填充物(
填充:仅限常量边集(左:30.0,右:30.0),
子项:buildTextField(),
),
],
),
),
);
}
var buildTextField=()=>scopedModelSecondant(
生成器:(上下文、子对象、模型)=>TextFormField(
装饰:输入装饰(
hintText:model.hintText,
enabledBorder:UnderlineInputBorder(
边界:
边框(颜色:颜色(0xFFE4E4),宽度:2.0)),
textAlign:textAlign.center,
控制器:model.controller,
),
);
}
最后是login-model.dart(包含登录页面的作用域模型):
导入“包装:颤振/材料.省道”;
导入“package:scoped_model/scoped_model.dart”;
类LoginModel扩展了该模型{
//步骤字段选项
String hintText=“我们如何称呼您?”;
文本编辑控制器;
int STEPSOUNTER=0;
列出步骤=[
台阶(
字段:“用户名”,
控制器:新文本编辑控制器(),
提示:“我们怎么称呼你?”,
台阶(
字段:“电子邮件”,
控制器:新文本编辑控制器(),
提示:“您的电子邮件发送到此处”),
台阶(
字段:“移动”,
控制器:新文本编辑控制器(),
提示:“电话号码ex:+201234…”),
台阶(
字段:“密码”,
控制器:新文本编辑控制器(),
提示:“您的密码”),
];
void initStep(){
hintText=步骤[0]。提示;
notifyListeners();
}
void nextStep(){
if(StepSconter我做了一些逻辑上的更改,但这很有效,并添加了我认为您正在寻找的功能。问题是有一个单独的login_fields.dart类。模型没有与login_base.dart对话。我已经测试过,似乎工作得很好
更新您的登录名。\u base.dart:
import 'package:flutter/material.dart';
import 'package:scoped_model/scoped_model.dart';
import 'login_model.dart';
class LoginPage extends StatelessWidget {
TextInputType getKeyboard(StepType type) {
switch (type) {
case StepType.email:
return TextInputType.emailAddress;
break;
case StepType.phone:
return TextInputType.phone;
break;
case StepType.username:
case StepType.password:
return TextInputType.text;
break;
default:
return TextInputType.text;
}
}
@override
Widget build(BuildContext context) {
return ScopedModel<LoginModel>(
model: LoginModel(),
child: Scaffold(
body: Material(
child: Container(
color: Colors.white,
child: ListView(
children: <Widget>[
Padding(
padding: const EdgeInsets.only(top: 20.0, bottom: 20.0),
child: Image(
image: AssetImage("assets/images/landing_screen.jpg")),
),
Padding(
padding:
const EdgeInsets.only(top: 50.0, left: 30.0, right: 30.0),
child: ScopedModelDescendant<LoginModel>(
builder: (context, child, model) => TextFormField(
autofocus: true,
decoration: InputDecoration(
hintText: model.steps[model.step].hint,
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: Color(0xFFE4E4E4), width: 2.0))),
keyboardType:
getKeyboard(model.steps[model.step].type),
textAlign: TextAlign.center,
controller: model.steps[model.step].controller,
// textInputAction:
// model.steps[model.step].type == StepType.password
// ? TextInputAction.done
// : TextInputAction.next,
obscureText:
model.steps[model.step].type == StepType.password,
onEditingComplete:
model.step == model.steps.length - 1
? null
: model.nextStep,
),
),
),
Padding(
padding: const EdgeInsets.only(
left: 10.0, right: 10.0, top: 100.0),
child: Row(
children: <Widget>[
Expanded(
flex: 1,
child: Padding(
padding:
const EdgeInsets.only(left: 15.0, right: 15.0),
child: ScopedModelDescendant<LoginModel>(
builder: (context, child, model) => FlatButton(
onPressed:
model.step == model.steps.length - 1
? model.startOver
: model.nextStep,
color: Colors.black,
shape: new RoundedRectangleBorder(
borderRadius:
new BorderRadius.circular(30.0)),
child: Padding(
padding: const EdgeInsets.all(14.0),
child:
model.step == model.steps.length - 1
? Text(
'Start Over',
style: Theme.of(context)
.textTheme
.copyWith(
button: TextStyle(
color: Colors.white,
))
.button,
)
: Icon(
Icons.arrow_forward,
color: Colors.white,
size: 25.0,
)),
),
),
)),
],
),
)
],
),
),
),
),
);
}
}
导入“包装:颤振/材料.省道”;
导入“package:scoped_model/scoped_model.dart”;
导入“login_model.dart”;
类LoginPage扩展了无状态小部件{
text输入类型getKeyboard(StepType类型){
开关(类型){
case StepType.email:
返回TextInputType.emailAddress;
打破
case StepType.phone:
返回TextInputType.phone;
打破
case StepType.username:
case StepType.password:
返回TextInputType.text;
打破
违约:
返回TextInputType.text;
}
}
@凌驾
小部件构建(构建上下文){
返回范围模型(
模型:LoginModel(),
孩子:脚手架(
主体:材料(
子:容器(
颜色:颜色,白色,
子:ListView(
儿童:[
填充物(
填充:仅限常量边集(顶部:20.0,底部:20.0),
import 'package:flutter/material.dart';
import 'package:scoped_model/scoped_model.dart';
import 'login-model.dart';
class LoginFields extends StatefulWidget {
@override
_LoginFieldsState createState() => _LoginFieldsState();
LoginFields();
}
class _LoginFieldsState extends State<LoginFields> {
@override
Widget build(BuildContext context) {
return ScopedModel<LoginModel>(
model: LoginModel(),
child: Container(
child: Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.only(left: 30.0, right: 30.0),
child: buildTextField(),
),
],
),
),
);
}
var buildTextField = () => ScopedModelDescendant<LoginModel>(
builder: (context, child, model) => TextFormField(
decoration: InputDecoration(
hintText: model.hintText,
enabledBorder: UnderlineInputBorder(
borderSide:
BorderSide(color: Color(0xFFE4E4E4), width: 2.0))),
textAlign: TextAlign.center,
controller: model.controller,
),
);
}
import 'package:flutter/material.dart';
import 'package:scoped_model/scoped_model.dart';
class LoginModel extends Model {
//steps fields options
String hintText = "How can we call you?";
TextEditingController controller;
int stepsCounter = 0;
List<Step> steps = <Step>[
Step(
field: "username",
controller: new TextEditingController(),
hint: "How can we call you?"),
Step(
field: "email",
controller: new TextEditingController(),
hint: "Your Email goes here"),
Step(
field: "mobile",
controller: new TextEditingController(),
hint: "Phone number ex: +201234..."),
Step(
field: "password",
controller: new TextEditingController(),
hint: "your Password"),
];
void initStep() {
hintText = steps[0].hint;
notifyListeners();
}
void nextStep() {
if (stepsCounter <= 3) {
steps[stepsCounter].controller=controller;
print(controller);
if (stepsCounter<3) {
hintText = steps[stepsCounter + 1].hint;
}
stepsCounter++;
} else {
return;
}
notifyListeners();
}
}
class Step {
String field;
TextEditingController controller;
String hint;
Step({this.field, this.controller, this.hint});
}
import 'package:flutter/material.dart';
import 'package:scoped_model/scoped_model.dart';
import 'login_model.dart';
class LoginPage extends StatelessWidget {
TextInputType getKeyboard(StepType type) {
switch (type) {
case StepType.email:
return TextInputType.emailAddress;
break;
case StepType.phone:
return TextInputType.phone;
break;
case StepType.username:
case StepType.password:
return TextInputType.text;
break;
default:
return TextInputType.text;
}
}
@override
Widget build(BuildContext context) {
return ScopedModel<LoginModel>(
model: LoginModel(),
child: Scaffold(
body: Material(
child: Container(
color: Colors.white,
child: ListView(
children: <Widget>[
Padding(
padding: const EdgeInsets.only(top: 20.0, bottom: 20.0),
child: Image(
image: AssetImage("assets/images/landing_screen.jpg")),
),
Padding(
padding:
const EdgeInsets.only(top: 50.0, left: 30.0, right: 30.0),
child: ScopedModelDescendant<LoginModel>(
builder: (context, child, model) => TextFormField(
autofocus: true,
decoration: InputDecoration(
hintText: model.steps[model.step].hint,
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: Color(0xFFE4E4E4), width: 2.0))),
keyboardType:
getKeyboard(model.steps[model.step].type),
textAlign: TextAlign.center,
controller: model.steps[model.step].controller,
// textInputAction:
// model.steps[model.step].type == StepType.password
// ? TextInputAction.done
// : TextInputAction.next,
obscureText:
model.steps[model.step].type == StepType.password,
onEditingComplete:
model.step == model.steps.length - 1
? null
: model.nextStep,
),
),
),
Padding(
padding: const EdgeInsets.only(
left: 10.0, right: 10.0, top: 100.0),
child: Row(
children: <Widget>[
Expanded(
flex: 1,
child: Padding(
padding:
const EdgeInsets.only(left: 15.0, right: 15.0),
child: ScopedModelDescendant<LoginModel>(
builder: (context, child, model) => FlatButton(
onPressed:
model.step == model.steps.length - 1
? model.startOver
: model.nextStep,
color: Colors.black,
shape: new RoundedRectangleBorder(
borderRadius:
new BorderRadius.circular(30.0)),
child: Padding(
padding: const EdgeInsets.all(14.0),
child:
model.step == model.steps.length - 1
? Text(
'Start Over',
style: Theme.of(context)
.textTheme
.copyWith(
button: TextStyle(
color: Colors.white,
))
.button,
)
: Icon(
Icons.arrow_forward,
color: Colors.white,
size: 25.0,
)),
),
),
)),
],
),
)
],
),
),
),
),
);
}
}
import 'package:flutter/material.dart';
import 'package:scoped_model/scoped_model.dart';
enum StepType { username, email, phone, password }
class LoginModel extends Model {
//steps fields options
int _step = 0;
int get step => _step;
set step(int value) => _step = value;
var steps = <Step>[
Step(
type: StepType.username,
field: "username",
controller: new TextEditingController(),
hint: "How can we call you?"),
Step(
type: StepType.email,
field: "email",
controller: new TextEditingController(),
hint: "Your Email goes here"),
Step(
type: StepType.phone,
field: "mobile",
controller: new TextEditingController(),
hint: "Phone number ex: +201234..."),
Step(
type: StepType.password,
field: "password",
controller: new TextEditingController(),
hint: "your Password"),
];
void startOver() {
_step = 0;
notifyListeners();
}
void saveStep(String value) {
nextStep();
}
void nextStep() {
_step++;
notifyListeners();
}
void previousStep() {
_step--;
notifyListeners();
}
}
class Step {
String field;
TextEditingController controller;
String hint;
StepType type;
Step({this.field, this.controller, this.hint, this.type});
}