[英]How do I set a default color in my box and let the color change depending on input?
我正在為女兒制作一個簡單的數學應用程序。 現在我有一個顯示問題的TextField(裝飾顏色為白色)(例如'2 + 5 ='),然后她可以在同一框中鍵入答案(顯示'2 + 5 = 7')。
在此textField下面,我還有另一個textField。 用戶點擊按鈕后,此字段將給出正確答案。 如果用戶的回答正確,則裝飾顏色變為綠色,如果答案錯誤,則裝飾顏色變為紅色。
問題是,在按下校正按鈕之前,我很難讓此框變白。 此時,直到變為綠色或紅色為止,沒有顏色。
我現在將每個框放在單獨的類中,所以如果我做錯了什么,我也不會太混亂。
我們非常感謝您對將顏色設置為“默認”到第二個框的任何幫助。 謝謝。
import 'package:flutter/material.dart';
class CorrectionTextField extends StatelessWidget {
final String text;
final boxPaint;
CorrectionTextField({this.text, this.boxPaint = Colors.white});
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.only(right: 10.0, top: 8.0),
child: Container(
constraints: BoxConstraints.expand(height: 60.0, width: 150),
decoration: BoxDecoration(
color: boxPaint,
borderRadius: BorderRadius.all(const Radius.circular(15.0)),
border: Border.all(color: Colors.black54, width: 4.0),
),
child: Center(
child:
Text(text, style: TextStyle(color: Colors.white, fontSize:
48.0)),
),
),
);
}
}
下一課:
Color correctionColor = Colors.white;
var correct = Colors.green;
var incorrect = Colors.red;
....
body: Column(
children: <Widget>[
operationField,
correctionField,
您可以為構造函數中的任何實例變量指定默認值,以添加可以編寫的默認顏色:
CorrectionTextField({
this.text,
this.boxPaint = Colors.white,
});
這樣,如果在實例化窗口小部件時未指定顏色屬性,則將其設置為白色。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.