Flutter 当子项和反馈相同时,如何防止在可拖动设备内重建 问题
我希望用户能够拖动卡片中的文本字段。 因此,孩子和反馈应该始终处于相同的状态。 然而,只要我触摸到卡,小部件就会被重建,状态就会被重置 尝试的解决方案 我试过使用键,无状态小部件,我不知道 代码Flutter 当子项和反馈相同时,如何防止在可拖动设备内重建 问题,flutter,draggable,Flutter,Draggable,我希望用户能够拖动卡片中的文本字段。 因此,孩子和反馈应该始终处于相同的状态。 然而,只要我触摸到卡,小部件就会被重建,状态就会被重置 尝试的解决方案 我试过使用键,无状态小部件,我不知道 代码 import'dart:math'; 进口“包装:颤振/材料.省道”; 导入“bloc/home.dart”; 常量字符='AABBCCDDEEFFGGHHIIJKKLMNNOOPQQRRSSTTUUVVWXXYYZ1234567890'; Random _rnd=Random(); String ge
import'dart:math';
进口“包装:颤振/材料.省道”;
导入“bloc/home.dart”;
常量字符='AABBCCDDEEFFGGHHIIJKKLMNNOOPQQRRSSTTUUVVWXXYYZ1234567890';
Random _rnd=Random();
String getRandomString(int-length)=>String.fromCharCodes(Iterable.generate(
长度,()=>_chars.codeunit(_rnd.nextInt(_chars.length));
类QuestionCardDragable扩展StatefulWidget{
最后一道弦乐题;
最终HomeBloc HomeBloc;
问题可拖动(this.question,this.homeBloc);
@凌驾
_QuestionCardDragableState createState()=>\u QuestionCardDragableState();
}
类_questionCardDragableState扩展状态{
TextEditingController\u myController;
@凌驾
void initState(){
super.initState();
这是._myController=TextEditingController(text:getRandomString(10));
_myController.addListener(()=>{widget.homeBloc.add(AnswerChanged(答案:_myController.text))});
}
@凌驾
小部件构建(构建上下文){
回程卡(
子:列(
mainAxisAlignment:mainAxisAlignment.start,
crossAxisAlignment:crossAxisAlignment.start,
儿童:[
容器(
填充:边缘组。对称(垂直:32.0,水平:16.0),
子:列(
crossAxisAlignment:crossAxisAlignment.start,
儿童:[
文本(widget.question,
样式:TextStyle(
fontSize:20.0,fontWeight:fontWeight.w700),
填充(填充:仅限边集(底部:8.0)),
TextFormField(
控制器:_myController,
maxLines:8,
装饰:输入装饰(
边框:OutlineInputBorder(),
hintText:'你的答案…'
),
),
],
))
],
),
);
}
@凌驾
无效处置(){
//当小部件从控制器中移除时,请清理控制器
//小部件树。
_myController.dispose();
super.dispose();
}
}
导入“包装:颤振/材料.省道”;
进口“包装:颤振团/颤振团.飞镖”;
导入“包:projectQ/bloc/bloc.dart”;
导入'questionCardDragable.dart';
导入“package:projectQ/models/QuestionAnswer.dart”;
类CardsSectionDragable扩展StatefulWidget{
@凌驾
_CardsSectionState createState()=>\u CardsSectionState();
}
类CardsSectionState扩展了状态{
家庭集团(HomeBloc);;
@凌驾
void initState(){
super.initState();
这._homeBloc=BlocProvider.of(上下文);
}
@凌驾
小部件构建(构建上下文){
返回BlocBuilder(
生成器:(上下文、状态){
变量长度=state.questionsAnswers.length;
var cards=List();
如果(长度>=3){
卡片。添加(不可拖动)(
抵销:1,
宽度:0.8,
高度:0.5,
子项:QuestionCardRaggableStateless(state.questionsAnswers[2]。问题,state.questionsAnswers[2]。答案,_homeBloc),
));
}
如果(长度>=2){
卡片。添加(不可拖动)(
偏移量:0.8,
宽度:0.85,
身高:0.55,
子项:QuestionCardRaggableStateless(state.questionsAnswers[1]。问题,state.questionsAnswers[1]。答案,_homeBloc),
));
}
如果(长度>=1){
打印('state:${state.questionsAnswers.first.answer}');
卡片。添加(可拖动)(
偏移量:0,
宽度:0.9,
高度:0.6,
子项:QuestionCardDragable(state.questionsAnswers.first.question,_homeBloc),//QuestionCardDragable无状态(state.questionsAnswers.first.question,state.questionsAnswers.first.answer,_homeBloc),
key:UniqueKey()//ValueKey(state.questionsAnswers.first.question)
));
}
扩大回报(
子:堆栈(
儿童:[
//拖动目标行
划船(
mainAxisSize:mainAxisSize.max,
儿童:[
dragTarget(),
灵活的(flex:2,child:Container()),
dragTarget()
],
),
//结束文本
对齐(
对齐:对齐(0.0,0.0),
子:容器(
子:文本(
“今天你完了!”,
风格:Theme.of(context).textTheme.headline3,
),
保证金:全部(40),
)
),
…卡片,
]
));
}
);
}
小部件不可拖动({双偏移量、双高度、双宽度、小部件子项}){
返回对齐(
对齐:对齐(0.0,偏移),
孩子:无知的指针(
子项:SizedBox.fromSize(
size:size(MediaQuery.of(context).size.width*width,
MediaQuery.of(context.size.height*height),
孩子:孩子,
)),
);
}
小部件可拖动({双偏移量,双高度,双宽度,小部件子项,键}){
Widget keyChild=SizedBox.fromSize(
钥匙:钥匙,
size:size(MediaQuery.of(context).size.width*width,
MediaQuery.of(context.size.height*height),
孩子:孩子,
);
返回对齐(
对齐:对齐(0.0,偏移),
孩子:拖拉(
反馈:k
import 'dart:math';
import 'package:flutter/material.dart';
import 'bloc/home.dart';
const _chars = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz1234567890';
Random _rnd = Random();
String getRandomString(int length) => String.fromCharCodes(Iterable.generate(
length, (_) => _chars.codeUnitAt(_rnd.nextInt(_chars.length))));
class QuestionCardDraggable extends StatefulWidget{
final String question;
final HomeBloc homeBloc;
QuestionCardDraggable(this.question, this.homeBloc);
@override
_QuestionCardDraggableState createState() => _QuestionCardDraggableState();
}
class _QuestionCardDraggableState extends State<QuestionCardDraggable> {
TextEditingController _myController;
@override
void initState(){
super.initState();
this._myController = TextEditingController(text: getRandomString(10));
_myController.addListener(() => {widget.homeBloc.add(AnswerChanged(answer: _myController.text))});
}
@override
Widget build(BuildContext context) {
return Card(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Container(
padding: EdgeInsets.symmetric(vertical: 32.0, horizontal: 16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(widget.question,
style: TextStyle(
fontSize: 20.0, fontWeight: FontWeight.w700)),
Padding(padding: EdgeInsets.only(bottom: 8.0)),
TextFormField(
controller: _myController,
maxLines: 8,
decoration: InputDecoration(
border: OutlineInputBorder(),
hintText: 'your answer...'
),
),
],
))
],
),
);
}
@override
void dispose() {
// Clean up the controller when the widget is removed from the
// widget tree.
_myController.dispose();
super.dispose();
}
}
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:projectQ/bloc/bloc.dart';
import 'QuestionCardDraggable.dart';
import 'package:projectQ/models/QuestionAnswer.dart';
class CardsSectionDraggable extends StatefulWidget {
@override
_CardsSectionState createState() => _CardsSectionState();
}
class _CardsSectionState extends State<CardsSectionDraggable> {
HomeBloc _homeBloc;
@override
void initState() {
super.initState();
this._homeBloc = BlocProvider.of<HomeBloc>(context);
}
@override
Widget build(BuildContext context) {
return BlocBuilder<HomeBloc, HomeState>(
builder: (context, state) {
var length = state.questionsAnswers.length;
var cards = List();
if (length >= 3){
cards.add(notDraggable(
offset: 1,
width: 0.8,
height: 0.5,
child: QuestionCardDraggableStateless(state.questionsAnswers[2].question, state.questionsAnswers[2].answer, _homeBloc),
));
}
if (length >= 2){
cards.add(notDraggable(
offset: 0.8,
width: 0.85,
height: 0.55,
child: QuestionCardDraggableStateless(state.questionsAnswers[1].question, state.questionsAnswers[1].answer, _homeBloc),
));
}
if (length >= 1){
print( 'state: ${state.questionsAnswers.first.answer}');
cards.add(draggable(
offset: 0,
width: 0.9,
height: 0.6,
child: QuestionCardDraggable(state.questionsAnswers.first.question, _homeBloc), //QuestionCardDraggableStateless(state.questionsAnswers.first.question, state.questionsAnswers.first.answer, _homeBloc),
key: UniqueKey() //ValueKey(state.questionsAnswers.first.question)
));
}
return Expanded(
child: Stack(
children: <Widget>[
// Drag target row
Row(
mainAxisSize: MainAxisSize.max,
children: <Widget>[
dragTarget(),
Flexible(flex: 2, child: Container()),
dragTarget()
],
),
//End text
Align(
alignment: Alignment(0.0, 0.0),
child: Container(
child: Text(
"You are done for today!",
style: Theme.of(context).textTheme.headline3,
),
margin: EdgeInsets.all(40),
)
),
...cards,
]
));
}
);
}
Widget notDraggable({double offset, double height, double width, Widget child}) {
return Align(
alignment: Alignment(0.0, offset),
child: IgnorePointer(
child: SizedBox.fromSize(
size: Size(MediaQuery.of(context).size.width * width,
MediaQuery.of(context).size.height * height),
child: child,
)),
);
}
Widget draggable({double offset, double height, double width, Widget child, Key key}){
Widget keyChild = SizedBox.fromSize(
key: key,
size: Size(MediaQuery.of(context).size.width * width,
MediaQuery.of(context).size.height * height),
child: child,
);
return Align(
alignment: Alignment(0.0, offset),
child: Draggable(
feedback: keyChild,
child: keyChild,
childWhenDragging: Container(),
),
);
}
Widget dragTarget() {
return Flexible(
flex: 1,
child: DragTarget(
builder: (_, __, ___) {
return Container();
},
onWillAccept: (_) {
return true;
},
onAccept: (_) {
_homeBloc.add(PopCard());
},
));
}
}