【发布时间】:2020-04-09 18:49:41
【问题描述】:
我有一个包含StateFull Widgets 的ListView,每个Tiles 代表列表中的一个对象。问题是当我尝试从该列表中删除对象时,屏幕上只会删除最后一个Tile。对于删除项目,我使用对每个 Tile 的适当方法 (_deleteItem(_HomeItem item)) 的引用。我怀疑这是我使用的Keys 的问题,但我不确定。我已经尝试使用不同的键(即ObjectKey(item) 和GlobalKey<_TileState>()),但这并没有改变任何东西。
我只发现了一个关于我的问题here 的问题。但是那里的解决方案要么不起作用,要么我错误地遵循了它们。
这是我尝试做的最小工作示例:
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: 'Flutter Slidable Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Slidable Demo'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final List<_HomeItem> items = List.generate(
5,
(i) => _HomeItem(
i,
'Tile n°$i',
),
);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: _buildList(context),
),
);
}
Widget _buildList(BuildContext context) {
return ListView.builder(
itemBuilder: (context, index) {
return Tile(items[index], _deleteItem);
},
itemCount: items.length,
);
}
void _deleteItem(_HomeItem item) {
setState(() {
print(context);
print("remove: $item");
print("Number of items before: ${items.length}");
items.remove(item);
print("Number of items after delete: ${items.length}");
});
}
}
class Tile extends StatefulWidget {
final _HomeItem item;
final Function delete;
Tile(this.item, this.delete);
@override
State<StatefulWidget> createState() => _TileState(item, delete);
}
class _TileState extends State<Tile> {
final _HomeItem item;
final Function delete;
_TileState(this.item, this.delete);
@override
Widget build(BuildContext context) {
return ListTile(
key: ValueKey(item.index),
title: Text("${item.title}"),
subtitle: Text("${item.index}"),
onTap: () => delete(item),
);
}
}
class _HomeItem {
const _HomeItem(
this.index,
this.title,
);
final int index;
final String title;
}
因为这会导致误解,所以现在列表中的每个项目是这样的:
/*
privacyIDEA Authenticator
Authors: Timo Sturm <timo.sturm@netknights.it>
Copyright (c) 2017-2019 NetKnights GmbH
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import 'dart:developer';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_slidable/flutter_slidable.dart';
import 'package:privacyidea_authenticator/model/tokens.dart';
import 'package:privacyidea_authenticator/utils/storageUtils.dart';
import 'package:privacyidea_authenticator/utils/util.dart';
class TokenWidget extends StatefulWidget {
final Token _token;
final Function _delete;
TokenWidget(this._token, this._delete);
@override
State<StatefulWidget> createState() {
if (_token is HOTPToken) {
return _HotpWidgetState(_token, _delete);
} else if (_token is TOTPToken) {
return _TotpWidgetState(_token, _delete);
} else {
throw ArgumentError.value(_token, "token",
"The token [$_token] is of unknown type and not supported.");
}
}
}
abstract class _TokenWidgetState extends State<TokenWidget> {
final Token _token;
static final SlidableController _slidableController = SlidableController();
String _otpValue;
String _label;
final Function _delete;
_TokenWidgetState(this._token, this._delete) {
_otpValue = calculateOtpValue(_token);
_saveThisToken();
_label = _token.label;
}
@override
Widget build(BuildContext context) {
return Slidable(
key: ValueKey(_token.serial),
// This is used to only let one Slidable be open at a time.
controller: _slidableController,
actionPane: SlidableDrawerActionPane(),
actionExtentRatio: 0.25,
child: Container(
color: Colors.white,
child: _buildTile(),
),
secondaryActions: <Widget>[
IconSlideAction(
caption: 'Delete',
color: Colors.red,
icon: Icons.delete,
onTap: () => _deleteTokenDialog(),
),
IconSlideAction(
caption: 'Rename',
color: Colors.blue,
icon: Icons.edit,
onTap: () => _renameTokenDialog(),
),
],
);
}
// TODO Test this behaviour with integration testing.
void _renameTokenDialog() {
final _nameInputKey = GlobalKey<FormFieldState>();
String _selectedName;
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text("Rename token"),
titleTextStyle: Theme.of(context).textTheme.subhead,
content: TextFormField(
autofocus: true,
initialValue: _label,
key: _nameInputKey,
onChanged: (value) => this.setState(() => _selectedName = value),
decoration: InputDecoration(labelText: "Name"),
validator: (value) {
if (value.isEmpty) {
return 'Please enter a name for this token.';
}
return null;
},
),
actions: <Widget>[
FlatButton(
child: Text("Rename"),
onPressed: () {
if (_nameInputKey.currentState.validate()) {
_renameToken(_selectedName);
Navigator.of(context).pop();
}
},
),
FlatButton(
child: Text("Cancel"),
onPressed: () => Navigator.of(context).pop(),
),
],
);
});
}
void _renameToken(String newLabel) {
_saveThisToken();
log(
"Renamed token:",
name: "token_widgets.dart",
error: "\"${_token.label}\" changed to \"$newLabel\"",
);
_token.label = newLabel;
setState(() {
_label = _token.label;
});
}
void _deleteTokenDialog() {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text("Confirm deletion"),
titleTextStyle: Theme.of(context).textTheme.subhead,
content: RichText(
text: TextSpan(
style: TextStyle(
color: Colors.black,
),
children: [
TextSpan(
text: "Are you sure you want to delete ",
),
TextSpan(
text: "\'$_label\'?",
style: TextStyle(
fontStyle: FontStyle.italic,
))
]),
),
actions: <Widget>[
FlatButton(
onPressed: () => {
_delete(_token),
Navigator.of(context).pop(),
},
child: Text("Yes!"),
),
FlatButton(
onPressed: () => Navigator.of(context).pop(),
child: Text("No, take me back!"),
),
],
);
});
}
void _saveThisToken() {
StorageUtil.saveOrReplaceToken(this._token);
}
void _updateOtpValue();
Widget _buildTile();
}
class _HotpWidgetState extends _TokenWidgetState {
_HotpWidgetState(Token token, Function delete) : super(token, delete);
@override
void _updateOtpValue() {
setState(() {
(_token as HOTPToken).incrementCounter();
_otpValue = calculateOtpValue(_token);
});
}
@override
Widget _buildTile() {
return Stack(
children: <Widget>[
ListTile(
title: Center(
child: Text(
insertCharAt(_otpValue, " ", _otpValue.length ~/ 2),
textScaleFactor: 2.5,
),
),
subtitle: Center(
child: Text(
_label,
textScaleFactor: 2.0,
),
),
),
Align(
alignment: Alignment.centerRight,
child: RaisedButton(
onPressed: () => _updateOtpValue(),
child: Text(
"Next",
textScaleFactor: 1.5,
),
),
),
],
);
}
}
class _TotpWidgetState extends _TokenWidgetState
with SingleTickerProviderStateMixin {
AnimationController
controller; // Controller for animating the LinearProgressAnimator
_TotpWidgetState(Token token, Function delete) : super(token, delete);
@override
void _updateOtpValue() {
setState(() {
_otpValue = calculateOtpValue(_token);
});
}
@override
void initState() {
super.initState();
controller = AnimationController(
duration: Duration(seconds: (_token as TOTPToken).period),
// Animate the progress for the duration of the tokens period.
vsync:
this, // By extending SingleTickerProviderStateMixin we can use this object as vsync, this prevents offscreen animations.
)
..addListener(() {
// Adding a listener to update the view for the animation steps.
setState(() => {
// The state that has changed here is the animation object’s value.
});
})
..addStatusListener((status) {
// Add listener to restart the animation after the period, also updates the otp value.
if (status == AnimationStatus.completed) {
controller.forward(from: 0.0);
_updateOtpValue();
}
})
..forward(); // Start the animation.
// Update the otp value when the android app resumes, this prevents outdated otp values
// ignore: missing_return
SystemChannels.lifecycle.setMessageHandler((msg) {
log(
"SystemChannels:",
name: "totpwidget.dart",
error: msg,
);
if (msg == AppLifecycleState.resumed.toString()) {
_updateOtpValue();
}
});
}
@override
void dispose() {
controller.dispose(); // Dispose the controller to prevent memory leak.
super.dispose();
}
@override
Widget _buildTile() {
return Column(
children: <Widget>[
ListTile(
title: Center(
child: Text(
insertCharAt(_otpValue, " ", _otpValue.length ~/ 2),
textScaleFactor: 2.5,
),
),
subtitle: Center(
child: Text(
_label,
textScaleFactor: 2.0,
),
),
),
LinearProgressIndicator(
value: controller.value,
),
],
);
}
}
计划是在未来将不同类型的项目添加到列表中,每个项目可能会呈现完全不同的内容,并且需要不同的逻辑。完整项目请参考github。虽然我确信这是为列表创建不同小部件的正确方法,但我愿意接受不同的方法来做到这一点。
【问题讨论】: