Flutter Tutorials

Flutter app development tutorials


flutter – AlertDialog actions center

main.dart


import 'package:flutter/material.dart';

void main() => runApp(const FlutterExample());

class FlutterExample extends StatelessWidget {
  const FlutterExample({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'Flutter Example',
        theme: ThemeData(primarySwatch: Colors.pink),
        home: const StateExample()
    );
  }
}

class StateExample extends StatefulWidget {
  const StateExample({Key? key}) : super(key: key);

  @override
  _StateExampleState createState() => _StateExampleState();
}

class _StateExampleState extends State<StateExample> {

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: const Color(0xFFF5F5F5),
        appBar: AppBar(
            title: const Text("Flutter - AlertDialog Actions Center")
        ),
        body: bodyContent()
    );
  }


  bodyContent(){
    return Center(
      child: ElevatedButton(
        child: const Text("Show Dialog"),
        onPressed: (){
          showAlertDialog();
        },
      ),
    );
  }


  showAlertDialog(){
    Widget cancelButton = TextButton(
        onPressed: (){
          Navigator.pop(context);
        },
        child: const Text("Cancel")
    );

    Widget positiveButton = TextButton(
        onPressed: (){
          Navigator.pop(context);
        },
        child: const Text("Yes")
    );

    AlertDialog dialog = AlertDialog(
      shape: const RoundedRectangleBorder(
          borderRadius: BorderRadius.only(
              topLeft: Radius.circular(5),
              topRight: Radius.circular(25),
              bottomRight: Radius.circular(5),
              bottomLeft: Radius.circular(25)

          )
      ),
      title: const Text("Lorem Ipsum"),
      content: const Text(
          "Lorem Ipsum is simply dummy text of the printing and"
              " typesetting industry. Lorem Ipsum has been the"
              " industry's standard dummy text ever since the 1500s,"
              " when an unknown printer took a galley of type and"
              " scrambled it to make a type specimen book."
      ),

      actions: [
        Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            cancelButton,
            const SizedBox(width: 15),
            positiveButton
          ],
        ),
      ],
    );

    showDialog(
        context: context,
        builder: (BuildContext context){
          return dialog;
        });
  }
}

More flutter tutorials



About Me

Flutter Developer

Design a site like this with WordPress.com
Get started