完成新手,所以请耐心等待。关于如何使用DRY原则重构代码的问题。我确定可以在这样一个简单的例子上完成,所以这里就是....我的代码如下所示,显示了三个' switchListTiles'。我已经设法解决了如何在彼此之上创建3个switchListTiles,以及如何让它们单独打开/关闭。只是这意味着我将创建以下功能3次:
bool _value3 = false;
void _onChanged3(bool value3) {
setState(() {
_value3 = value3;
});
}
我确定有一种方法可以改变这一点,所以我不会有三次相同的代码。 任何帮助将不胜感激 非常感谢提前
import 'package:flutter/material.dart';
void main() {
runApp(new MaterialApp(
title: "Switch Widget Demo",
home: new MyApp(),
));
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
bool _value = false;
void _onChanged(bool value) {
setState(() {
_value = value;
});
}
bool _value2 = false;
void _onChanged2(bool value2) {
setState(() {
_value2 = value2;
});
}
bool _value3 = false;
void _onChanged3(bool value3) {
setState(() {
_value3 = value3;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Switch Demo"),
backgroundColor: Colors.redAccent,
centerTitle: true,
),
body: Container(
padding: EdgeInsets.all(32.0),
child: Column(
children: <Widget>[
Switch(value: _value,
onChanged: (bool value) {_onChanged(value);}),
SwitchListTile(value: _value,
title: Text("Click Me"),
activeColor: Colors.red,
secondary: Icon(Icons.home),
subtitle: Text("For my small print"),
onChanged: (bool value) {_onChanged(value);}),
SwitchListTile(value: _value2,
title: Text("Click Me Again Please"),
activeColor: Colors.lightGreen,
secondary: Icon(Icons.perm_identity),
onChanged: (bool value2) {_onChanged2(value2);}),
SwitchListTile(value: _value3,
title: Text("And Again Please"),
activeColor: Colors.blueGrey,
subtitle: Text("Some more small print"),
secondary: Icon(Icons.person),
onChanged: (bool value) {_onChanged3(value);}),
],
),
),
);
}
}
答案 0 :(得分:2)
您只需将 public class MyService extends Service {
final static String TAG = MyService.class.getName();
ReceiverCall receiverCall;
private static final int NOTIFICATION_ID = 1;
MyService (){
super();
}
class Mythread implements Runnable {
int service_id;
Mythread(int service_id) {
this.service_id = service_id;
}
@Override
public void run() {
int i = 0;
synchronized (this) {
// while (i < 10) {
try {
wait(2500);
i++;
processStartNotification(String.valueOf(i));
} catch (InterruptedException e) {
e.printStackTrace();
}
// }
}
}
}
@Override
public void onCreate() {
receiverCall= new ReceiverCall();
super.onCreate();
}
@Override
public void onDestroy() {
super.onDestroy();
super.onDestroy();
Toast.makeText(getApplicationContext(), "Service Task destroyed", Toast.LENGTH_LONG).show();
Intent myIntent = new Intent(getApplicationContext(), MyService.class);
startService(myIntent);
}
@Override
public void onTaskRemoved(Intent rootIntent) {
super.onTaskRemoved(rootIntent);
Intent myIntent = new Intent(getApplicationContext(), MyService.class);
startService(myIntent);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Thread thread = new Thread(new Mythread(startId));
thread.start();
return START_STICKY;
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
private void processStartNotification(String s) {
// Do something. For example, fetch fresh data from backend to create a rich notification?
final NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentTitle("Scheduled Notification")
.setAutoCancel(true)
.setColor(getResources().getColor(R.color.colorAccent))
.setContentText(" Notification Service"+s)
.setSmallIcon(R.drawable.ic_launcher_background);
builder.setDeleteIntent(receiverCall.getDeleteIntent(this));
final NotificationManager manager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(NOTIFICATION_ID, builder.build());
}
}
BroadcastReceiver Class :
public class ReceiverCall extends BroadcastReceiver {
private static final String ACTION_START_NOTIFICATION_SERVICE = "ACTION_START_NOTIFICATION_SERVICE";
private static final String ACTION_DELETE_NOTIFICATION = "ACTION_DELETE_NOTIFICATION";
private static final int NOTIFICATIONS_INTERVAL_IN_HOURS = 2;
@Override
public void onReceive(Context context, Intent intent) {
Log.i("Service Stops", "Ohhhhhhh");
context.startService(new Intent(context, MyService.class));
}
public static PendingIntent getDeleteIntent(Context context) {
Intent intent = new Intent(context, ReceiverCall.class);
intent.setAction(ACTION_DELETE_NOTIFICATION);
return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
}
重构为其单独的类,然后在父窗口小部件中生成 <service
android:name=".MyService"
android:exported="true"
android:stopWithTask="false"></service>
<receiver android:name=".ReceiverCall">
<intent-filter>
<action android:name="com.servicesex" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
。
在这里,我创建了20个代码较少的代码:
SwitchListTiles