Reputation: 63
Functionality behind the app:
I have included an image example of how it would look like once the the code is successful.
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:material_design_icons_flutter/material_design_icons_flutter.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Welcome to Flutter',
home: Scaffold(
appBar: AppBar(
backgroundColor: Colors.deepPurple[200],
actions: <Widget>[
IconButton(
icon: Icon(
MdiIcons.soccer,
color: Colors.grey[800],
),
iconSize: 30,
onPressed: () => {
CupertinoAlertDialog(
title: Text('Success!'),
content: Text('You are in the football universe!'),
),
},
),
],
),
body: Center(
child: Text('Hello World'),
),
),
);
}
}
Issues:
Things I have tried that were unsuccessful:
Upvotes: 0
Views: 4703
Reputation: 54367
You can copy paste run full code below
Step 1: You can use showDialog
Step 2: Move MaterialApp
to upper level void main() => runApp(MaterialApp(title: 'Welcome to Flutter', home: MyApp()));
code snippet
Future<void> _handleClickMe() async {
return showDialog<void>(
context: context,
barrierDismissible: false, // user must tap button!
builder: (BuildContext context) {
return CupertinoAlertDialog(
title: Text('Success!'),
content: Text('You are in the football universe!'),
);
},
);
}
working demo
full code
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:material_design_icons_flutter/material_design_icons_flutter.dart';
void main() => runApp(MaterialApp(title: 'Welcome to Flutter', home: MyApp()));
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
Future<void> _handleClickMe() async {
return showDialog<void>(
context: context,
barrierDismissible: false, // user must tap button!
builder: (BuildContext context) {
return CupertinoAlertDialog(
title: Text('Success!'),
content: Text('You are in the football universe!'),
);
},
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.deepPurple[200],
actions: <Widget>[
IconButton(
icon: Icon(
MdiIcons.soccer,
color: Colors.grey[800],
),
iconSize: 30,
onPressed: () => _handleClickMe(),
),
],
),
body: Center(
child: Text('Hello World'),
),
);
}
}
Upvotes: 1
Reputation: 703
Looks like you need to call showDialog and pass your CupertinoAlertDialog to the builder
onPressed: () => {
showDialog(
context: context,
builder: (BuildContext context) => CupertinoAlertDialog(
title: Text('Success!'),
content: Text('You are in the football universe!')
),
),
},
Upvotes: 1