我正在開發我的顫振應用程式,一切對我來說都很好,但是現在當我在重新加載后通過滑動洗掉筆記時,它們又回來了,我對此無能為力。
import 'dart:convert';
import 'dart:collection';
import 'package:flutter/material.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/rendering.dart';
import 'package:shared_preferences/shared_preferences.dart';
class Home extends StatefulWidget {
const Home ({key}): super(key: key);
@override
State<Home> createState() => _HomeState();
}
class _HomeState extends State<Home> {
final myController = TextEditingController();
bool submit = false;
Color mainColor = Color(0xFFEEEFF5);
Color secColor = Color(0xFF3A3A3A);
Color tdBlue = Color(0xFF5F52EE);
String temp = "";
List<String> todoList = [];
@override
void initState() {
super.initState();
myController.addListener(() {
setState(() {
submit = myController.text.isNotEmpty;
});
});
omLoadData();
}
submitData() async {
setState(() {
todoList.add(temp);
clearText();
});
SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.setStringList('todo_list', todoList);
}
omLoadData() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
final data = prefs.getStringList("todo_list");
todoList.addAll(data!);
}
@override
void dispose() {
// Clean up the controller when the widget is disposed.
myController.dispose();
super.dispose();
}
void clearText() {
myController.clear();
}
@override
Widget build(BuildContext context){
return Scaffold(
backgroundColor: mainColor,
appBar: AppBar(
elevation: 0.0,
backgroundColor: secColor,
title: Text ('ToDo - List', style: TextStyle(fontSize: 26.5, fontWeight: FontWeight.bold, fontStyle: FontStyle.italic),),
),
body: Column(
children: [
Container(
margin: EdgeInsets.only(
top: 15.0,
left: 13.0,
right: 8.0,
),
child: Row(
children: [
Expanded(
child: TextField(
onChanged: (String value) {
temp = value;
},
controller: myController,
decoration:
InputDecoration(
prefixIcon: Icon(Icons.notes, color: Colors.orangeAccent,),
labelText: 'Зам?тка',
hintText: 'Введ?ть зам?тку',
filled: true,
fillColor: Colors.white,
border: OutlineInputBorder(),
contentPadding: EdgeInsets.all(10.0),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(30.0),),
),
),
),
),
SizedBox(
width: 10.0,
),
ElevatedButton(
style: ElevatedButton.styleFrom(backgroundColor: tdBlue),
onPressed:
submit ? () => submitData() : null,
child:
Text(' ', style: TextStyle(fontSize: 35),),
),
], // Закрива?ться 2-ий ч?лдрен
), //Row 1-ий
),
Expanded(
child: Padding(
padding: EdgeInsets.only(
top: 5.0,
),
child: ListView.builder(
itemCount: todoList.length,
itemBuilder: (BuildContext context, int index){
return Dismissible(
direction: DismissDirection.endToStart,
background: const Card(color: Colors.red,
child: Icon(Icons.delete_sweep,size: 30, color: Colors.white,)
),
key: Key(todoList[index]),
onDismissed: (direction){
setState(() async {
todoList.removeAt(index);
});
},
child: Card(
margin: EdgeInsets.only(
top: 8.5,
left: 25.0,
right: 25.0,
),
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(25.0)),
elevation: 0.0,
child:
ListTile(
title: Text(todoList[index],
),),
),
);
},
),
)
),
], // Закрива?ться 1-ий ч?лдрен
),
);
}
}
使用滑動洗掉串列項時,它會被洗掉,直到我進行熱重啟。當我進行熱重啟時,該專案會重新出現。也許我的代碼中有錯誤。請幫幫我。
uj5u.com熱心網友回復:
submitData() async {
setState(() {
todoList.add(temp);
clearText();
onSaveData();
});
}
onSaveData()async{
SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.setStringList('todo_list', todoList);
}
在 onDismissed 中呼叫 onSaveData()
onDismissed: (direction) {
setState(() async {
todoList.removeAt(index);
onSaveData();
});
},
uj5u.com熱心網友回復:
onDismissed在你的方法上試試這個:
onDismissed: (direction){
setState(() async {
todoList.removeAt(index);
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.remove(todoList[index]);
});
},
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/523017.html
標籤:扑镖
上一篇:type'_InternalLinkedHashMap<String,dynamic>'不是使用http包的'String'型別的子型別
