"Fossies" - the Fresh Open Source Software Archive

Member "flutter-3.7.1/examples/api/lib/widgets/actions/action_listener.0.dart" (1 Feb 2023, 3136 Bytes) of package /linux/misc/flutter-3.7.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Dart source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the last Fossies "Diffs" side-by-side code changes report for "action_listener.0.dart": 3.3.10_vs_3.7.0.

    1 // Copyright 2014 The Flutter Authors. All rights reserved.
    2 // Use of this source code is governed by a BSD-style license that can be
    3 // found in the LICENSE file.
    4 
    5 /// Flutter code sample for [ActionListener].
    6 
    7 import 'package:flutter/material.dart';
    8 
    9 void main() => runApp(const MyApp());
   10 
   11 class MyApp extends StatelessWidget {
   12   const MyApp({super.key});
   13 
   14   static const String _title = 'Flutter Code Sample';
   15 
   16   @override
   17   Widget build(BuildContext context) {
   18     return MaterialApp(
   19       title: _title,
   20       home: Scaffold(
   21         appBar: AppBar(title: const Text(_title)),
   22         body: const Center(
   23           child: MyStatefulWidget(),
   24         ),
   25       ),
   26     );
   27   }
   28 }
   29 
   30 class ActionListenerExample extends StatefulWidget {
   31   const ActionListenerExample({super.key});
   32 
   33   @override
   34   State<ActionListenerExample> createState() => _ActionListenerExampleState();
   35 }
   36 
   37 class _ActionListenerExampleState extends State<ActionListenerExample> {
   38   bool _on = false;
   39   late final MyAction _myAction;
   40 
   41   @override
   42   void initState() {
   43     super.initState();
   44     _myAction = MyAction();
   45   }
   46 
   47   void _toggleState() {
   48     setState(() {
   49       _on = !_on;
   50     });
   51   }
   52 
   53   @override
   54   Widget build(BuildContext context) {
   55     return Row(
   56       mainAxisAlignment: MainAxisAlignment.center,
   57       children: <Widget>[
   58         Padding(
   59           padding: const EdgeInsets.all(8.0),
   60           child: OutlinedButton(
   61             onPressed: _toggleState,
   62             child: Text(_on ? 'Disable' : 'Enable'),
   63           ),
   64         ),
   65         if (_on)
   66           Padding(
   67             padding: const EdgeInsets.all(8.0),
   68             child: ActionListener(
   69               listener: (Action<Intent> action) {
   70                 if (action.intentType == MyIntent) {
   71                   ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
   72                     content: Text('Action Listener Called'),
   73                   ));
   74                 }
   75               },
   76               action: _myAction,
   77               child: ElevatedButton(
   78                 onPressed: () => const ActionDispatcher()
   79                     .invokeAction(_myAction, const MyIntent()),
   80                 child: const Text('Call Action Listener'),
   81               ),
   82             ),
   83           ),
   84         if (!_on) Container(),
   85       ],
   86     );
   87   }
   88 }
   89 
   90 class MyAction extends Action<MyIntent> {
   91   @override
   92   void addActionListener(ActionListenerCallback listener) {
   93     super.addActionListener(listener);
   94     debugPrint('Action Listener was added');
   95   }
   96 
   97   @override
   98   void removeActionListener(ActionListenerCallback listener) {
   99     super.removeActionListener(listener);
  100     debugPrint('Action Listener was removed');
  101   }
  102 
  103   @override
  104   void invoke(covariant MyIntent intent) {
  105     notifyActionListeners();
  106   }
  107 }
  108 
  109 class MyIntent extends Intent {
  110   const MyIntent();
  111 }
  112 
  113 class MyStatefulWidget extends StatefulWidget {
  114   const MyStatefulWidget({super.key});
  115 
  116   @override
  117   State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
  118 }
  119 
  120 class _MyStatefulWidgetState extends State<MyStatefulWidget> {
  121   @override
  122   Widget build(BuildContext context) {
  123     return const ActionListenerExample();
  124   }
  125 }