Search
 
SCRIPT & CODE EXAMPLE
 

DART

widget capture in flutter

import 'package:flutter/material.dart';
import 'dart:async';
import 'dart:typed_data';
import 'dart:ui' as ui;
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';
import 'package:path_provider/path_provider.dart';
import 'dart:io';

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

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Flutter Demo',
      theme: new ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: new MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;

  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  static GlobalKey previewContainer = new GlobalKey();
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      // This call to setState tells the Flutter framework that something has
      // changed in this State, which causes it to rerun the build method below
      // so that the display can reflect the updated values. If we changed
      // _counter without calling setState(), then the build method would not be
      // called again, and so nothing would appear to happen.
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {

    return RepaintBoundary(
        key: previewContainer,
      child: new Scaffold(
      appBar: new AppBar(

        title: new Text(widget.title),
      ),
      body: new Center(
        child: new Column(

          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            new Text(
              'You have pushed the button this many times:',
            ),
            new Text(
              '$_counter',
              style: Theme.of(context).textTheme.display1,
            ),
            new RaisedButton(
                onPressed: takeScreenShot,
              child: const Text('Take a Screenshot'),
            ),
          ],
        ),
      ),
      floatingActionButton: new FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: new Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    )
    );
  }
  takeScreenShot() async{
    RenderRepaintBoundary boundary = previewContainer.currentContext.findRenderObject();
    ui.Image image = await boundary.toImage();
    final directory = (await getApplicationDocumentsDirectory()).path;
    ByteData byteData = await image.toByteData(format: ui.ImageByteFormat.png);
    Uint8List pngBytes = byteData.buffer.asUint8List();
    print(pngBytes);
    File imgFile =new File('$directory/screenshot.png');
    imgFile.writeAsBytes(pngBytes);
  }
}
Comment

PREVIOUS NEXT
Code Example
Dart :: dart get class name 
Dart :: late in dart 
Dart :: comments in dart 
Dart :: flutter map 
Dart :: image not shoing when i use network image,flutter 
Dart :: opendrawer without appbar 
Dart :: dart how to tell if an object is an instance of a class 
Dart :: perform async function in widget build method 
Dart :: flutter add checkbox 
Dart :: flutter sembast delete a single record 
Dart :: dart zip two lists 
Dart :: flutter wait 2 seconds 
Dart :: flutter force soft keyboard on widget 
Dart :: how to stop listening to location change listener on dispose in flutter 
Dart :: dart get href attribute 
Dart :: flutter circular elevated button 
Dart :: flutter obfuscation 
Swift :: swift int to octal 
Swift :: shadow color swiftui 
Swift :: swift enum all cases 
Swift :: date formatter swift 
Swift :: pop the view controller xcode 
Swift :: swift convert data to dictionary 
Swift :: generate random bool swift 
Swift :: swiftui console log 
Swift :: how to loop swift 
Swift :: string to swift 2022 
Swift :: get height of navigation bar swift 
Swift :: swift dictionary get key from valye 
Swift :: switch button swift 5 
ADD CONTENT
Topic
Content
Source link
Name
6+5 =