Notice
Recent Posts
Recent Comments
Link
ยซ   2024/05   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
๊ด€๋ฆฌ ๋ฉ”๋‰ด

lgvv98

[flutter] 19๊ฐ• | ๋นŒ๋” ์œ„์ ฏ ์—†์ด ์Šค๋‚ต๋ฐ” ๋งŒ๋“ค๊ธฐ ๋ฐ ํ† ์ŠคํŠธ ๋ฉ”์‹œ์ง€ ๋ณธ๋ฌธ

flutter/์ˆœํ•œ๋ง›(๊ธฐ์ดˆ)

[flutter] 19๊ฐ• | ๋นŒ๋” ์œ„์ ฏ ์—†์ด ์Šค๋‚ต๋ฐ” ๋งŒ๋“ค๊ธฐ ๋ฐ ํ† ์ŠคํŠธ ๋ฉ”์‹œ์ง€

๐Ÿฅ• ์บ๋Ÿฟ๋งจ 2021. 8. 13. 14:39

โœ… ์ด๋ฒˆ ์‹œ๊ฐ„์—๋Š” ์Šค๋‚ต๋ฐ”์™€ ํ† ์ŠคํŠธ ๋ฉ”์‹œ์ง€์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด์ž.

์Šค๋‚ต๋ฐ”์˜ ๊ฒฝ์šฐ์—๋Š” ํŒจ์น˜๊ฐ•์˜์—์„œ ๋ณด์•˜๋‹ค.

๊ทธ๋ž˜์„œ ScaffoldMessenger๋ฅผ ์ด์šฉํ•ด์„œ ์Šค์Šค๋กœ ๊ฐ•์˜์˜ ์˜ค๋ฅ˜๋ฅผ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ์—ˆ์–ด

๊ฐ•์˜์—์„œ๋Š” 2๊ฐœ๋กœ ๋‚˜๋ˆ ์„œ ๊ฐ•์˜๋ฅผ ์ง„ํ–‰ํ•˜๋Š”๋ฐ ScaffoldMessenger์™€ Toast๋ฉ”์‹œ์ง€๋กœ ๋‚˜๋ˆ ์–ด์„œ ๋ฐœํ‘œํ•œ๋‹ค.

 

ํŠนํžˆ, ํ† ์ŠคํŠธ ๋ฉ”์‹œ์ง€ ์ชฝ์—์„œ error๊ฐ€ ์žˆ์—ˆ์–ด์„œ error ํ•ด๊ฒฐํ•˜๋Š” ๊ฒƒ๊นŒ์ง€ ์ ์–ด๋‘์—ˆ์œผ๋‹ˆ ๊ผญ ๋ณด๊ธฐ

 

 

โœ… ScaffoldMessenger ์‚ฌ์šฉํ•˜๊ธฐ

import 'package:flutter/material.dart'; // ๋ฐ์Šคํฌํƒ‘, ์•ฑ ๋“ฑ์— ๊ณ ๋ฃจ UI๋ฅผ ์ ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋Š” ๊ตฌ๊ธ€์ด ์ œ๊ณตํ•ด์ฃผ๋Š” ํŒจํ‚ค

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      // ์‹ค์งˆ์ ์œผ๋กœ ๋ชจ๋“  ์•ฑ์„ ๊ฐ์‹ธ๊ณ  ์žˆ๋‹ค.
      title: 'Appbar', // ์•ฑ์„ ์ด์นญํ•˜๋Š” ์ด๋ฆ„ -> ์Šค๋งˆํŠธ ํฐ ์•ฑ์—์„œ ์ตœ๊ทผ ์‚ฌ์šฉํ•œ ์•ฑ ๋ณด์—ฌ์ค„ ๋•Œ์˜ ์ด๋ฆ„
      theme:
          ThemeData(primarySwatch: Colors.green // ํŠน์ •์ƒ‰์˜ ์‘์šฉ๋“ค์„ ๊ธฐ๋ณธ ์ƒ‰์ƒ์œผ๋กœ ์ง€์ •ํ•ด์„œ ์‚ฌ์šฉํ•˜๊ฒ 
              ),
      home: MyPage(), // home์€ ์•ฑ์ด ์‹คํ–‰๋  ๋•Œ ๊ฐ€์žฅ๋จผ์ € ๋ณด์—ฌ์ฃผ๋Š” ํŽ˜์ด
    );
  }
}

class MyPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Snack Bar'),
        centerTitle: true,
      ),
      body: MySnackBar(),
    );
  }
}

class MySnackBar extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(
        child: ElevatedButton(
      child: Text('Show me'),
      onPressed: () {
        ScaffoldMessenger.of(context).showSnackBar(
          // ์Šค์บํด๋“œ๋ฉ”์‹ ์ € ๋งŒ๋“ค๊ธฐ
          // ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ์Šค์บํด๋“œ ์ฐพ์•„์„œ ๋ฐ˜ํ™˜ํ•ด
          SnackBar(
            // ์Šค๋‚ต๋ฐ”
            content: Text(
              'Hello',
              textAlign: TextAlign.center, // ์ค‘์•™์ •๋ ฌ
              style: TextStyle(color: Colors.white), // ํ…์ŠคํŠธ
            ),
            backgroundColor: Colors.teal,
            duration: Duration(seconds: 1),

          ),
        );
      },
    ));
  }
}

ScaffoldMessenger ๊ฒฐ๊ณผ

 

 

 

โœ… ํ† ์ŠคํŠธ ๋ฉ”์‹œ์ง€

์ด๊ฑด ์ƒ๊ฐ๋ณด๋‹ค ๋ณต์žกํ•˜๋‹ค .. ๋ฌผ๋ก  ํ•œ๊ฒฝ์„ค์ • ๋ถ€๋ถ„์ด...

ํ™˜๊ฒฝ๊ณผ ๊ด€๋ จํ•˜์—ฌ์„œ ์ค‘์š”ํ•œ์  ๋ช‡๊ฐ€์ง€...!

1. pubspec.yaml ์ชฝ์— 

fluttertoast: ^8.0.8 ๋ฅผ ๋„ฃ๋Š”๋‹ค.

โ—๏ธ ๋„์–ด์“ฐ๊ธฐ ์ž˜ ๋งž์•„์•ผํ•จ. โ—๏ธ

์ดํ›„์—๋Š” Pub Get์„ ํ†ตํ•ด์„œ ์—…๋ฐ์ดํŠธ ํ•œ๋‹ค.

 

โ—๏ธ์—๋Ÿฌ ๋–ด์„ ๋•Œ ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•โ—๏ธ

[verbose-2:ui_dart_state.cc(199)] unhandled exception: missingpluginexception
(no implementation found for method showtoast on channel ponnamkarthik/fluttertoast) 
#0 methodchannel._invokemethod (package:flutter/src/services/platform_channel.dart:156:7) <asynchronous suspension> 
#1 fluttertoast.showtoast (package:fluttertoast/fluttertoast.dart:100:17) <asynchronous suspension>

1. ์šฐ์„  pubspec.yaml์ชฝ์— ์ธ๋ฑ์Šค๋ฅผ ์ž˜ ๋งž์ถฐ์„œ ์ ์—ˆ๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.

2. ๊ณต์‹ ์‚ฌ์ดํŠธ๋ฅผ ๊ฐ€์„œ ๋ฒ„์ „์ด ๋งž๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.

https://pub.dev/packages/fluttertoast

 

fluttertoast | Flutter Package

Toast Library for Flutter, Easily create toast messages in single line of code

pub.dev

3. ์•ฑ์„ ์™„์ „ํžˆ ๊ป๋‹ค๊ฐ€ ๋‹ค์‹œ ํ‚จ๋‹ค. 

 

import 'package:flutter/material.dart'; // ๋ฐ์Šคํฌํƒ‘, ์•ฑ ๋“ฑ์— ๊ณ ๋ฃจ UI๋ฅผ ์ ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋Š” ๊ตฌ๊ธ€์ด ์ œ๊ณตํ•ด์ฃผ๋Š” ํŒจํ‚ค
import 'package:fluttertoast/fluttertoast.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      // ์‹ค์งˆ์ ์œผ๋กœ ๋ชจ๋“  ์•ฑ์„ ๊ฐ์‹ธ๊ณ  ์žˆ๋‹ค.
      title: 'Appbar', // ์•ฑ์„ ์ด์นญํ•˜๋Š” ์ด๋ฆ„ -> ์Šค๋งˆํŠธ ํฐ ์•ฑ์—์„œ ์ตœ๊ทผ ์‚ฌ์šฉํ•œ ์•ฑ ๋ณด์—ฌ์ค„ ๋•Œ์˜ ์ด๋ฆ„
      theme:
          ThemeData(primarySwatch: Colors.green // ํŠน์ •์ƒ‰์˜ ์‘์šฉ๋“ค์„ ๊ธฐ๋ณธ ์ƒ‰์ƒ์œผ๋กœ ์ง€์ •ํ•ด์„œ ์‚ฌ์šฉํ•˜๊ฒ 
              ),
      home: MyPage(), // home์€ ์•ฑ์ด ์‹คํ–‰๋  ๋•Œ ๊ฐ€์žฅ๋จผ์ € ๋ณด์—ฌ์ฃผ๋Š” ํŽ˜์ด
    );
  }
}

class MyPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Toast Bar'),
        centerTitle: true,
      ),
      body: Center(
        child: TextButton(
          onPressed: () {
            print('TextButton is clicked');
            flutterToast(); // ํ•จ์ˆ˜ ํ˜ธ์ถœ
          },
          child: Text('Toast'),
          //color: Colors.blue
        ),
      ),
    );
  }
}

void flutterToast() {
  Fluttertoast.showToast(
      msg: "This is Center Short Toast", // ๋ฉ”์‹œ์ง€ ๋‚ด์šฉ
      toastLength: Toast.LENGTH_SHORT, // ๋ฉ”์‹œ์ง€ ์‹œ๊ฐ„ - ์•ˆ๋“œ๋กœ์ด๋“œ
      gravity: ToastGravity.BOTTOM, // ๋ฉ”์‹œ์ง€ ์œ„์น˜
      timeInSecForIosWeb: 1, // ๋ฉ”์‹œ์ง€ ์‹œ๊ฐ„ - iOS ๋ฐ ์›น
      backgroundColor: Colors.red, // ๋ฐฐ๊ฒฝ
      textColor: Colors.white, // ๊ธ€์ž
      fontSize: 16.0); // ๊ธ€์ž ํฌ๊ธฐ
}

ํ•จ์ˆ˜๋กœ ๊ตฌํ˜„ํ•œ ๋ถ€๋ถ„ ๋ง๊ณ ๋Š” ํฌ๊ฒŒ ์–ด๋ ค์šด๊ฒŒ ์—†์—ˆ๋‹ค.

๋‹ค๋งŒ, ํ† ์ŠคํŠธ ๋ฉ”์‹œ์ง€ ๋ถ€๋ถ„์—์„œ ์ฃผ์„์„ ํ•œ๋ฒˆ ๊ผผ๊ผผํ•˜๊ฒŒ ๋ณด๊ธฐ๋กœ !

 

ํ”Œ๋Ÿฌํ„ฐ ํ† ์ŠคํŠธ ๋ฉ”์‹œ์ง€

Comments