Om du ofta vill skapa liknande dialogrutor kan du göra en funktion för det. Skapa alerts.dart
:
import 'package:flutter/material.dart'; class Alert { static message(BuildContext context, String title, String body, {bool error = false}) async { showDialog( barrierDismissible: false, context: context, builder: (BuildContext context) { return AlertDialog( title: Text(title, style: TextStyle(color: (error) ? Colors.red : Colors.green, fontSize: 18, fontWeight: FontWeight.w700, letterSpacing: 1)), content: Text(body), actions: <Widget>[ FlatButton( onPressed: () { Navigator.of(context).pop(); }, child: Text('OK'), ) ], ); } ); } }
Hämta alerts.dart
i den fil du ska använda
import 'alerts.dart';
Anropa Alert.message() där du vill ha en dialogruta.
RaisedButton( child: Text('Show error message'), onPressed: () { Alert.message(context, 'Error message', 'Lorem ipsum dolor sit amet consectetur adipisicing elit.', error: true); }, ),