DevBoi

[Flutter] 팝업 띄우기 본문

[Mobile]/[Flutter]

[Flutter] 팝업 띄우기

HiSmith 2023. 7. 23. 12:29
반응형

1. 별도 메소드 작성

Future<void> _dialogBuilder(BuildContext context) {
  return showDialog<void>(
    context: context,
    builder: (BuildContext context) {
      return AlertDialog(
        title: const Text('Basic dialog title'),
        content: const Text(
          'A dialog is a type of modal window that\n'
              'appears in front of app content to\n'
              'provide critical information, or prompt\n'
              'for a decision to be made.',
        ),
        actions: <Widget>[
          TextButton(
            style: TextButton.styleFrom(
              textStyle: Theme.of(context).textTheme.labelLarge,
            ),
            child: const Text('Disable'),
            onPressed: () {
              Navigator.of(context).pop();
            },
          ),
          TextButton(
            style: TextButton.styleFrom(
              textStyle: Theme.of(context).textTheme.labelLarge,
            ),
            child: const Text('Enable'),
            onPressed: () {
              Navigator.of(context).pop();
            },
          ),
        ],
      );
    },
  );
}

 

2. 기존 레이아웃에서 Context 전달

  @override
  Widget build(BuildContext context) {

    return Scaffold(
      backgroundColor: Colors.blue,
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[

          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () => _dialogBuilder(context),
        tooltip: 'Increment',
        backgroundColor: Colors.white,
        child: const Icon(Icons.add,color: Colors.black,),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }

 

 

해당 버튼 클릭했을때 팝업이 나오게 할 수있다.

 

반응형