我是 RiverPod 的新手。我想從用戶獲取輸入 b?t 我無法在我的應用程式中使用 TextField,而 RiverPod.TextController 的值為空。
我也得到一個錯誤FormatError:Invalid Number
final User_value = StateProvider<int>((ref) {
return 0;
});
final controller = TextEditingController();
final controllerprovider = StateProvider<String>((ref) {
return controller.text;
});
class HomePage extends ConsumerWidget {
const HomePage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context, WidgetRef ref) {
final controllertext = ref.watch(controllerprovider);
final user_value = ref.watch(User_value);
return Scaffold(
appBar: AppBar(
title: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text("Home"),
],
),
),
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
child: Text("Go to the Counter page"),
onPressed: () {
Navigator.of(context).push(
//Navigation
MaterialPageRoute(builder: (((context) => CounterPage()))),
);
},
),
SizedBox(
height: 60,
),
TextField(
controller: controller,
decoration: InputDecoration(
labelText: "Enter a number",
border: OutlineInputBorder(),
prefixIcon: Icon(Icons.numbers),
suffixIcon: controllertext.isEmpty
? Container(
width: 0,
)
: IconButton(
icon: Icon(Icons.close), //If user wants to enter another number this is the easy way to delete the current one.
onPressed: () {
ref.invalidate(controllerprovider);
},
)),
),
IconButton(
onPressed: () {
temp = ref.read(User_value.notifier).state;
print(controllertext);
ref.read(User_value.notifier).state =
int.parse(ref.read(controllerprovider.notifier).state); // Thats where I got the error.
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text("Input has been saved"),
action: SnackBarAction(
label: "Undo",
onPressed: () {
ref.read(User_value.notifier).state = temp;
},
),
));
},
icon: Icon(Icons.add_task)),
],
));
}
}
我無法解決如何從用戶那里獲取輸入。用戶應該輸入一個數字,單擊按鈕,數字應該保存為控制器的狀態。
uj5u.com熱心網友回復:
最好使用int.tryParse并提供例外的默認值,您不需要使用全域 TextEditingController。你可以關注這個小部件。
final userValueProvider = StateProvider<int>((ref) {
return 0;
});
final controllerProvider = StateProvider<String>((ref) {
return "";
});
class HomePage extends ConsumerWidget {
HomePage({Key? key}) : super(key: key);
final controller = TextEditingController();
@override
Widget build(BuildContext context, WidgetRef ref) {
final controllerText = ref.watch(controllerProvider);
final userValue = ref.watch(userValueProvider);
debugPrint("rebuilding");
return Scaffold(
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text("controllerText $controllerText userValue $userValue"),
TextField(
controller: controller,
onChanged: (value) {
debugPrint(value);
ref.read(controllerProvider.notifier).state = value;
},
decoration: InputDecoration(
labelText: "Enter a number",
suffixIcon: controllerText.isEmpty
? null
: IconButton(
icon: const Icon(Icons.close),
onPressed: () {
ref.read(controllerProvider.notifier).state = ""; //prefer using .update
controller.clear(); //clear TextField
},
)),
),
IconButton(
onPressed: () {
final oldValue = ref.read(userValueProvider.notifier).state;
ref
.read(userValueProvider.notifier)
.update((state) => int.tryParse(controller.text) ?? 0);
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: const Text("Input has been saved"),
action: SnackBarAction(
label: "Undo",
onPressed: () {
ref
.read(userValueProvider.notifier)
.update((state) => oldValue);
},
),
));
},
icon: const Icon(Icons.add_task)),
],
),
);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/515988.html
標籤:扑镖颤振布局文本域河荚
